-
Notifications
You must be signed in to change notification settings - Fork 0
소켓 통신
Im Sejin edited this page Mar 14, 2019
·
4 revisions
package practice.network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerClass {
public static void main(String[] args) throws Exception{
ServerSocket ss = null;
Socket s = null;
PrintWriter pw = null;
BufferedReader br = null;
ss = new ServerSocket(9000);
System.out.println(" -- ready to be connected with client -- ");
s = ss.accept(); // 해당 포트로 소켓이 연결되기까지 계속 대기한다
System.out.println(" -- success to be connected with client -- ");
System.out.println("socket: " + s);
// 소켓 인스턴스에서 OutputStream 객체를 받아 PrintWriter 인스턴스를 생성한다
pw = new PrintWriter(s.getOutputStream());
// 소켓 인스턴스에서 InputStream 객체를 받아 BufferedReader 인스턴스를 생성한다
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while (true) {
String msg = br.readLine();
System.out.println("msg: " + msg);
if(msg.equals("stop")) {
System.out.println(" -- the client stops the dialogue -- ");
break;
}
pw.println(" -- success to send the message to client -- ");
pw.flush();
}
ss.close();
}
}