New paste Repaste Download
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main (String args[]) {
try{
int serverPort1 = 7896;
int serverPort2 = 7897;
int serverPort3 = 7898;
ServerSocket listenSocket1 = new ServerSocket(serverPort1);
ServerSocket listenSocket2 = new ServerSocket(serverPort2);
ServerSocket listenSocket 3= new ServerSocket(serverPort3);
while(true) {
Socket clientSocket1 = listenSocket1.accept();
Connection c1 = new Connection(clientSocket1);
Socket clientSocket2 = listenSocket2.accept();
Connection c2 = new Connection(clientSocket2);
Socket clientSocket3 = listenSocket1.accept();
Connection c3 = new Connection(clientSocket3);
}
} catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());}
}
}
class Connection extends Thread {
DataInputStream in1;
DataOutputStream out1;
DataInputStream in2;
DataOutputStream out2;
DataInputStream in3;
DataOutputStream out3;
Socket clientSocket1;
Socket clientSocket2;
Socket clientSocket3;
public Connection (Socket ClientSocket1,Socket ClientSocket2,Socket ClientSocket3) {
try {
clientSocket1 = ClientSocket1;
clientSocket2 = ClientSocket2;
clientSocket3 = ClientSocket3;
in1 = new DataInputStream( clientSocket1.getInputStream());
out1 =new DataOutputStream( clientSocket1.getOutputStream());
in2 = new DataInputStream( clientSocket2.getInputStream());
out2 =new DataOutputStream( clientSocket2.getOutputStream());
in3 = new DataInputStream( clientSocket3.getInputStream());
out3 =new DataOutputStream( clientSocket3.getOutputStream());
this.start();
} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
}
public void run(){
try {
System.out.println("[Servidor] Servidor central iniciado");
boolean ocupado=false;
while(true) {
String data1 = in1.readUTF();
if(data1=="DAME_TESTIGO") {
ocupado=true;
System.out.println("[Servidor] Testigo concedido a Cliente 1\n");
Thread.sleep(3000);
}
ocupado=false;
String data2 = in2.readUTF();
else if(data2=="DAME_TESTIGO" && ocupado= false) {
ocupado= true;
System.out.println("[Servidor] Testigo concedido a Cliente 2\n");
Thread.sleep(4000);
}
String data3 = in3.readUTF();
else(data3=="DAME_TESTIGO") {
ocupado=true;
System.out.println("[Servidor] Testigo concedido a Cliente 3\n");
Thread.sleep(5000);
}
out1.writeUTF("ok");
out2.writeUTF("ok");
out3.writeUTF("ok");
}
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
} catch(IOException e) {System.out.println("readline:"+e.getMessage());
} finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}
}
}
Filename: None. Size: 3kb. View raw, , hex, or download this file.

This paste expires on 2026-05-04 11:39:35.885853+00:00. Pasted through web.