LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   cannot connect to a socket from other computer (https://www.linuxquestions.org/questions/linux-networking-3/cannot-connect-to-a-socket-from-other-computer-313010/)

duongvn 04-13-2005 09:16 PM

cannot connect to a socket from other computer
 
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;
}

macemoneta 04-13-2005 10:28 PM

It sounds like you have a firewall blocking the connection. Are you sure the port is permitted access? Also, if you have a NAT-type router in place, you will need to configure it for port forwarding (see the documentation for the router). If neither of these apply, please please specify the networking environment in as much detail as possible.

duongvn 04-14-2005 03:03 AM

firewall ???
 
Thanks for you reply.
I don't think the reason is firewall, just because I can ssh, sftp, telnet form other machines to the host that I run my server program.
I am using HP Proliant sysytem with 4 parallel computers, running RedHat Enterprise Linux 3.
These 4 processors are connected to CenterCOM 9606T - Layer 3 Gigabit Ethernet switch. The switch is connected to Internet zone via optical cable.
My problem is: I couldn't connect to the TCP socket (of my program) from computers in the same segment.
Would you need any further information, please let me know.
Thanks.

macemoneta 04-14-2005 09:43 AM

Try this... Start the listening side of the application. Verify operation on the same host, then from the remote host you're having a problem with, telnet to the listening host and port. For example:

telnet somehost 123

If you don't get a connect, then something is blocking your communications. That will be a firewall (hardware or software), or a NATing router without a port forward for the port you're using. You'll need to contact your administrator to enable your communications to continue.

duongvn 04-14-2005 11:11 PM

I have tried
$> telnet myseverhost 3300
from other machines in the same LAN (pluged into the same switch) but it doesn't work.
I have asked my admin about this problem but he also doesn't know how to open the firewall (if it is).
Thank you for your instructions.
Now it works. Thanks you very much.


All times are GMT -5. The time now is 10:41 AM.