?
?
?
?
package com.itheima.d7_socket3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerReaderThread extends Thread{
private Socket socket;
public ServerReaderThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
//3.从socket通信管道中得到一个字节输入流
InputStream is = socket.getInputStream();
//4.把字节输入流包装成缓冲字符输入流进行消息的接收
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//5.按照行读取消息
String msg;
while ((msg = br.readLine()) != null){
System.out.println(socket.getRemoteSocketAddress() + "说了:" + msg);
}
} catch (Exception e) {
System.out.println(socket.getRemoteSocketAddress() + "下线了!!!!");
}
}
}
package com.itheima.d7_socket3;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 目标:实现服务端可以同时处理多个客户端的消息。
*/
public class ServeDemo2 {
public static void main(String[] args) {
try {
System.out.println("======服务端启动成功=========");
//1.注册端口
ServerSocket serverSocket = new ServerSocket(7777);
//a.定义一个死循环由主线程负责不断的接收客户端的Socket管道连接。
while(true){
//2.每接收到一个客户端的Socket管道,交给一个独立的子线程负责读取消息
Socket socket = serverSocket.accept();
System.out.println(socket.getRemoteSocketAddress() + "他来了,上线了!");
//3.开始创建独立线程处理socket
new ServerReaderThread(socket).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.itheima.d7_socket3;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
/**
* 目标:实现多发多收
*/
public class ClientDemo1 {
public static void main(String[] args) {
try {
System.out.println("====客户端启动====");
//1.创建Socket通信管道请求有服务端的连接
//public Socket(String host, int port)
//参数一:服务端的IP地址
//参数二:服务端的端口
Socket socket = new Socket("127.0.0.1",7777);
//2.从socket通信管道中得到一个字节输出流 负责发送数据
OutputStream os = socket.getOutputStream();
//3.把低级的字节流包装成打印流
PrintStream ps = new PrintStream(os);
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("请说:");
String msg = sc.nextLine();
//4.发送消息
ps.println(msg);
ps.flush();
}
//关闭资源。
// socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
?
?
?
?
|