一、简单的客户端与服务器一对一连接:
Socket通信的步骤:
1、创建ServerSocket和Socket
2、打开连接到Scket的输入/输出流
3、按照协议对Socket进行读/写操作
4、关闭输入输出流、关闭Socket
废话不多说,直接上代码,服务端和客户端的注释写的很清楚!!!
package com.socket; 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 { public static void main(String[] args) { try { // 1.创建服务器端Socket对象,绑定并监听窗口 ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器已启动,等待客户端连接..."); // 2.调用accept()方法开始监听,等待客户端连接 Socket socket = serverSocket.accept(); // 3.获取输入流,并读取客户端信息 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()); } // 4.获取输出流,响应客户端的请求 OutputStream outputStream = socket.getOutputStream(); PrintWriter printWriter = new PrintWriter(outputStream); printWriter.print("你好服务端已收到您的信息"); printWriter.flush();// 强行将缓冲区的数据输出 socket.shutdownOutput();// 关闭输出流 // 5.关闭对应的资源 printWriter.close(); outputStream.close(); bufferedReader.close(); inputStream.close(); inputStreamReader.close(); socket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
package com.socket; 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; public class Client { public static void main(String[] args) { try { // 1.创建客户端Socket,制定服务器地址和端口 Socket socket = new Socket("localhost", 8888); // 2.获取输出流,向服务端发送信息 OutputStream outputStream = socket.getOutputStream();// 字节输出流 PrintWriter printWriter = new PrintWriter(outputStream);// 将输出流包装成打印流 printWriter.print("服务端你好,我是zz");// 内容 printWriter.flush();// 强制将缓冲区的数据输出 socket.shutdownOutput();// 关闭输出流 // 3.获取输入流,并读取服务器端的响应信息 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); } // 4.关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
输出的结果为:
客户端发送的消息为:你好服务端已收到您的信息
服务器已启动,等待客户端连接...
已收到客户端连接
服务端接收到客户端信息:服务端你好,我是zz当前客户端ip:127.0.0.1
二、多个客户端与服务器连接:
package com.threadsocket; 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; 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.flush(); socket.shutdownOutput();//关闭输出流 //关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
package com.threadsocket; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { 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(); } } }
package com.threadsocket; 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 { public static void main(String[] args) { try { // 创建Socekt对象 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(); } } }
package com.threadsocket; 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 Client2 { public static void main(String[] args) { try { // 创建Socekt对象 Socket socket = new Socket("localhost", 8888); // 根据输入输出流和服务端连接 OutputStream outputStream = socket.getOutputStream();// 获取一个输出流向服务端发送信息 PrintWriter printWriter = new PrintWriter(outputStream); printWriter.print("服务端你好,我是客户2"); 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(); } } }
运行结果:
服务端已启动,等待客户端连接...
已接收到客户端连接
服务端接收到客户端信息:服务端你好,我是客户2当前客户端ip为:127.0.0.1
已接收到客户端连接
服务端接收到客户端信息:服务端你好,我是客户1当前客户端ip为:127.0.0.1