forked from seattlewill/multi-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCPServer.java
More file actions
30 lines (23 loc) · 914 Bytes
/
TCPServer.java
File metadata and controls
30 lines (23 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package network;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws Exception {
//1. 연결 요청만 담당하는 서버용 socket필요
//prototype(프로토타입) 방법 <-> singleton(싱글톤)
//필요할때마다 객체 생성 <-> 하나만 객체 생성 주소를 재사용
ServerSocket server = new ServerSocket(9100);
System.out.println("TCP 서버 시작됨");
System.out.println("클라이언트의 요청을 기다리는 중..");
int count = 0;
while (true) {
Socket socket = server.accept();
System.out.println(count + "번 클라이언트와의 연결 성공.");
count++;
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("i am a java programmer!!!");
}
}
}