Java网络编程——Socket
2021/9/18 12:05:06
本文主要是介绍Java网络编程——Socket,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 服务器端(单线程)
- 服务器端(多线程)
- 客户端
服务器端(单线程)
public class MySocketServer1 { private int port; private ServerSocket serverSocket; DataInputStream in; DataOutputStream out; public MySocketServer1(int port) throws IOException { this.port = port; serverSocket = new ServerSocket(port); } public void start() throws IOException { Socket socket = null; for (; ; ) { try { if (socket == null) socket = serverSocket.accept(); in = new DataInputStream(socket.getInputStream()); System.out.println("收到: " + socket.getInetAddress().getHostAddress() + "\n消息: " + in.readUTF() + "\n"); out = new DataOutputStream(socket.getOutputStream()); out.writeUTF("服务器已收到"); } catch (Exception e) { e.printStackTrace(); if (socket != null) { socket.close(); socket = null; } } } } public static void main(String[] args) throws IOException { new MySocketServer1(4399).start(); } }
服务器端(多线程)
public class MySocketServer2 implements Runnable { private int port; private ServerSocket serverSocket; DataInputStream in; DataOutputStream out; ExecutorService executorService = Executors.newFixedThreadPool(2); public MySocketServer2(int port) throws IOException { this.port = port; serverSocket = new ServerSocket(port); } public void start() throws IOException { executorService.execute(this); executorService.execute(this); } @Override public void run() { Socket socket = null; for (; ; ) { try { if (socket == null) socket = serverSocket.accept(); in = new DataInputStream(socket.getInputStream()); System.out.println("收到: " + socket.getInetAddress().getHostAddress() + "\n消息: " + in.readUTF() + "\n"); out = new DataOutputStream(socket.getOutputStream()); out.writeUTF("服务器已收到"); } catch (Exception e) { e.printStackTrace(); if (socket != null) { try { socket.close(); } catch (IOException ioException) { ioException.printStackTrace(); } socket = null; } } } } public static void main(String[] args) throws IOException { new MySocketServer2(4399).start(); } }
客户端
public class MySocketClient1 { private int port; Socket client; DataInputStream in; DataOutputStream out; public MySocketClient1(int port) throws IOException { this.port = port; client = new Socket("127.0.0.1", port); } public void start() throws IOException, InterruptedException { int n = 0; for (int i = 0; i < 3; i++) { out = new DataOutputStream(client.getOutputStream()); out.writeUTF("hello" + " " + n++); in = new DataInputStream(client.getInputStream()); System.out.println("收到: " + client.getRemoteSocketAddress() + "\n消息: " + in.readUTF() + "\n"); Thread.sleep(2000); } client.close(); } public static void main(String[] args) throws IOException, InterruptedException { new MySocketClient1(4399).start(); } }
这篇关于Java网络编程——Socket的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?