LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2005, 09:36 AM   #1
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Rep: Reputation: 15
Networking:socket ip address and port??? Tricky situations!!!


Hello Guys

I have got some misunderstanding about socket and ip addresses and ports, connect and accept functionality.
Would you please help me?!
/------------------------------------/
Situation 1
We have host1 with 195.137.203.134 and host2 195.137.203.133

Host1 listen on 1234 host2 connects (from port 49076) to host1
Host2 has already set up connection with Host1 (195.137.203.134:1234 < - >195.137.203.133: 49076)
Host1 continues listening on 1234 host2 tries to connect (from port 49076) to host1 once more

What will happen in this situation?
Will the new connection be set up? How will this two connection be distinguished?

On Linux and Win32 platforms!!!

Situation 2

We have host1 with 195.137.203.134 and host2 195.137.203.133 and host3 195.137.203.135

Host1 listen on 1234 host2 connects (from port 49076) to host1
Host2 has already set up connection with Host1 (195.137.203.134:1234 < - >195.137.203.133: 49076)
Host2 listen also on 49076 host3 connects (from port 13718) to host2

What will happen in this situation?
Will the new connection (195.137.203.133:49076 < - >195.137.203.135:13718) be set up?
If so why it can happen otherwise what will happen?

On Linux and Win32 platforms!!!

Situation 3 (combining two mentioned before)

We have host1 with 195.137.203.134 and host2 195.137.203.133 and host3 195.137.203.135

Host1 listen on 1234 host2 connects (from port 49076) to host1
Host2 has already set up connection with Host1 (195.137.203.134:1234 < - >195.137.203.133:49076)
Host1 listen on 1234 host3 connects (from port 13718) to host1
Host2 has already set up connection with Host1 (195.137.203.134:1234 < - >195.137.203.135:13718)

Host2 listen also on 49076 host3 connects (from port 13718) to host2
Host3 listen also on 13718 host2 connects (from port 49076) to host3

What will happen in this situation?

On Linux and Win32 platforms!!!
/-------------------------------/


I do not know how connect from the same port maybe using the same socket twice or like that???

Some one can say it is impossible and full of errors Some one can say it will works but Would you please give your understanding why on Linux and Win32 platforms!!!


Thanks a lot guys in advance
Any help will be appreciated



Last edited by Igor007; 11-12-2005 at 09:40 AM.
 
Old 11-12-2005, 10:43 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
a client can not make a socket and bind to a port which is already in use. lol thats a short anwser
 
Old 11-12-2005, 10:49 AM   #3
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 33
1. i would advise you to not post public ip-addresses, sensor them with something, ie: aaa.bbb.ccc.134 and aaa.bbb.ccc.133

2. usually host2 will pick random high ports to connect to a single port on any server, ie: you can connect multiple times to the same webserver and request different pages, the difference is the port the client opens when connecting to a server.

so posting client connection related ports does not really make sence since they vary for each connection (most of the time).

3. a client seldom never a port for an outgoing connection it listens on for some other service. if you try to force that, it will fail.

for most services you run on lower ports (~ 1 - 16000 something), outgoing connection related ports usually very high ports (~ 33000-65000).

when a client makes a connection to a server, it picks a random port, and connects to a fixed port on the server (ie. 80:http) and will listen on that port for response packets from the server it connected to.

4. a socket is something completly different. a server for example can listen on a certain port, and create a 'socket' on the server for other local applications to connect through (ie. mysql will listen on port 3306 and create a socket, where lets say cyrus-mysql can connect through, to not have to connect through tcp on the loopback interface.)
 
Old 11-12-2005, 11:12 AM   #4
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
Thanks Guys
I would like to make clear my questions

I am familiar with stuff how server and client function and organized (from programmerís point of view)
The questions are:

Can connect()ion be made from same address and port twice successfully (one time connect() successfully once more successfully from same address and port) what will happen from programmerís point of view (sockets) ???





Can connect()ing and accept()ing be made using same address and port at the same time what will happen from programmerís point of view (sockets) ???

Some stuff about this I have found here
http://www.omnetpp.org/listarchive/msg05234.php

What do you think


Please share your experience and understanding
 
Old 11-12-2005, 11:14 AM   #5
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 33
NO...
and sockets dont use ports!
 
Old 11-12-2005, 11:36 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by Nathanael
NO...
and sockets dont use ports!
IT SEEMS YOU HAVE AN ATTITUDE PROBLEM AND LIKE TO SHOUT!
in addition whats "recieving help is a privileg, not a right!" all about? after all this is a forum with the word "questions" in the url!

if sockets don't use ports why do you bind a socket to a SOCKADDR which erm hmmm erm has a port?
and on the other side connect a socket with a SOCKADDR which once again has an erm thingy erm port?

Last edited by dmail; 11-12-2005 at 11:40 AM.
 
Old 11-12-2005, 11:39 AM   #7
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
Nathanael

socket uses ports and ip addresses
See here short overview of NetWorking programming:

http://beej.us/guide/bgnet/output/html/syscalls.html
 
Old 11-12-2005, 11:45 AM   #8
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 33
appologize...
 
Old 11-12-2005, 01:05 PM   #9
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Can connect()ion be made from same address and port twice successfully (one time connect() successfully once more successfully from same address and port) what will happen from programmerís point of view (sockets) ???
Yes a client can connect to the same Address and Port number twice. You have to set up the socket options on the client i beleive to allow reuse of the address and port number. This will allow more then one connect to be made on an address an port number. Since the server asses a new port number to each client that attaches to it, and leaves the listening socket free to accept new connects more then one client at attach at a time. To the program you will have two sockets pointing to the same server, but on the server since there will be two sockets with different port numbers that it must use. On the server side you have to be careful if you are listen for connections in one thread and read/writing in another since the server could overwrite the socket you are using the a new client tries to connect.
 
Old 11-12-2005, 02:22 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
A "socket" is a programming abstraction.

Using the sockets API, you can initiate, read and write from, and finally close a TCP/IP "Connection" (among other things).

Each TCP/IP "Connection" consists of two "Endpoints".

Each "Endpoint" consists of a host address (IP address) and a port number.

The server process opens a TCP/IP "listening socket" and "binds" to the port it wishes to service (let's call it port "P"). At this point, no other process on the host can bind to that port. If they try, they'll get a "port in use error".

And at that point, no client can talk to the server. The server process must first "listen()".

Now the client can try to talk to the host. The client must know the server's IP address, and must know the port number ("P"). The client opens its own socket and attempts to "connect()".

Key point: at this point, clients *STILL* cannot talk to the server. The server must also do an "accept()" on the listener socket.

A client attempts to "connect()"; the server does a corresponding "accept()". Another key point: at this point, a NEW socket is created on the server. A socket with TWO endpoints:
Endpoint 1: Client IP::Port "P"
Endpoint 2: Server IP::"Ephemeral port number"

The client and server communicate on this NEW socket, representing a UNIQUE connection. The server (assuming it's multithreaded) is free to go and accept new connections. "Ephemeral ports" are just a system-assigned numbers. They're a relatively scarce resource, and can easily be exhausted on a busy server with the default TCP/IP settings. The new socket is closed ONLY when both hosts close their respective endpoints.

TWO SUGGESTIONS:
1. Get a copy of "The Bible":
Unix Network Programming, W. Richard Stevens:
http://www.bookpool.com/sm/0131411551

2. Get a copy of "Ethereal" and analyze a trace of a simple client/server "hello world" connection:
http://www.ethereal.com/download.html

'Hope that helps .. PSM

Last edited by paulsm4; 11-12-2005 at 02:24 PM.
 
Old 11-12-2005, 03:48 PM   #11
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
paulsm4, FLLinux

Guys thanks a lot for giving me basics of network programming I read Stevens book It is a wonderful book. I have not got the answer my questions. May be I am to curious. But I think it can be answered.

My questions are related of port and ip address re usage on Linux (and also win 32 platforms)

---------------------------------------------------------------------------------


I am familiar with options (setsockopt, SO_REUSEADDR,SO_REUSEPORT(cannot be used on Win32))

Thank FLLinux You answered for one main question


But I have also second concerning the using the same port and ip address for listen()ing ( accept() ) and connect()ing at the same time


Will listen(), accept() (on ) and connect() (from) work properly using the same port and ip address???

Thanks in advance

Last edited by Igor007; 11-12-2005 at 03:53 PM.
 
Old 11-12-2005, 04:33 PM   #12
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Will listen(), accept() (on ) and connect() (from) work properly using the same port and ip address???

The anwser to this is a something like this i think.

You can Listen, Accept (server side) on the same Ip Address, but not the same port number. What i mean is you have have a server listen to say IP 127.0.0.1 on many different ports, but you can't have the two different sockets listening to the same Port number. This would be kind of strange since I don't know if both sockets would get the connection request at the same time or what.

For connect (client side) can have more then one socket attached to the same IP and Port number. You would have two instances connected to the same server, but useing the sockopt you can have this happen.

I beleive that is how it works under Linux since you are using the BSD sockets more directly then the WinSock API in windoze. I would have to look into how windoze does this.

Hope this muddies up the water a little more.
 
Old 11-12-2005, 04:51 PM   #13
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"Endpoints", folks. Think "Endpoints"!

PS:
Igor007 -

1) I'm not sure I completely understand your question. You're either asking something fairly subtle - or something very fundamental. I'm not sure which!

Please re-read my post, please re-state your question in terms of "endpoints", and I'd be happy to help clarify.

3) I'm definitely glad you're familiar with Stevens - it's an excellent book.

Please, too, consider trying out Ethereal, and analyzing a simple connection.

3) You should see the following:
a) When the client tries to "connect()", it will send a SYN packet to
the server.
b) When (if) the server does an "accept()", the server will send an ACK to
the client's SYN request, and also send its own SYN request (typically
in the same packet).
c) To initiate communications, the client will ACK the server's SYN.

This is the so-called "three-way handshake".

As you analyze the packets, pay close attention to the source and destination IP addresses and port numbers in each packet.

I honestly think this little "experiment" with Ethereal will give you a more meaningful explanation to your question.

Good luck!


PPS:
TCP/IP is TCP/IP, and connections are connections. Whether you're on Linux, on BSD or on Windows. Whether you're programming them in standard Unix/Linux/BSD sockets, in "Winsock", or in "Java Sockets". Or you're using SysV streams (should you wish to do something so perverse!) or you're talking mano-a-mano with your NIC interface (should you wish to bypass sockets altogether).

PPS:
SO_REUSEADDR and SO_REUSEPORT are *server side* socket options - they have nothing directly to do with a client "connect()"!

Last edited by paulsm4; 11-12-2005 at 05:11 PM.
 
Old 11-13-2005, 07:57 AM   #14
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
Hi guys
Thanks your answers a lot

The situation I am facing with is the following: the application bind()ing the same IP address(for connecting sckets and "0.0.0.0" for listenning one) and port number for a sockets used for a few connect()ions and also listen()ing, accept()ing the incoming connections at the same time.

As I can see it is possible that the application uses non-blocking sockets (using select()ing ).

It also sets SO_REUSEADDR option befor bind()ing

What's wrong with this situation???
Will it work fine???

Last edited by Igor007; 11-13-2005 at 07:59 AM.
 
Old 11-20-2005, 03:26 AM   #15
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
The application bind()ing the same IP address(for connecting sckets and "0.0.0.0" for listenning one) and port number for a sockets used for a few connect()ions and also listen()ing, accept()ing the incoming connections at the same time.

As I can see it is possible that the application uses non-blocking sockets (using select()ing ).

It also sets SO_REUSEADDR option befor bind()ing


Will it work smoothly ???
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket error when spoofing source address David Knecht SUSE / openSUSE 0 06-15-2005 08:01 AM
unable to obtain socket and address of client David@330 Linux - Newbie 5 06-01-2005 10:23 AM
cannot bind to socket: address already in use exfacior Linux - Wireless Networking 0 09-27-2004 01:34 PM
Help me with this socket address bind program. rnice Linux - Networking 0 09-17-2004 08:33 PM
could not listen on UDP socket: address in use peaceofcrap2001 Linux - Networking 2 07-10-2004 01:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration