|
|
What happens to an accepted socket in a server?
|
|
|
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
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
|
|
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.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
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
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|