-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
51 lines (44 loc) · 1.12 KB
/
Server.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.verysu.test;
import java.net.ServerSocket;
import java.net.Socket;
/**
* TCP服务
* @author sujunguang
* 2016年9月29日
* 下午9:05:45
*/
public class Server {
/**
* @param args
*/
public static void main(String[] args) {
int port;
ServerSocket serverSocket;
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
System.out.println("port = 8080 (默认)");
port = 8080;
}
try {
// 建立端口监听
serverSocket = new ServerSocket(port);
System.out.println("服务器正在监听端口: " + serverSocket.getLocalPort());
while (true) {
try {
// 等待客户的TCP连接请求
final Socket socket = serverSocket.accept();
System.out.println("建立一个客户端的新的TCP连接,该客户的地址为:" + socket.getInetAddress() + ":" + socket.getPort());
service(socket);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/* 处理HTTP请求 */
public static void service(Socket socket) throws Exception {
}
}