Hello there,
I am trying to write a simple C program that allow user to connect to a TCP port.
I have built it on RedHat Linux 9 and it works well.
When building and run in RedHat Enterprise Linux 3, there is a weird problem that I hardly understand: When I try to connect to that TCP port from localhost, it works, but connect from outside (other computers), the accept() function in the server didn't work.
The net status (netstat -a) in client:
Proto Revc-Q Send-Q ... State
...
tcp 0 1 SYN_SENT
...
Could anyone show me how to solve this. Do I have to change my code or update Linux ?
Thanks for instructions.
Here is the code:
int main(int argc, char *argv[])
{
int s, c;
socklen_t cli_size;
struct sockaddr_in srv, cli;
if (argc != 2) {
fprintf(stderr, "usage: %s port\n", argv[0]);
return 1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) {
perror("socket() failed");
return 2;
}
srv.sin_addr.s_addr = INADDR_ANY;
srv.sin_port = htons( (unsigned short int) atol(argv[1]));
srv.sin_family = AF_INET;
if (bind(s, (struct sockaddr *) &srv, sizeof(srv)) == -1) {
perror("bind() failed");
return 3;
}
if (listen(s, 1) == -1) {
perror("listen() failed");
return 4;
}
printf("Service is listening at port %s\n", argv[1]);
for(;

{
printf("Waiting for connection...\n");
cli_size = sizeof(cli);
c = accept(s, &cli, &cli_size);
if (c == -1) {
perror("accept() failed");
return 5;
}
// printf("client from %s\n", inet_ntoa(cli.sin_addr));
if (!handling(c))
fprintf(stderr, "%s: handling() failed\n", argv[0]);
close(c);
}
return 0;
}