forked from xftxyz2001/atguigu-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatClientTest.java
89 lines (74 loc) · 1.76 KB
/
ChatClientTest.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//package com.atguigu02.tcpudp.chat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
/**
* 案例:聊天室的实现 (客户端)
*
* @author 尚硅谷-宋红康
* @create 16:42
*/
public class ChatClientTest {
public static void main(String[] args)throws Exception {
//1、连接服务器
Socket socket = new Socket("127.0.0.1",8989);
//2、开启两个线程
//(1)一个线程负责看别人聊,即接收服务器转发的消息
Receive receive = new Receive(socket);
receive.start();
//(2)一个线程负责发送自己的话
Send send = new Send(socket);
send.start();
send.join();//等我发送线程结束了,才结束整个程序
socket.close();
}
}
class Send extends Thread{
private Socket socket;
public Send(Socket socket) {
super();
this.socket = socket;
}
public void run(){
try {
Scanner input = new Scanner(System.in);
OutputStream outputStream = socket.getOutputStream();
//按行打印
PrintStream ps = new PrintStream(outputStream);
//从键盘不断的输入自己的话,给服务器发送,由服务器给其他人转发
while(true){
System.out.print("自己的话:");
String str = input.nextLine(); //阻塞式的方法
if("bye".equals(str)){
break;
}
ps.println(str);
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Receive extends Thread{
private Socket socket;
public Receive(Socket socket) {
super();
this.socket = socket;
}
public void run(){
try {
InputStream inputStream = socket.getInputStream();
Scanner input = new Scanner(inputStream);
while(input.hasNextLine()){
String line = input.nextLine();
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}