Hint: a program which is not running can't do much. Take a look at your
code again.
Ok, look again.
Are you still missing it? Apparently, since you took the time to type
all this out to the list and still didn't realize it.
Notice the part where you close(file)? Yeah, that closes the socket.
Notice the part where there's a right curly brace? Yeah, that terminates
your program.
A closed socket can't accept().
A terminated program can't make new sockets.
Forgive my sarcasm here but you should have thought about this more
before posting it to the list.
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]