LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-13-2005, 09:16 PM   #1
duongvn
LQ Newbie
 
Registered: Apr 2005
Location: Kobe, Japan
Posts: 4

Rep: Reputation: 0
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;
}
 
Old 04-13-2005, 10:28 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
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.
 
Old 04-14-2005, 03:03 AM   #3
duongvn
LQ Newbie
 
Registered: Apr 2005
Location: Kobe, Japan
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 04-14-2005, 09:43 AM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
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.
 
Old 04-14-2005, 11:11 PM   #5
duongvn
LQ Newbie
 
Registered: Apr 2005
Location: Kobe, Japan
Posts: 4

Original Poster
Rep: Reputation: 0
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.

Last edited by duongvn; 04-15-2005 at 05:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
Socket error on Connect mdimanna Programming 2 06-24-2005 10:28 AM
MySQL can't connect through socket! Avatar Linux - Software 15 08-10-2004 01:25 PM
Mysql problem - can`t connect to socket TiCkO Linux - Software 7 05-22-2004 04:21 PM
connect: socket operation on non-socket bit7 Linux - Networking 0 02-18-2003 05:08 PM


All times are GMT -5. The time now is 02:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration