Transferring data using java socket - java

I have developed a very simple Client and a Server softwares using Java socket. The client generates a "Draw object" and I'm able to send it to the server correctly trough a socket (I'm using Gson to convert it to a String, since my client will be an Android device and RMI is not available natively).
The point is that I need to send many "Draw objects" to my server (one object per second, for N minutes).
My code currently does this:
Open socket connection
Capture the Draw Object
Send object to Server
Close socket connection
On the server side a have a Thread that loops (inside the run method) that receives the data and manipulates it.
My question is: Is there a way to send them (continuously) without need to close the client socket connection or is this the correct pattern?
To be more specific. Is there a way to have the following instructions?
Open socket connection
Loop
2.1. Capture draw object & send it to server
When draws are over, close connection

Related

java help understanding how socket connections work

I am completely new to creating a network connection in java so I apologize if this is a stupid question.
I am trying to create a D&D companion in java that will allow a player to create their character and then send it to the DM so that they can view it and make changes and send it back to the player. I want to be able to make it so that any time a field is changed on one computer it will also be changed on the other computer.
After a bunch of research online I have been able to create a socket connection between the DM(server) and the player(client) and pass a message between the two but I am not sure how a socket connection works after this initial connection is made. My research has not been very clear on this. I have found many resources that have said that java closes the socket after a message has been passed and many that say that the socket stays open.
If java closes the socket then my problem is easy enough to solve because then I will just have to open a new socket every time I need to pass data making sure that I pass the IP address of the client to the server the first time I make a connection.
My real questions come in when a socket stays open.
If the socket stays open and multiple clients connect to the server, will the server just shout over the network whenever it transmits a message so that all clients receive the message? (If this is the case then I know I can just attach a username to the front of the message so that the client can determine if the server is talking to it.)
If the server does not shout then how do I specify which client I want the server to talk to?
Will I have to add a loop to my receive methods so that the client/server is constantly listening for a transmission from the server/client or will java automatically do so after I run the method the first time?
I have found many resources that have said that java closes the socket after a message has been passed
You found them where?
and many that say that the socket stays open.
All those are correct. Java never closes connections. The application closes connections.
If java closes the socket then my problem is easy enough to solve because then I will just have to open a new socket every time I need to pass data making sure that I pass the IP address of the client to the server the first time I make a connection.
It doesn't.
My real questions come in when a socket stays open.
If the socket stays open and multiple clients connect to the server, will the server just shout over the network whenever it transmits a message so that all clients receive the message?
No. It will respond via the socket that is connected to the corresponding client.
(If this is the case then I know I can just attach a username to the front of the message so that the client can determine if the server is talking to it.)
Unnecessary.
If the server does not shout then how do I specify which client I want the server to talk to?
The server responds via the same socket it read the request from.
Will I have to add a loop to my receive methods so that the client/server is constantly listening for a transmission from the server/client
No, you will have to add a thread per accepted socket, that loops reading requests until end of stream.
or will java automatically do so after I run the method the first time?
No.
You seem to have been reading some truly appalling drivel. Take a look at the Custom Networking section of the Java Tutorial.
Adding to EJP's wise answer, it might be worth clarifying:
Sounds like you (wisely) use TCP, so your Socket represents a connection between 1 server and 1 client. No "shouting". In examples such as this , when connection is established (namely, client obtains a Socket by calling "new Socket" and server obtains a Socket by calling "accept"), those Sockets are dedicated to those 2 specific endpoints. So if 10 clients connect to 1 server, the server will keep 10 Sockets and won't mix them up. A bit like a poor secretary that has 10 phones on his desk and answers them all - despite the mess, each earpiece is clearly connected to 1 customer.
The connection can hold for a while & serve several messages. It will terminate when either one of the sides calls 'socket.close', or it can be terminated by underlying 3rd parties (operating system, proxies, firewalls).
For your first version, or for simple business requirements, it's probably enough to converse over this 1 simple connection. However, for commercial critical data that requires 'assurance of delivery', you might need to invest some careful thought & possibly tools such as RabbitMQ.
Good luck:)

Java Socket Programming single Client-server

I want to create a simple chat program. But my requirement is some what different I want a single client - server type of thing. Means I don't want a multiple client server.
I am creating a server and waiting for client to connect. After client connects I am waiting for any message from client.
Now my problem is that when I run my client program, I can run multiple instances of it, but the only first client messages are delivered to server as it is single server client program. What I want is some sort of method with which I can limit other clients to connect and say display error message saying that server is busy try after sometime. Can anyone help me do that?
The server should have a list of all client connected so whenever a new client connect; instead of just adding a new client; it check if there is already a user connected and if yes ; it close the connection/return special exception.It is up to the client project you made that decide how it will handle this closing of connection/exception.

How to share a socket single connection between two clients?

I want to know how to share a socket single connection between two clients. I have programmed in the following way, and need advise for the rest of the development.
Socket (Java desktop program)
|
|
|<---------------------->|
| |
iOS Mobile client Java Applet (Runs on desktop where same Socket is there)
Steps are,
1. Java Applet connect with this socket and gets a random number from this socket, in the very first call. It is developed.
[Socket program accepts client and creates a Thread and runs ]
2. I have a text field in iOS Mobile client where user will type that random number (shared the rand number via phone or chat) which it got from that Applet. It is developed.
3. iOS Mobile client sends this random number and also get it connected with socket. It is developed.
4. I need to share images from iOS client to Applet now via this socket established.
I want to know, how to make the 'Applet connection with socket' and 'iOS client with socket' to be in the same connection, so that I can share images from iOS app to Applet via this socket connection? Can a single socket connection be shared between two clients (iOS and Applet) like this?
Please advise me, how can i achieve the 4th point mentioned above with the same connection established between two clients.
Thank you!
There is no connection between the iOS client and the applet.
While you could try to establish a connection between the two, it's probably better for your server (the Java desktop program) to act as a proxy sending images received from the iOS client on one socket to the applet on the other socket.

Java SSLSocket won't stay alive (HTTPS Persistent connection needed)

I have written a Client-Server program using java Sockets and it works just fine. Now I'm trying to move on to HTTPS for communicating between client and server.
My code is based on java built-in Sockets (Socket and ServerSocket) and it works just fine. But when I use HTTPS Sockets (SSLSocket and SSLServerSocket) without changing the rest of the code, unfortunately won't work anymore.
The program needs persistent connection between server and client to let them send and receive data for minutes. The problem seems to be that HTTPS connection has been closed in server or client side after first transaction has been completed.
To be more precise, This is what happens:
1. Server creates a SSLServerSocket and calls "accept()" on it.
2. Client creates a SSLSocket and connects to the server. Then writes to server through "DataOutputStream"
3. Server reads the client through "DataInputStream" and sends back its response through "DataOutputStream".
4. Client reads the server through "DataInputStream".
Everything is OK till now! But after that when the client sends another stream, on the server side no data would be "available()" on the server through the same method used before.
Is there a way to keep this connection alive?
Tnx for ur helps in advance!
InputStream.available() isn't a reliable way of detecting socket input. You therefore have no evidence that the connection isn't alive. Just block in a read method. This implies a dedicated thread for reading per client.

How to open a socket in anticipation of an ensuing HttpURLConnection

I'm working on an Android application that requires very quick responses from our server. We communicate using HttpURLConnections. In an attempt to reduce latency, I'd like to "warm up" the connection by opening the socket when the user is actively using the application so that when we do get a request to send data, the connection will already be established (assuming the socket wasn't closed server side due to timeout). I believe that URL.openConnection() does not actually open this socket (despite its name) until data is actually sent. Is this correct? If so, is there a better way to simply open a socket and pass that to another HTTPURLConnection later without sending data and have that socket stay open for the keep-alive time?
i think this migth help you
https://github.com/Gottox/socket.io-java-client
its an IO Socket, mantains a conection with the server recieving data on the method "on", and you can send as well data trough "emit".

Categories