Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > What happens to an accepted socket in a server?

What happens to an accepted socket in a server?
Thread Tools
itistoday
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Oct 24, 2004, 10:40 PM
 
In a java based multi-client server, you have a server that listens continuously for incoming connections on a port. When a connection is "accepted", a thread is launched with that accepted socket.

My question is this: In Java, and I think also in C, you do not specify what port this new socket is opened on, the socket is just "created". Do the languages automatically assign the socket to a new random port? And how are these ports determined?

I'm pretty sure the new sockets aren't created on the same port as the server's listening socket, right?
     
fromthecloud
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Oct 25, 2004, 12:31 PM
 
Originally posted by itistoday:
In a java based multi-client server, you have a server that listens continuously for incoming connections on a port. When a connection is "accepted", a thread is launched with that accepted socket.

My question is this: In Java, and I think also in C, you do not specify what port this new socket is opened on, the socket is just "created". Do the languages automatically assign the socket to a new random port? And how are these ports determined?

I'm pretty sure the new sockets aren't created on the same port as the server's listening socket, right?
The new socket is created on the same port as the original listening socket. When accept() is called, it creates a new socket that is established on the same port for you to send() and recv(). The old socket continues to listen for new connections.
chown -R us:us yourbase

Dissent is not un-American.
     
itistoday  (op)
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Oct 25, 2004, 05:28 PM
 
Originally posted by fromthecloud:
The new socket is created on the same port as the original listening socket. When accept() is called, it creates a new socket that is established on the same port for you to send() and recv(). The old socket continues to listen for new connections.
So how does the information not get confused with other sockets... headers probably?
     
fromthecloud
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Oct 25, 2004, 06:23 PM
 
Originally posted by itistoday:
So how does the information not get confused with other sockets... headers probably?
Most likely. When a TCP connection is accepted it becomes a pipe. It doesn't see anything else, it just goes straight from source to destination.
chown -R us:us yourbase

Dissent is not un-American.
     
itistoday  (op)
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Oct 25, 2004, 06:27 PM
 
Originally posted by fromthecloud:
Most likely. When a TCP connection is accepted it becomes a pipe. It doesn't see anything else, it just goes straight from source to destination.
Alright thanks for clearing that up.
     
hayesk
Guest
Status:
Reply With Quote
Oct 26, 2004, 09:37 AM
 
Originally posted by itistoday:
So how does the information not get confused with other sockets... headers probably?
It is sent during the negotiation when the first connection is attempted. Very simply, the client knocks on port x and says "hey, wanna chat". The server says "sure, but let's not keep port x busy, call me on port y". The client then knows to call on port y and says "hey, it's me again" on port y and the connection is made.
     
itistoday  (op)
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Oct 26, 2004, 03:11 PM
 
Originally posted by hayesk:
It is sent during the negotiation when the first connection is attempted. Very simply, the client knocks on port x and says "hey, wanna chat". The server says "sure, but let's not keep port x busy, call me on port y". The client then knows to call on port y and says "hey, it's me again" on port y and the connection is made.
Ok, this is different from what fromthecloud is saying. Who's right? Are these new connections going through different ports or the same ports? And if they are different ports, does C and Java automatically assign them to new ports?

     
hayesk
Guest
Status:
Reply With Quote
Oct 27, 2004, 04:23 PM
 
Originally posted by itistoday:
Ok, this is different from what fromthecloud is saying. Who's right? Are these new connections going through different ports or the same ports? And if they are different ports, does C and Java automatically assign them to new ports?
fromthecloud is correct, my explanation is just on a lower level. The TCP stack takes care of that for you. The language doesn't define it, but the TCP libraries that come with Java or with the OS do.

You don't need to worry about it in your code, unless you are writing your own TCP stack.
     
   
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:05 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,