This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hello all, I am writting a Chat Server/Cleint program. I connect to the server using the port 5001. What I want to know is can I use the same port (5001) of the client to connect to another client for a Private Chat - Personal chat I mean. What I am trying to do is, making the one client a server and the other client the client for that perticular chat. Thank you very much.
SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
I think you wanted to establish private chat directly between two clients, bypassing the server. You make a socket connection with a two part address: IP address of the machine plus a socket number. So you certainly could open a connection to server:5001 and a connection to peer:5001. The machine addresses are different so the connections are distinct.
You might want to make a "well known" socket available to accept invitations to private sessions, but dynamically find an available socket to carry on the conversation. Then you can carry on severy private chats at the same time.
Say I call you on 5001 and say "let's chat". You could open a server socket on 5002 (keep adding 1 until you find an open one) and respond to me "switch to 5002". While we're chatting on 5002, maybe Lucy calls you on 5001 and says "Let's chat". You look again and find 5003 open and respond to Lucy "switch to 5003". Now you can keep your two private chats straight.
This kind of this is common in some protocols. It drives firewall configuration nuts. What ports should they leave open? But if you're all on the same network, it's not a big deal.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jul 03, 2001
I cannot see clear the scenario just described above. Sockets bound to ports 5002, 5003 and so on shouldn't be ServerSocket IMHO.
Say all the clients are listening for private-chat requests on a port known by the others, for instance 5001. When the ServerSocket.accept blocked on this port returns a Socket, such socket is already connected to the client making the connect call. Thus there is no need of telling the client to switch to any new socket because it has already been connected. Socket.getLocalAddress will tell us the IP to wich the connected socket was bound. While getInetAddress() will return the client IP.
Joined: Feb 18, 2003
Hello all, Thanks for the answers. Let me explain it more. Say I am a chat client connected to the Server using the Socket 5001. You are also a chat client connected to the same server using the socket 5001. Now I want to have a private chat with you. So for that I have to make another connection directly with you. There is no server involvement for this. What my question is for the private chat can I use the same socket 5001 to connect to you. Hope you got the clear picture. Thank you very much.
I think Stan gave perfect ans. You can use any port to establish client2client connection. Just make sure that the client's program has a socket listener on that port.
Joined: Jan 29, 2003
Jose, you're right, there's no need to switch. Every time a ServerSocket accepts a new client they get a new connection and the ServerSocket is available again. I just wrote a web server that does this; shoulda been fresh in my mind. I was just digging the CB culture from Convoy days.
AFAIK it depends. If you connect to a http-server, you ask for a webpage. The server returns the webpage, and the connection is lost. For the next page, you open a new connection.
I don't know the english technical term, but translated from german, word by word, it is: 'connectionless protocol'. You might implement a 'connected protocol', and then you may only manage one connection on a port. But for chatting, you only have very small datagrams, so the time you are connected is very small, and a lot of connections might be opened in turn, giving you the feeling to be connect to lots of hosts.
Right. A web server releases the socket as soon as it has sent the page. A chat server could just hold on to it and converse all day long, I think. It's worth a try anyhow.
Connection-ness is really strange. As I recall, TCP is connectionless, IP is connection oriented, HTTP is connectionless but web applications can be connection oriented. It's very cool how the various layers in the stack can maintain connectsions or not.
Joined: Jul 03, 2001
ransika, I guess we are talking about TCP. 5001 is the listening port the server uses? Or is the port each client is bound to? The former means that very same port should be free in each client. The latter implies is busy in the clients and you cannot use it for another connection.
You need to make one of the clients to establish a connection to a known port the other client is listening to for a private-chat request. -----------------------------------------------------------------------------
IP uses either TCP (connection oriented) or UDP (conectionless)
Stefan, Have you invoked Socket.getLocalPort on the socket returned by ServerSocket.accept ? Though the Custom Networking Tutorial in the Sun site says a ServerSocket returns a socket that is bound to a new port, that method returns the same port the ServerSocket is listening to. If another client connects the returned port is still the same. There is no possible confussion because the other end of such sockets are all different, at least their IPs must be different.
Dou you know if it happens the same with an HTTP server? [ May 23, 2004: Message edited by: Jose Botella ]