客户端
package TCP3;
//完成TCP通信的 多发多收
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args)throws Exception {
//1:创建Socket对象并同时请求服务端程序的链接
Socket socket= new Socket("127.0.0.1",8888);
//2:从Socket通信管道中得到一个字节输出流 用来数据发给到服务器
OutputStream os= socket.getOutputStream();
//3:把低级的输出流封装成数据输出流
DataOutputStream dos= new DataOutputStream(os);
Scanner sr=new Scanner(System.in);
while (true) {
//开始写数据出去了
System.out.print("请说: ");
String msg= sr.nextLine();
dos.writeUTF(msg);
dos.flush();
//用户输入exit 退出
if ("exit".equals(msg)){
System.out.println("欢迎下次光临 退出成功");
dos.close();
socket.close();
break;
}
}
}
}
服务端主线程
package TCP3;
// 完成TCP通信服务端 多发多收
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
System.out.println("===============服务端启动成功================");
//1:创建serverSocket的对象 同时为服务端注册端口
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
//2:使用serverSocket 对象 调用accept 方法 等待客户端连接请求
Socket socket= serverSocket.accept();
System.out.println("有人上线了"+socket.getRemoteSocketAddress());
// 3:把客户端对应的Socket 通信管道 交给独立的线程负责
new ServerReaderThread(socket).start();
}
}
}
服务端子线程
package TCP3;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class ServerReaderThread extends Thread{
private Socket socket;
public ServerReaderThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
super.run();
try {
InputStream is= socket.getInputStream();
DataInputStream dis= new DataInputStream(is);
while (true){
try {
String msg= dis.readUTF();
System.out.println(msg);
} catch (Exception e) {
System.out.println("有人下线了"+socket.getRemoteSocketAddress());
dis.close();
socket.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
群聊
客户端主线程
package TCP4;
//完成TCP通信的 多发多收
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args)throws Exception {
//1:创建Socket对象并同时请求服务端程序的链接
Socket socket= new Socket("127.0.0.1",8888);
//创建一个独立的线程 负责随时从socket中接服务端发过来的消息
new ClintReaderThead(socket).start();
//2:从Socket通信管道中得到一个字节输出流 用来数据发给到服务器
OutputStream os= socket.getOutputStream();
//3:把低级的输出流封装成数据输出流
DataOutputStream dos= new DataOutputStream(os);
Scanner sr=new Scanner(System.in);
while (true) {
//开始写数据出去了
System.out.print("请说: ");
String msg= sr.nextLine();
dos.writeUTF(msg);
dos.flush();
//用户输入exit 退出
if ("exit".equals(msg)){
System.out.println("欢迎下次光临 退出成功");
dos.close();
socket.close();
break;
}
}
}
}
客户端子线程
package TCP4;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;
public class ClintReaderThead extends Thread{
private Socket socket;
public ClintReaderThead(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
super.run();
try {
InputStream is= socket.getInputStream();
DataInputStream dis= new DataInputStream(is);
while (true){
try {
String msg= dis.readUTF();
System.out.println(msg);
//把这个消息分配发给全部客户端进行接收
} catch (Exception e) {
System.out.println("自己下线了"+socket.getRemoteSocketAddress());
dis.close();
socket.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务端主线程
package TCP4;
// 完成TCP通信服务端 多发多收
import javax.swing.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static List<Socket> onlineSocket =new ArrayList<>();
public static void main(String[] args) throws Exception {
System.out.println("===============服务端启动成功================");
//1:创建serverSocket的对象 同时为服务端注册端口
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
//2:使用serverSocket 对象 调用accept 方法 等待客户端连接请求
Socket socket= serverSocket.accept();
onlineSocket.add(socket);
System.out.println("有人上线了"+socket.getRemoteSocketAddress());
// 3:把客户端对应的Socket 通信管道 交给独立的线程负责
new ServerReaderThread(socket).start();
}
}
}
服务端子线程
package TCP4;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ServerReaderThread extends Thread{
private Socket socket;
public ServerReaderThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
super.run();
try {
InputStream is= socket.getInputStream();
DataInputStream dis= new DataInputStream(is);
while (true){
try {
String msg= dis.readUTF();
System.out.println(msg);
//把这个消息分配发给全部客户端进行接收
SendNsgToAll(msg);
} catch (Exception e) {
System.out.println("有人下线了"+socket.getRemoteSocketAddress());
Server.onlineSocket.remove(socket);
dis.close();
socket.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void SendNsgToAll(String msg)throws Exception {
// 发送给全部在线的Socket管道接收
for (Socket socket1 : Server.onlineSocket) {
OutputStream os= socket1.getOutputStream();
DataOutputStream dos =new DataOutputStream(os);
dos.writeUTF(msg);
dos.flush();
}
}
}