생성자
ServerSocket()
ServerSocket(int port)
ServerSocket(int port, int backlog)
ServerSocket(int port, int backlog, InetAddress bindAddr)
backlog
: 큐의 개수, 객체가 바빠서 연결 요청을 처리 못하고 대기 시킬 때 최대 대기 개수InetAddress
: 특정 주소에서만 접근이 가능하도록 지정생성자
Socket()
Socket(Proxy proxy)
Socket(SocketImpl impl)
Socket(InetAddress addr, int port)
Socket(InetAddress addr, int port, InetAddress localAddr, int localPort)
Socket(String host,int port)
Socket(String host, int port, InetAddress localAddr, int localPort)
public class PracticeSocket {
public void createServer(){
ServerSocket serverSocket = null;
Socket client = null;
try{
serverSocket = new ServerSocket(8888);
while (true){
System.out.println("-----------Server start----------");
client = new Socket("localhost", 8888);
System.out.println("-----------accepted.-----------");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String chat = "";
while (true){
if(chat == null) break;
chat = br.readLine();
System.out.println("text: " + chat);
}
if("bye".equalsIgnoreCase(chat)){
System.out.println("-----Finish Server-----");
break;
}
br.close();
client.close();
System.out.println("--------------------------------------------");
}
}catch(Exception e){
e.printStackTrace();
}finally {
if(serverSocket != null){
try {
serverSocket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}