I developed a simple, multi-threaded proxy server. Note that this program is a proxy for one specific server. Here is my code :
public class Proxy {
int remotePort;
InetAddress remoteHost;
public Proxy(InetAddress remoteHost, int remotePort) {
this.remotePort = remotePort;
this.remoteHost = remoteHost;
}
public void go() {
ExecutorService pool = Executors.newCachedThreadPool();
try (ServerSocket server = new ServerSocket(2015);) {
System.out.println("Proxy is running....!");
while (true) {
Socket socketClient = server.accept();
System.out.println("client connected...!");
Socket socketServer = new Socket(remoteHost, remotePort);
System.out.println("connection established with the server...!");
Worker p1 = new Worker(socketClient, socketServer);
Worker p2 = new Worker(socketServer, socketClient);
pool.execute(p1);
pool.execute(p2);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
class Worker implements Runnable {
Socket from, to;
public Worker(Socket from, Socket to) {
this.from = from;
this.to = to;
}
#Override
public void run() {
System.out.println(Thread.currentThread().getName());
try (BufferedInputStream bis = new BufferedInputStream(from.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(to.getOutputStream());) {
while (true) {
int octet = bis.read();
if (octet == -1) {
break;
}
bos.write(octet);
}
bos.flush();
from.close();
to.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public static void main(String[] args) {
InetAddress adr = null;
try {
adr = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
System.out.println(ex.getMessage());
}
new Proxy(adr, 2020).go();
}
}
I would like to test this proxy server with a client and a server. the client sends an array of integers to find the maximum. the server of his turn, returns the maximum of a received array.
the problem is the following: the proxy server is not playing its role. the array is not sent to the server and the client not received the maximum value of its array.
The code of my Server is as follow:
public class ServerTCPMax {
static void display(int[] tab) {
for (int u : tab) {
System.out.print(u + " ");
}
System.out.println();
}
static int searchMax(int[] t) {
int max = t[0];
for (int i = 1; i < t.length; i++) {
if (t[i] > max) {
max = t[i];
}
}
return max;
}
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(2020);
System.out.println("server is running .......! ");
while (true) {
try (Socket sclient = server.accept()) {
System.out.println("Proxy connected...!");
ObjectInputStream ois = new ObjectInputStream(sclient.getInputStream());
int[] tab = (int[]) ois.readObject();
display(tab);
int max = searchMax(tab);
try (DataOutputStream dos = new DataOutputStream(sclient.getOutputStream())) {
dos.writeInt(max);
dos.flush();
}
}
}
} catch (IOException | ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}
The following code represents my client:
public class ClientTCPMax {
public static void main(String[] args) {
int[] A = {3, -7, 9, 22, 0, 7, 11, 2};
try {
try (Socket socket = new Socket("127.0.0.1", 2015)) {
System.out.println("connection established with the Proxy ...!");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(A);
oos.flush();
DataInputStream dis = new DataInputStream(socket.getInputStream());
int max = dis.readInt();
System.out.println(" the max is : " + max);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
Related
I have a Java program with a client and server sockets, there I want to test that an exception is raised after the server is down.
This is the server:
public class SocketServer implements Runnable {
private bool serverRuns = false;
private int timeout = 10000;
private DataInputStream in;
private DataOutputStream out;
private Socket client;
private ServerSocket server;
private String message = "Ok";
private waitForInstruction = true;
public SocketServer() throws IOException {
ServerSocket server = new ServerSocket(tcpPort,0,ipAdress);
serverRuns = true;
server.setSoTimeout(timeout)
}
private void waitForClient() {
try {
client = server.accept();
client.setSoTimeout(timeout);
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
fail("I/O Error " + e.getMessage());
}
}
public void run() {
waitForClient();
while (serverRuns) {
if (clientSocket.isClosed()) {
waitForClient();
}
try {
while (waitForInstruction == false) {
// Read input message
String inputStreamString = "";
while (in.available() > 0) {
int c = in.read();
inputStreamString += (char) c;
}
out.write(message.getBytes());
System.out.println("Sent bytes: " + out.size());
setWaitForInstruction(true);
}
} catch (IOException E) {
fail("I/O Error " + E.getMessage());
}
}
}
public void closeServerSocket() {
try {
serverRuns = false;
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void setWaitForInstruction(boolean waitForInstruction) {
this.waitForInstruction = waitForInstruction;
}
public void startServerSocket() {
serverRuns = true;
}
}
This is the client:
public class SocketClient extends Socket {
private InetAddress address;
private short tcpPort;
private DataInputStream in;
private DataOutputStream out;
private static final int timeOut = 10000;
public SocketClient(InetAddress address, short tcpPort) {
this.tcpPort = tcpPort;
this.address = address;
}
public void connect() throws IOException, SocketTimeoutException {
super.connect(new InetSocketAddress(address, tcpPort), timeOut);
}
public void doStuff() throws IOException {
String request = "Ok?";
String InputStreamString = "";
super.setSoTimeout(timeOut);
this.setSoTimeout(timeOut);
out = new DataOutputStream(super.getOutputStream());
in = new DataInputStream(super.getInputStream());
out.writeBytes(requestString);
int c;
do {
if (in.available() > 0) {
c = in.read();
InputStreamString += (char) c;
}
} while (!InputStreamString.equals("Ok"));
System.out.println(InputStreamString);
}
}
I start the server socket thread with:
#BeforeClass
public static void startSocket() throws IOException {
testServer = new SocketServer();
monitor = new Thread(testServer);
monitor.setName("Test Server Thread");
monitor.setDaemon(true);
monitor.start();
}
And the JUnit test is this:
#Before
public void createSocket() throws Exception {
ipAdress = InetAddress.getLocalHost();
SystemConfig.setLoggerConfigFilePath("LoggerConfig.xml");
socketClient = new SocketClient(ipAdress, 5000);
}
#Test
public void checkServerisDown() {
try {
socketClient.connect();
} catch (IOException e) {
fail("IO Error");
}
testServer.closeServerSocket();
monitor.interrupt();
try {
testServer = new SocketServer();
monitor = new Thread(testServer);
monitor.setName(" Test Server Thread");
monitor.setDaemon(true);
// monitor.start();
testServer.startServerSocket();
testServer.setWaitForInstruction(false);
System.out.print("Test (1/1) CHECK SERVER IS DOWN.....\n");
socketClient.doStuff();
System.out.println("NOT OK!");
} catch (IOException e) {
fail("IO Error " + e.getMessage() + " OK");
}
try {
drEstimControlInterface.close();
testServer.getSocket().close();
} catch (IOException e) {
fail("IO Error");
}
}
#AfterClass
public static void closeSocket() throws IOException {
testServer.closeSocket();
}
However the test is not performing as I intended, I thought that this should return an IOException since the server socket has been closed and the thread has been interrupted, but the client socket still gets the answer from the server socket and prints the "NOT OK!". Could anybody tell me why?
You have to close not only ServerSocket but client socket with streams too.
public void closeServerSocket() {
try {
serverRuns = false;
serverSocket.close();
in.close();
out.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I have to write a socket program to communicate with a server on a given port and DNS. The communication can be a single message or a list of messages; against every message a response is generated from the server. Once a connection is made I receive a response for the first message, but on the next message receive a response of -1. The following is the class handling client server communication:
public class SocketCommunication {
static String[] adresses = null;
final static int port = 1234;
final static int timeout = 60000;
static long pbmId;
private static int count = 0;
private static void loadPBMDNS()
{
DynamicQuery pbmQuery = PH_PBM_SwitchLocalServiceUtil.dynamicQuery();
pbmQuery.add(RestrictionsFactoryUtil.eq("Activated", true));
try
{
List<PH_PBM_Switch> pbmList = PH_PBM_SwitchLocalServiceUtil.dynamicQuery(pbmQuery);
if(pbmList != null && pbmList.size() > 0)
{
if(pbmList.get(0).getServer_DNS() != null
&& !pbmList.get(0).getServer_DNS().equals(""))
{
pbmId = pbmList.get(0).getPBM_Switch_Id();
if(pbmList.get(0).getServer_DNS().contains(";"))
{
String[] tokens = pbmList.get(0).getServer_DNS().split(";");
System.out.println(tokens.toString());
adresses = tokens;
}
else
{
adresses[0] = pbmList.get(0).getServer_DNS();
}
}
}
}
catch (SystemException e)
{
e.printStackTrace();
}
}
public static ArrayList<BatchClaimVO> ConnectSendAndReadResponse
(int addressNumber, ArrayList<BatchClaimVO> batchClaimVOs)
{
try
{
loadPBMDNS();
System.out.println("Connecting to " + adresses[addressNumber] + " on port " + port);
SocketFactory sslsocketfactory = SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket();
sslsocket.connect(new InetSocketAddress(adresses[addressNumber], port), timeout);
System.out.println("Just connected to " + sslsocket.getRemoteSocketAddress());
for (int i = count; i < batchClaimVOs.size(); i++)
{
sendClaim(
sslsocket,
batchClaimVOs.get(i).getCb(),
batchClaimVOs.get(i).getUniqueIdentifier()
);
if(addressNumber <= 2)
{
batchClaimVOs.get(i).setResponse
(readResponse(sslsocket, addressNumber, batchClaimVOs.get(i).getCb()));
}
}
System.out.println("Closing socket");
count = 0;
sslsocket.close();
return batchClaimVOs;
}
catch (IOException e)
{
if(addressNumber < 2)
{
System.out.println("connection timedout trying again on new DNS");
ConnectSendAndReadResponse(++addressNumber, batchClaimVOs);
}
else
{
System.out.println
("unable to connect to server or server did not responed intime");
e.printStackTrace();
}
}
return null;
}
public static void sendClaim(SSLSocket sslSocket, ClaimBuilder cb, long uniqueIdentifier)
throws IOException
{
System.out.println("sending claim");
OutputStream outToServer = sslSocket.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeBytes(cb.getClaim());
System.out.println("claim sent");
SaveRequestLog(uniqueIdentifier, cb.getClaim(), pbmId);
}
public static void SaveRequestLog(long uniqueIdentifier, String claim, long pbmId)
{
if(uniqueIdentifier > 0)
{
try
{
PH_Request_Transaction_Log log = PH_Request_Transaction_LogLocalServiceUtil.getPH_Request_Transaction_Log(uniqueIdentifier);
log.setPBM_Switch_Id(pbmId);
log.setRequest_Log(claim);
log.setCreated_By(LiferayFacesContext.getInstance().getUserId());
log.setCreated_Date(Calendar.getInstance().getTime());
PH_Request_Transaction_LogLocalServiceUtil.updatePH_Request_Transaction_Log(log);
}
catch (PortalException e)
{
e.printStackTrace();
}
catch (SystemException e)
{
e.printStackTrace();
}
}
}
public static String readResponse(SSLSocket sslSocket, int addressNumber, ClaimBuilder cb)
throws IOException
{
sslSocket.setSoTimeout(timeout);
InputStream inFromServer = sslSocket.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
byte[] data = new byte[1048];
int count = in.read(data);
System.out.println(count);
System.out.println(new String(data));
String response = fixString(new String(data), count);
System.out.println("Verifying checksum");
if(verifyTransmissionCheckSum(response))
{
System.out.println("checksum verified");
System.out.println(response);
}
else
{
System.out.println("transmission corrupted");
}
sendAcknowledgement(sslSocket, cb);
return response;
}
public static void sendAcknowledgement(SSLSocket sslSocket, ClaimBuilder cb)
throws IOException
{
System.out.println("sending Acknowledgement");
OutputStream outToServer = sslSocket.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeBytes(cb.buildClaimAcknowledgement());
System.out.println("Acknowledgement Sent");
count++;
}
public static String fixString(String toFix, int count)
{
return toFix.substring(0, count);
}
public static boolean verifyTransmissionCheckSum(String str)
{
return (Integer.parseInt((String) str.subSequence(0, 4))
== (str.subSequence(4, str.length())).length())
? true : false;
}
}
The given server do not support batch communication.
Hello it is my first topic here.
I wrote some code to making connection client-server by sockets. As a client I mean opening terminal window and connection to this server by telnet. Application works as a server listening for requests from client on specified port. Every single connection is treat as a new thread. Once the connection is bound the client can send a message and gets an answer back as an echo.
The problem is I want to be informed when the client closes the terminal window (finish thread). I want to know id of this closed thread.
This is full code:
public class MultithreadEchoServer
{
public static void main(String[] args) throws IOException
{
int count=0;
long[] ids = new long[10];
try (ServerSocket ss = new ServerSocket(1234))
{
System.out.println("Listening...");
while(true)
{
count++;
Socket s = ss.accept();
Runnable r = new MyThread(s, count);
Thread t = new Thread(r);
t.start();
ids[count-1] = t.getId();
if(Thread.activeCount() != count+1)
{
count = Thread.activeCount();
System.out.println("Client "+/*??thread's id??*/" left the session. Now connected: "+count);
}
System.out.println("Clients connected: "+count);
System.out.println(ids[count-1]);
}
}
}
}
class MyThread implements Runnable
{
private Socket s;
private int count;
private long id;
InputStream io;
OutputStream os;
private PrintWriter pw;
MyThread(Socket s, int count)
{
this.s = s;
this.count = count;
}
public long getId()
{
id = Thread.currentThread().getId();
return id;
}
#Override
public void run()
{
try
{
io = s.getInputStream();
os = s.getOutputStream();
}
catch (IOException e) {System.err.println(e);}
try (Scanner sc = new Scanner(io))
{
pw = new PrintWriter(os, true);
pw.println("Connected");
while(sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println("Client "+count+": "+line);
pw.println("Echo: "+line);
}
}
}
}
Why you donn't print Thread ID inside Thread execution with a finally statement after your try with resource statement :
try (Scanner sc = new Scanner(io))
{
pw = new PrintWriter(os, true);
pw.println("Connected");
while(sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println("Client "+count+": "+line);
pw.println("Echo: "+line);
}
}
finally {
System.out.println("Client " + this.id + " left the session."");
}
MultithreadEchoServer
public class MultithreadEchoServer
{
public static void main(String[] args) throws IOException
{
int count=0;
long[] ids = new long[10];
try (ServerSocket ss = new ServerSocket(1234))
{
System.out.println("Listening...");
while(true)
{
count++;
Socket s = ss.accept();
Runnable r = new MyThread(s, count);
Thread t = new Thread(r);
t.start();
ids[count-1] = t.getId();
if(Thread.activeCount() != count+1)
{
count = Thread.activeCount();
System.out.println("Now connected: "+count);
}
System.out.println("Clients connected: "+count);
System.out.println(ids[count-1]);
}
}
}
}
MyThread
class MyThread implements Runnable
{
private Socket s;
private int count;
private long id;
InputStream io;
OutputStream os;
private PrintWriter pw;
MyThread(Socket s, int count)
{
this.s = s;
this.count = count;
}
public long getId()
{
id = Thread.currentThread().getId();
return id;
}
#Override
public void run()
{
try
{
io = s.getInputStream();
os = s.getOutputStream();
}
catch (IOException e) {System.err.println(e);}
try (Scanner sc = new Scanner(io))
{
pw = new PrintWriter(os, true);
pw.println("Connected");
while(sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println("Client "+count+": "+line);
pw.println("Echo: "+line);
}
}
finally {
System.out.println("Client " + this.id + " left the session."");
}
}
}
I've got a working simple client-server app. The problem is it works fine just with one started client, but not with two or more. It establish connection, but when you try to enter text in first or second, the server breakes. I think that problem may be at the function broadcast() in Server.java.
Server.java
public class Server {
final int PORT = 5000;
private ArrayList<NewClient> al = new ArrayList<NewClient>();
private Scanner in;
private PrintWriter out;
private SimpleDateFormat sdf;
private int uniqueID = 0;
private void go(){
try{
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Waiting for clients...");
while(true) {
Socket s = serverSocket.accept();
NewClient chat = new NewClient(s);
System.out.println("Client number " + chat.getId() + " connected from: " + s.getLocalAddress().getHostName());
al.add(chat);
Thread t = new Thread(chat);
t.start();
}
}catch (Exception e) {
System.out.println("Problem with establishing network connection: ");
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.go();
}
class NewClient implements Runnable{
private Socket socket;
private int id;
public NewClient(Socket s) {
this.socket = s;
this.id = ++uniqueID;
}
public int getId() {
return this.id;
}
#Override
public void run() {
try{
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream());
sdf = new SimpleDateFormat("HH:mm:ss");
while(true) {
String input = in.nextLine();
System.out.println("Client said: " + input);
broadcast(input);
}
}catch (Exception e) {
e.printStackTrace();
}
}
private void writeMsg(String input) {
String msg = input + " on " + sdf.format(new Date());
out.println("You said: " + msg);
out.flush();
}
private void broadcast(String input) {
for (int i = 0; i < al.size(); i++) {
NewClient t = al.get(i);
t.writeMsg(input);
}
}
}
}
Client.java:
public class Client {
final int PORT = 5000;
final String HOST = "127.0.0.1";
private Scanner stdIn;
private Scanner in;
private PrintWriter out;
private void go() {
setUpNetwork();
}
private void setUpNetwork(){
try{
Socket s = new Socket(HOST, PORT);
System.out.println("You are connected to " + HOST);
NewClient client = new NewClient(s);
Thread t = new Thread(client);
t.start();
} catch (Exception e) {
System.out.println("Problem with connection to server: " + e);
}
}
public static void main(String[] args) {
Client client = new Client();
client.go();
}
class NewClient implements Runnable {
private Socket socket;
public NewClient(Socket s) {
this.socket = s;
}
#Override
public void run() {
try {
stdIn = new Scanner(System.in);
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream());
while(true) {
System.out.print("> ");
String input = stdIn.nextLine();
out.println(input);
out.flush();
if(in.hasNext()) {
System.out.println(in.nextLine());
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
When opens two Client.java and connect it to the server.java everything is ok. But when i try to send some message from this two opened clients server returns these errors:
java.lang.IndexOutOfBoundsException: end
at java.util.regex.Matcher.region(Matcher.java:1038)
at java.util.Scanner.findPatternInBuffer(Scanner.java:1010)
Client said: sds
at java.util.Scanner.findWithinHorizon(Scanner.java:1679)
at java.util.Scanner.nextLine(Scanner.java:1538)
at Server$NewClient.run(Server.java:66)
at java.lang.Thread.run(Thread.java:745)
What's happening is that your code scans the first line from the client ("sds", which is printed to stdout), and then it loops back and immediately tries to scan the next line from the client. Since the client hasn't sent anything more yet, the input stream scanner throws an exception.
The NewClient class is an inner class. All instances of this class are created with the same instance of the outer class. Changing the code will sove the problem:
public NewClient(Socket s) {
this.socket = s;
this.id = ++uniqueID;
try{
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream());
sdf = new SimpleDateFormat("HH:mm:ss");
}catch(Exception e) {
System.out.println("Exception while creating input/output streams: " + e);
}
}
Ok so I have constructed a working example of a client and server which can accept multiple client connections. My problem is is that I cannot connect a client which is not running the same internet connection as the one the server is being hosted on. Is this possible using server sockets?
Here is the code for my server:
import java.io.IOException;
import java.net.*;
public class MultipleSocketServer {
public static Socket connection;
public static String name = "Tyler's Server";
public static int limit = 2;
public static Thread[] clients = new Thread[limit];
public static int current = 0;
public static int port = 25565;
public static String[] connected = new String[limit];
public static ServerSocket socket;
public static void main(String[] args) {
System.out.println("Server starting...");
for(int i = 0; i < limit; i++) {
connected[i] = "";
}
try {
ServerSocket socket = new ServerSocket(port);
while(true) {
Socket connection = socket.accept();
String ip = connection.getRemoteSocketAddress().toString().substring(1, 13);
loop:
for(int i = 0; i < connected.length; i++) {
if(connected[0].equals(ip) || connected[1].equals(ip)) {
break loop;
}else if(!connected[i].equals(ip)) {
connected[i] = ip;
MultiServer_Client client = new MultiServer_Client(connection, i);
Thread run = new Thread(client);
run.start();
break loop;
}
}
}
} catch (IOException e1) {
System.out.println("Could not bind server on: " + port);
System.exit(-1);
}
}
}
And here is the rest:
import java.io.*;
import java.net.Socket;
public class MultiServer_Client implements Runnable {
public String time;
public Socket client;
public StringBuffer process = new StringBuffer();
public BufferedInputStream inputStream;
public InputStreamReader reader;
public BufferedOutputStream outputStream;
public OutputStreamWriter writer;
public StringVis check = new StringVis("");
public StringChangeListener checkListener = new StringChangeListener() {
public void textChanged(StringChangeEvent e) {
System.out.println("Changed");
write("Server recieved message...");
}
};
public boolean connected = true;
public int ID;
public MultiServer_Client(Socket connection, int i) {
client = connection;
ID = i;
try {
//declare text input/output
inputStream = new BufferedInputStream(client.getInputStream());
reader = new InputStreamReader(inputStream);
outputStream = new BufferedOutputStream(client.getOutputStream());
writer = new OutputStreamWriter(outputStream, "US-ASCII");
} catch (IOException e) {
System.out.println("IOException: " + e);
}
System.out.println(MultipleSocketServer.connected[ID] + " connected...");
write("Connected to " + MultipleSocketServer.name);
}
public void run() {
while(connected) {
read();
}
System.out.println("Disconnecting client...");
}
public void write(String authen) {
try {
time = new java.util.Date().toString();
String message = time + ": " + authen + (char) 13;
writer.write(message);
writer.flush();
} catch (IOException e) {
connected = false;
MultipleSocketServer.connected[ID] = "";
}
}
public void read() {
//read from client
int character;
process = new StringBuffer();
try {
while ((character = reader.read()) != 13) {
process.append((char) character);
}
check.setText(process.toString());
process.delete(0, process.length());
} catch (IOException e) {
connected = false;
MultipleSocketServer.connected[ID] = "";
}
}
}
Here's the client code:
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class SocketClient {
public static String host = "69.182.134.79";
public static int port = 25565;
public static void main(String [] args) {
StringBuffer imports = new StringBuffer();
String time;
System.out.println("Client starting...");
try {
//establish client
InetAddress address = InetAddress.getByName(host);
Socket connection = new Socket(address, port);
BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
while(true) {
Scanner scan = new Scanner(System.in);
String message = scan.nextLine();
//write to server
time = new java.util.Date().toString();
String process = host + ":" + port + " sent data at " + time + ": " + message + (char) 13;
osw.write(process);
osw.flush();
//read from server
int c;
while ((c = isr.read()) != 13) {
imports.append((char) c);
}
System.out.println(imports);
imports.replace(0, imports.length(), "");
if(message.equals("--EXIT")) {
connection.close();
}
}
} catch (UnknownHostException e) {
System.out.println("UnknownHostException: " + e);
} catch (IOException e) {
System.out.println("IOExcepion: " + e);
}
}
}
Change
MultiServer_Client client = new MultiServer_Client(connection, i);
to
MultiServer_Client client = new MultiServer_Client(new Socket([Server IP], port), i);
This should work.