msmsms Posted October 11, 2014 Report Share Posted October 11, 2014 (edited) tenho este servidor em java mas no lugar de abrir um na consola do eclipse preciso que esse abra 2 que alteração devo fazer dentro deste para serem abertos 2 servidores com a mesma classe? // TCPServer2.java: Multithreaded server import java.net.*; import java.io.*; public class TCPServer{ public static void main(String args[]){ int numero=0; try{ int serverPort = 6000; // apenas serve para estabelecer ligação System.out.println("A Escuta no Porto 6000"); ServerSocket listenSocket = new ServerSocket(serverPort); System.out.println("LISTEN SOCKET="+listenSocket); while(true) { Socket clientSocket = listenSocket.accept(); // BLOQUEANTE System.out.println("CLIENT_SOCKET (created at accept())="+clientSocket); numero ++; new Connection(clientSocket, numero); } }catch(IOException e) {System.out.println("Listen:" + e.getMessage());} } } //= Thread para tratar de cada canal de comunicação com um cliente class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; int thread_number; public Connection (Socket aClientSocket, int numero) { thread_number = numero; try{ clientSocket = aClientSocket; in = new DataInputStream(clientSocket.getInputStream()); out = new DataOutputStream(clientSocket.getOutputStream()); this.start(); }catch(IOException e){System.out.println("Connection:" + e.getMessage());} } //============================= public void run(){ String resposta; try{ while(true){ //an echo server String data = in.readUTF(); System.out.println("T["+thread_number + "] Recebeu: "+data); resposta=data.toUpperCase(); out.writeUTF(resposta); } }catch(EOFException e){System.out.println("EOF:" + e); }catch(IOException e){System.out.println("IO:" + e);} } } Edited October 11, 2014 by msmsms Link to comment Share on other sites More sharing options...
sandokan.dias Posted January 30, 2015 Report Share Posted January 30, 2015 Amigo, você vai precisar subir 2 processos diferentes, vai precisar fazer bind em portas diferentes. Remova o hardcode da porta e passe como argumento para o seu main. Ex: java Connection 6000 java Connection 6001 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now