I have created a ServerSocket binded with a port in my application. I distribute this application to the connected PCs on the same network. I can post a request to any of the PC where the application is installed, using this port. Now the recepients can receive the messages, but when they send the response back to the requesting PC, the Input stream never receives the message. Is there a way to do it. I do not know any thing about URGENT TCP messages. I enabled it but I do not know how to send them. Is my message being discarded by default? I do not get the answer in other threads.
Do you flush the outputstream on the PCs sending data?
// Send data here
yourOutputStreamReference.flush();
There should be a loop in both ClientSocket and ServerSocket that keeps communication in the active state.
Otherwise: There should be implementation of both client socket and a server socket on all the sides (all the sender and receiver devices). When a server socket A receives a message, in order to reply back, there should be a client socket that replies back to server socket B which resides in the sender's application in his device.
In this way, all the devices with a server socket with the same port can detect and send messages to each other.
Of course the IP address of the sender should be kept in order to reply back.
Related
I am using socket.io-java-client for connecting my java class on the server side to node.js and emit some events.
since I am running this on the server I dont want the socket thread to be running always.
As soon as my emit is done I want to disconnect the socket.
I tried
SocketIO socket=new SocketIO("http://IP:9001");
socket.emit("EVENT", "data");
socket.disconnect();
but this fails because we are closing the socket even before it has sent the message.
Is there any handler for emit success? How can I close the socket after the emit is successful?
After you've sent message to server, server can drop connection from its side. Just on event of receiving specific message it simply can disconnect that client socket.
Or server can additionally send response and client can close him self on receiving this response. But server should secure him self creating timeout in order to close idle clients who did not closed them self.
I recommend to do this operation on server side, and do not ever trust client side with such decisions.
Client can do it additionally after some timeout.
If you use Socket.IO just to send one message and close it after message sent, then there is no point to use Socket.IO as it will have overhead based on handshaking process, and you might consider using just HTTP request in order to send single messages to server.
I have a client socket which sends messages to the server.I want to get an acknowledgement on the client side whenever the server receives the message. Is it possible to get that acknowledgement. I developed the client using apache mina. Thanks in advance.
There are no messages in TCP, only a byte stream. There is an internal ACK mechanism that tracks how much of the stream has been correctly received, but it is not available to applications. If you want an acknowledgment from the server, your server will have to send it.
I've got UDPserver which recieves messages from clients and sends response to them. But I need to check if UDPclients is online or not.
With isReachable I can test whether pc online or not. But when differents clients use same pc but with differents ports how to check whether port is open or not(Client1 with port 5678 is online but client2 6777 is off.IP 192.168.1.7 IP isReachable in this case but client2 is not)?
UDP is a connectionless, unreliable packet-oriented protocol. If there is no socket bound to a particular port on the remote machine then the packet will simply be dropped.
You would have to implement some sort of protocol which supported detection of whether a client is online or not. Perhaps sending a keep-alive type message periodically.
You're conflating two different concepts here. The 'isReachable' function is an IP-layer property which is simply checking to see if the IP address in question is responding to IP-protocol packets (which it is).
UDP is a broadcast datagram protocol designed for unacknowledged data broadcasts over IP, so it has no inbuilt concept of 'reachability'. Your UDP client must somehow watch for incoming detection messages, and respond to them accordingly.
IsReachable will only do a ICMP echo request (ping) on the given host so event if your client is not running but as long as your host responds to ICM requests it will show as reachable.
If you which to check if your remote client is running, you will need to implements a simple "ping" protocol : ie: your server will send a special message to the client and the client has to respond to it.
If the only issue is to know if a client is alive, then keep-alive message from the client is easiest. You can identify the client for example from the content of the package (or the sender port).
If you wish to send messages to the clients, I think you'll have to have a UDP server on both ends, and then exchange the ports the 'client-servers' are listening on and use that for communications.
Of course, the latter will cause issues with firewalls as most will block all incoming traffic.
1, my server has a ServerSocket to accept client connection
2, my client create a socket and and send message to the server.
3, the server gets the message and reply to the client.
So it's always the client that initiate the communication.
Say some time later after initial client connection, the server wants to send another message to the client, is it possible to do so?
If you keep the socket connection from the client open on the server yes. The client would need to be always listening for data coming in.
Otherwise the client will have to poll in intervals to the server if the connection on the server is closed after a message is sent.
The client must always initiate the connection, that's pretty much the only distinction between a server and a client (a program can be both be a server and a client).
However once a connection is established, as long as it's not closed either side can send data to the other (assuming the other side is listening for data).
I am trying to implement a minimal chat server in java over regular TCP protocol. The chat server will listen on a specific port. The question I have is if there are multiple clients sending messages to the same port, can the server distinguish between the clients and respond to each individually if the messages do not contain the IP address or destination name of the client?
to make my question a bit more clear, suppose the server gets a packet that contains only
"user: abc to-user:efg message:"Hello""
Can I find out in java the address of the client who sent the packet and respond back to the same address or will I need to include some identifier in the message itself like "sender-ip = 1.1.1.1"
Multiple clients will never send data over the same port. The only time your clients will talk over the same port is when they will connect to the server. In the server, whenever the ServerSocket receives a connection it returns a new Socket. This socket is a combination of the following : Server IP+ServerPort and Client IP+Client Port. The Server IP and the Server Port will be same for each socket; what differs is the client IP and Port. Usually this socket is passed to a new thread for further communication while the ServerSocket goes back to listen to incoming connections. Once you have a reference to the socket you can call socket..getInetAddress().getHostAddress() to get the remote IP and socket.getPort() to get the port of the respective client.
Yes, each connection will be separate - you'll have a different stream to read from for each connection. It's up to you to associate the relevant user information with the connection though.