1、简单的通讯
服务器端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author: butcher
* @Date: 2021/03/06/12:07
*/
public class Server {
public static void main(String[] args) {
try {
// 创建端口
ServerSocket serverSocket = new ServerSocket(9888);
System.out.println("等待新用户进入.....");
Socket socket = serverSocket.accept();
// 获取客户端的数据
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String mes = "";
while((mes=br.readLine())!=null){
//从客户端读到了数据
System.out.println(mes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* @Author: butcher
* @Date: 2021/03/06/12:13
*/
public class Client {
public static void main(String[] args) {
try {
// 创建套接字对象
Socket socket = new Socket("localhost",9888);
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os);
Scanner sc = new Scanner(System.in);
while (true){
out.println("用户信息:"+sc.next());
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
2、多个客户端向服务器通讯
实现的原理是多线程
服务器端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
/**
* @Author: butcher
* @Date: 2021/03/06/12:07
*/
public class Server {
public static void main(String[] args) {
try {
// 创建端口
ServerSocket serverSocket = new ServerSocket(9888);
System.out.println("等待新用户进入.....");
while(true){
// 要接收多个请求
Socket socket = serverSocket.accept();
Random random = new Random();
int name = random.nextInt(100);
System.out.println(name+"新用户进入!");
// 对每个请求创建一个线程来处理
Thread t = new Thread(new Runnable() {
Socket socket = null;
int name;
@Override
public void run() {
InputStream is = null;
try {
// 获取客户端的数据
is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String mes = "";
while((mes=br.readLine())!=null){
//从客户端读到了数据
System.out.println(name+":"+mes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 通过放方法向匿名内部类传参
public Runnable getSocket(Socket socket,int name){
this.socket = socket;
this.name = name;
return this;
}
}.getSocket(socket,name));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* @Author: butcher
* @Date: 2021/03/06/12:13
*/
public class Client {
public static void main(String[] args) {
try {
// 创建套接字对象
Socket socket = new Socket("localhost",9888);
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os);
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("请输入:");
out.println(sc.next());
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
3、模拟私聊,点对点通信
原理就是一个客户端就是一台服务器,可以发数据,也可以收数据,但是首先要把服务先起,只有两个的服务都起了以后,才能进行聊天。
按照端口号进行通信,客户端1的服务端口号是客户端2请求的端口号,反之一样。
代码:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* @Author: butcher
* @Date: 2021/03/06/14:36
*/
public class Client2 {
public static void main(String[] args) {
try {
// 创建本机服务
ServerSocket serverSocket = new ServerSocket(9886);
Socket accept = serverSocket.accept();
// 睡眠3秒等待连接
Thread.sleep(3000);
//创建套接字对象
Socket socket = new Socket("localhost",9887);
InputStream inputStream = accept.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
String mes="";
try {
while ((mes = br.readLine())!=null){
System.out.println(mes);
}
}catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os);
Scanner sc = new Scanner(System.in);
while (true){
out.println("悟空:"+sc.next());
out.flush();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果: