4 대의 서버가 실행되고 하나의 클라이언트가 이러한 서버에 패키지를 보내는 응용 프로그램을 빌드해야하지만이 서버는 클라이언트 (보낸 사람)로부터 무언가를 받고 계속 실행해야합니다. 그래서 나는 두 클래스, 클라이언트와 서버 생성 :소켓으로 서버와 클라이언트를 만드는 방법은 무엇입니까?
public class Server {
public Event receive(int port) {
Evento event = null;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
evento = (Evento) ois.readObject();
is.close();
s.close();
ss.close();
}catch(Exception e){
System.out.println(e);
}
return event;
}
}
public class Client {
public void send(Event event, int port) {
try {
Socket s = new Socket("localhost", 2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(event);
oos.close();
os.close();
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
을하지만 말했듯이 내가 한 번 테스트하면, 나는 그 괜찮아,하지만 두 번 이상,이 서버는 모든 시간을 실행 계속 필요하지 않습니다 . 어떻게 할 수 있습니까?
당신은 잠시와 스레드를 의미? –
차단 연결을 읽는 스레드입니다. while 루프를 포함 할 수 있습니다. –