I'm new to Sockets so need some advice. I've been trying to write my own basic chat app. So far I have it working that a client and server communicate. I have a gui for both and messages that are typed in one, display on the other. Now, I'm trying to create a server to pass messages to clients..i.e.. client A passes message to Client B, it passes through the server. I thought I would start with 2 clients to begin with and if I can get it working expand from there.
My idea to achieve this would be to....
Create a server.
Server creates socket to listen for client.
Client A sends connection request to server.
Server accepts Client A.
Client B sends connection request to server.
Server accepts Client B.
Client A sends message to server.
Server passes message to Client B.
So I'm unclear how to achieve this...how to pass the message through the server to the other client. My thought was to create a port for Client A and a different port for Client B on the server and then somehow pass the message through.
Can anyone offer any help or guidance on how to precede??
Is your server already capable of handling multiple clients?
The server is perfectly fine just with one listening
address as the communication itself will be delegated
to a different socket by your operating system.
What does your server do with the client connection
after accept()?
Shanna Ripley
Greenhorn
Joined: Mar 12, 2010
Posts: 18
posted
0
Matt Cartwright wrote:Howdy Shanna and welcome to the Ranch
Is your server already capable of handling multiple clients?
The server is perfectly fine just with one listening
address as the communication itself will be delegated
to a different socket by your operating system.
What does your server do with the client connection
after accept()?
Thanks Matt
At the mo, the server can't handle multiple clients. It just accepts the connection request from the server on port 4444.
Once the server accepts the client connection after accept(), I create my input and output streams for the text messages to be sent to each other.
Does this help??
This message was edited 1 time. Last update was at by Shanna Irl
My thought was to create a port for Client A and a different port for Client B on the server and then somehow pass the message through.
Server always listen on the same port. Client will connect to the server with different Ip address in the same port number.
-Prabhakaran
Hmm, not really sure what you are trying to say...
A client connected to one IP address will stick with that IP address.
And the same is true for the port.
Example:
ssh daemon (sshd), the server is listening on port 22 on all IP addresses available on one machine.
From the netstat output we can see that sshd is listening on port
22 on all IPv4 and IPv6 interfaces of this machine.
The first column is the protocol in use. In our case it is Transmission Control Protocol (TCP).
The second column is the depth of the receive queue. Zero is good, no backlog.
The third column is the send queue depth.
Column four is the IP address and port the server is listening on.
An IPv4 address of 0.0.0.0 and an IPv6 address of ::: means "any interface that is up".
Columns five is IP address and port the client is connecting from. And in this
case where no one is connected, basically the addresses our server will accept.
The last column shows the status on the socket. It says LISTENING,
pretty obvious, the server is listening.
Now let's see what it looks like when a client connects.
The netstat output tells us:
sshd has a connection accepted on 192.168.1.80 port 22
a remote client is connected from 192.168.1.65 port 59235
even though something in sshd is dealing with the client conected to port 22, the server is still listening on port 22
So what does it look like with more than one client connected?
now we have two more clients connected.
One more from 192.168.1.65 (same IP address) from port 58132.
and another from 206.169.85.86 (different IP) from port 59446
So, what happens inside sshd?
The socket sshd is listening on, is equivalent to our ServerSocket in Java. The socket used by sshd for handling client connections
is our Socket.
Depending on your operating system implementation, these might
be different sockets (internally, and hidden from us) and thus,
different streams.
If your OS supports the system call select() it will be the same
socket with multiple streams associated.
However, we get different streams for listening and each individual
client.
This allows us to handle client connections in the client handler
while the server loop continues listening.
Hope this made it a bit more clear
Matt
This message was edited 1 time. Last update was at by Matt Cartwright