基于TCP协议Socket服务端和客户端的通信模型:
Socket通信步骤:(简单分为4步)
1.建立服务端ServerSocket和客户端Socket
2.打开连接到Socket的输出输入流
3.按照协议进行读写操作
4.关闭相对应的资源
服务端Server.java
1.创建ServerSocket对象,绑定并监听端口
2.通过accept监听客户端的请求
3.建立连接后,通过输出输入流进行读写操作
4.关闭相关资源
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { /** * Socket服务端 */ public static void main(String[] args) { try { ServerSocket serverSocket=new ServerSocket(8888); System.out.println("服务端已启动,等待客户端连接.."); Socket socket=serverSocket.accept();//侦听并接受到此套接字的连接,返回一个Socket对象 //根据输入输出流和客户端连接 InputStream inputStream=socket.getInputStream();//得到一个输入流,接收客户端传递的信息 InputStreamReader inputStreamReader=new InputStreamReader(inputStream);//提高效率,将自己字节流转为字符流 BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//加入缓冲区 String temp=null; String info=""; while((temp=bufferedReader.readLine())!=null){ info+=temp; System.out.println("已接收到客户端连接"); System.out.println("服务端接收到客户端信息:"+info+",当前客户端ip为:"+socket.getInetAddress().getHostAddress()); } OutputStream outputStream=socket.getOutputStream();//获取一个输出流,向服务端发送信息 PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流 printWriter.print("你好,服务端已接收到您的信息"); printWriter.flush(); socket.shutdownOutput();//关闭输出流 //关闭相对应的资源 printWriter.close(); outputStream.close(); bufferedReader.close(); inputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
客户端Client.java
1.创建Socket对象,指定服务端的地址和端口号
2.建立连接后,通过输出输入流进行读写操作
3.通过输出输入流获取服务器返回信息
4.关闭相关资源
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { /** * Socket客户端 */ public static void main(String[] args) { try { //创建Socket对象 Socket socket=new Socket("localhost",8888); //根据输入输出流和服务端连接 OutputStream outputStream=socket.getOutputStream();//获取一个输出流,向服务端发送信息 PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流 printWriter.print("服务端你好,我是Balla_兔子"); printWriter.flush(); socket.shutdownOutput();//关闭输出流 InputStream inputStream=socket.getInputStream();//获取一个输入流,接收服务端的信息 InputStreamReader inputStreamReader=new InputStreamReader(inputStream);//包装成字符流,提高效率 BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//缓冲区 String info=""; String temp=null;//临时变量 while((temp=bufferedReader.readLine())!=null){ info+=temp; System.out.println("客户端接收服务端发送信息:"+info); } //关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上代码实现了单客户端和服务端的连接,若要实现多客户端操作,需要涉及到多线程,只要你把每个接收到的Socket对象单独开一条线程操作,然后用一个死循环while(true)去监听端口就行,这边直接给代码了
线程操作类:SocketThread.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; /** * Socket多线程处理类 用来处理服务端接收到的客户端请求(处理Socket对象) */ public class SocketThread extends Thread { private Socket socket; public SocketThread(Socket socket) { this.socket = socket; } public void run() { // 根据输入输出流和客户端连接 try { InputStream inputStream = socket.getInputStream(); // 得到一个输入流,接收客户端传递的信息 InputStreamReader inputStreamReader = new InputStreamReader( inputStream);// 提高效率,将自己字节流转为字符流 BufferedReader bufferedReader = new BufferedReader( inputStreamReader);// 加入缓冲区 String temp = null; String info = ""; while ((temp = bufferedReader.readLine()) != null) { info += temp; System.out.println("已接收到客户端连接"); System.out.println("服务端接收到客户端信息:" + info + ",当前客户端ip为:" + socket.getInetAddress().getHostAddress()); } OutputStream outputStream = socket.getOutputStream();// 获取一个输出流,向服务端发送信息 PrintWriter printWriter = new PrintWriter(outputStream);// 将输出流包装成打印流 printWriter.print("你好,服务端已接收到您的信息"); printWriter.flush(); socket.shutdownOutput();// 关闭输出流 // 关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
服务端类:Server.java
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { /** * Socket服务端 */ public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务端已启动,等待客户端连接.."); while (true) { Socket socket = serverSocket.accept();// 侦听并接受到此套接字的连接,返回一个Socket对象 SocketThread socketThread = new SocketThread(socket); socketThread.start(); } } catch (IOException e) { e.printStackTrace(); } } }
客户端类:Client.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { /** * Socket客户端 */ public static void main(String[] args) { try { //创建Socket对象 Socket socket=new Socket("localhost",8888); //根据输入输出流和服务端连接 OutputStream outputStream=socket.getOutputStream();//获取一个输出流,向服务端发送信息 PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流 printWriter.print("服务端你好,我是客户1"); printWriter.flush(); socket.shutdownOutput();//关闭输出流 InputStream inputStream=socket.getInputStream();//获取一个输入流,接收服务端的信息 InputStreamReader inputStreamReader=new InputStreamReader(inputStream);//包装成字符流,提高效率 BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//缓冲区 String info=""; String temp=null;//临时变量 while((temp=bufferedReader.readLine())!=null){ info+=temp; System.out.println("客户端接收服务端发送信息:"+info); } //关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
这里只是抛砖引玉,在实际开发中,基于Socket编程,一般传递的并非字符串,很多情况下是对象,我们可以使用ObjectOutputStream将输出流对象序列化。
例如:
OutputStream outputStream = socket.getOutputStream(); ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream); User user=new User("admin","123456"); objectOutputStream.writeObject(user);