I am trying to connect two socket clients connected to the same remote server. The point is this:
- Client_1] connect to the server
- Client_2] connect to the server
- Server] creates a tunnel between Client_1 and Client_2
- Client_1] write "something"
- Client_2] (which is waiting for some messages) receives "something" from Client_1
and vice versa.
What is my code:
package jtestsock; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.List; public class Server extends Thread{ private List<Socket> clients; private ServerSocket server; private int port = 5001; private BufferedReader input; private PrintWriter output; public Server() { try { server = new ServerSocket(port); } catch (IOException e) { System.out.println("Impossibile istanziare il server: "+e.getMessage()); } } @Override public void run() { System.out.println("Waiting for client message...");
Can anybody help me? Thanks:)
Update:
clients.size ()> 0 should be> = 2, since you would like to have at least two sockets, not only one - boxed__l 6
Sorry, you're right ... thanks!
Update:
Do you plan to have only 2 clients? Because one thing you can do is when one client sends information to the server, you can transfer this information to another client. If you plan to have multiple clients, I would suggest assigning each client a UID. - Josh M 10 minutes ago
Yes, I would like to make a Point to Point connection between two clients , going to my server, for example a proxy server ...
The server must accept more than two connections and make two threads on the server for writing and reading, which redirect messages from one client to the other two, the processor will be saturated.
(Ps. Sorry for my English XD)
source share