How to share a socket single connection between two clients? - java

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.

Related

How do I access a ServerSocket behind an NAT router?

Goal
I'm making a chat application for android and am currently testing with 2 phones which must eventually work for a few thousand users.
Problem
I get a ConnectionException saying "Connection refused" whenever the 2 phones try connecting to each other via sockets.
Current Design
Each phone starts a ServerSocket, calls the accept() method waiting for some Socket to connect to, and whichever phone sends a message first will create a client Socket. I'm certain the IP addresses I'm using are correct (they're actually both using the same external IP).
I believe the problem is with the ports. I generate a port number at random, and if it's free to use, I say ServerSocket s = new ServerSocket( randomPortNumber ).
What I think is the source of the problem
What I think is the problem is this port number is one sitting behind an NAT router. So when a Socket tries to connect to the ServerSocket using something like Socket socket = new Socket( ip, serverSocketRandomPortNumber ), it will try to connect to the NAT router and feed it this port number which won't work since the router itself is not listening on this port, but the phone behind the router is.
Questions and thoughts
My question is, how do I deal with this problem?
Do I have to change my design?
If I must, an alternative design I'm thinking of is using a single ServerSocket on a web host and use it to redirect messages sent from client sockets to other client sockets.
I'd be implementing the server-side in php referencing something along these lines:
http://php.net/manual/en/sockets.examples.php
And I would still use Java for the client-side.
Since one of the phones is behind a NAT router, nothing can initiate a connection to it unless port forwarding (or some other techniques) is enabled on the router.
The usual way a chat application is implemented is, there is a common server that all clients will connect to.
You don't have to write your own chat server (unless you really want to). I suggest using the XMPP protocol. A list of already made servers here. On the client side (Android), you can find libraries you can use here.

Transferring data using java socket

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

Connecting two separate sockets through serversocket

I'm developing a chat application in Java that enables client to connect to a predefined port and when two clients are connected the server should connect those two sockets and data should be exchanged between them.
I know to create a ServerSocket and Socket that will connect and establish a communication between them through a separate port and a server socket. But, how can I connect those two connected clients?
If you are creating a chat application then Chat server will not connect client socket in order to communicate.
You can try following approach:
On server side Use a HashTable to store connected clients.
When you receive a message from client, include a an id of CLient to whom message is to be sent and simply pass on the message to other client.
There will be separate threads running for these clients, so you need to have a policy to control the load on server.
This questions sounds like a homework assignment.
If you would like to see a great contemporary demo of a chat system you may learn from Play's WebSocket chat sample application.

Multiple java socket connections

I have created this game bot where it connects to the game, and starts playing.
My problem is that i can't start more than one of these as the other then won't work.
Is is possible that if i run 2 instances of the same program the sockets are interfering with each other ? After all, they do connect to the same IP with the same port ?
And sometimes after i close(just closing cmd) the program is unable to connect again. Is that cause i didn't close the connections right ?.
I hope this is enough else i'll just have to post my source code
Best regards.
It's possible to connect to the same socket/port several times. Actually a socket is a double peer: {client ip/ client port}{server ip/server port}. When you connect to a server, your client port is assigned dynamically. You will have a new and different client port per client. So it should work unless the server side forbid it.
You should have a server that listens for multiple connections. A server is bound to a port and once that port is in use another application cannot use it. So for the server just have one instance. Multiple clients can connect to this IP/Port as long as the Server accepts multiple connections.
If a client connects to the server and the other clients stop working this may be because the server does not support multiple clients. To do this you need to use multi threading in the server. The server should accept a client socket and create a new instance of a client with it's own StreamReader/Writer objects.
http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html
if you are working with a specific TCP port, then there is a close-wait period that this port cannot be claimed temporariliy for some time. also multiple programs cannot listen the same TCP port. Use threads.

Java tcp socket server with j2me client socket

I want to create java server socket and j2me client socket which can communicate with each other.
Does anyone know how to do this?
Are you trying to share data between two MIDlets running on one mobile phone?
Are you trying to exchange data between a mobile phone and a remote server?
Have you read the documentation for the javax.microedition.io package?

Categories