-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPTest.java
More file actions
43 lines (35 loc) · 1.14 KB
/
Copy pathTCPTest.java
File metadata and controls
43 lines (35 loc) · 1.14 KB
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
package NetworkProgramming;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(3000, 1000, InetAddress.getByName("127.0.0.1"));
while (true) {
Socket s = ss.accept();
//把Socket的输出流包装成PrintStream
PrintStream ps = new PrintStream(s.getOutputStream());
//IO操作
ps.println("您好,您收到了服务器的新年祝福");
ps.close();
s.close();
}
}
}
class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 3000);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = br.readLine();
System.out.println("服务器消息:"+line);
br.close();
socket.close();
}
}
public class TCPTest {
public static void main(String[] args) {
}
}