My most humble apologies! I didn't notice that you were running test1
again after the first connection.

You must set a socket option on the socket that tells bind() to reuse
the address. You can do this like so, AFTER creating the socket and
BEFORE binding it:

int reuse;

reuse = 1;
setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

See setsockopt(2) for more info. Once again please forgive my
a-little-too-quick-ness today.

Scott

Stephen Inkpen wrote:
> 
> When I run this the first time I can telnet into the port '5000' and connect.
> But after disconnecting I can't connect again afterwards....  Is there some
> sort of timeout thing or something?
> 
> ---------------screen insert--------------------
> beta:/mnt/coding/network$ ./test1 &
> [1] 1617
> beta:/mnt/coding/network$ telnet beta 5000
> Trying 192.168.1.4...
> Connected to beta.
> Escape character is '^]'.
> helloConnection closed by foreign host.
> [1]+  Done                    ./test1
> beta:/mnt/coding/network$ ./test1 &
> [1] 1619
> beta:/mnt/coding/network$ telnet beta 5000
> Trying 192.168.1.4...
> telnet: Unable to connect to remote host: Connection refused
> beta:/mnt/coding/network$
> --------------------------------------------
> 
> Here is my code:
> 
> ----------------------------------------------------------
> #include <string.h>
> #include <sys/socket.h>
> #include <sys/types.h>
> #include <netinet/in.h>
> 
> #define PORT    5000
> 
> int main()
> {
>     struct sockaddr_in local_addr;
>     struct sockaddr_in remote_addr;
>     int file,remote_file,size,length;
>     char *response = "hello";
> 
>     file = socket(AF_INET,SOCK_STREAM,0);
>     if (file == -1)
>         printf("No socket call work");
> 
>     local_addr.sin_family = AF_INET;
>     local_addr.sin_port = htons(PORT);
>     local_addr.sin_addr.s_addr = inet_addr("192.168.1.4");
>     memset(&(local_addr.sin_zero),0,8);
> 
>     if (bind(file, (struct sockaddr *) &local_addr, sizeof(struct sockaddr)) == -1)
>         printf("Bind no work");
> 
>     listen(file,5);
>     size = sizeof(struct sockaddr_in);
>     remote_file = accept(file, (struct sockaddr *) &remote_addr, &size);
> 
>     length = strlen(response);
>     send(remote_file,response,length, 0);
> 
>     close(file);
>     close(remote_file);
> }
> 
> -----------------------------------------------------
> -
> To unsubscribe from this list: send the line "unsubscribe linux-net" in
> the body of a message to [EMAIL PROTECTED]
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]

Reply via email to