Jump to content

como duplicar este servidor?


msmsms
 Share

Recommended Posts

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 by msmsms
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.