网络编程的服务端和客户端
2021/12/20 17:20:05
本文主要是介绍网络编程的服务端和客户端,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
服务端:
public class Test1 { public static void main(String[] args) { ServerRead serverRead = new ServerRead(); serverRead.start(); while (true){ ServerSocket serverSocket = null; Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { serverSocket = new ServerSocket(6666); socket = serverSocket.accept(); //阻塞 in = new DataInputStream(socket.getInputStream()); System.out.println(in.readUTF()); out = new DataOutputStream(socket.getOutputStream()); //阻塞 Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); out.writeUTF("server:"+s); } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
public class Test{ public static void main(String args[]){ while (true){ Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { socket = new Socket("127.0.0.1",6666); out = new DataOutputStream(socket.getOutputStream()); Scanner scanner = new Scanner(System.in); //阻塞 String s = scanner.nextLine(); out.writeUTF("client:"+s); //阻塞 in = new DataInputStream(socket.getInputStream()); System.out.println(in.readUTF()); } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
问题:当任意一端发送两个消息以上,第一个问题是第二条(包括第二条)以后的消息不会立即发送到另外一端;第二个问题是从第三条开始的消息消失了。由于每个程序都是单线程的,而有的代码会造成线程阻塞,所以导致后面的代码执行不了。
解决方法:使用多线程
客户端
public class Test{ public static void main(String args[]){ while (true){ Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { socket = new Socket("127.0.0.1",6666); out = new DataOutputStream(socket.getOutputStream()); Scanner scanner = new Scanner(System.in); //阻塞 in = new DataInputStream(socket.getInputStream()); ClientRead clientRead = new ClientRead(in); clientRead.start(); // System.out.println(in.readUTF()); //阻塞 while (scanner.hasNext()){ String s = scanner.nextLine(); out.writeUTF("client:"+s); } } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } class ClientRead extends Thread{ DataInputStream in; public ClientRead(DataInputStream in){ this.in = in; } public void run(){ while (true){ try { System.out.printf("%30s\n",in.readUTF()); } catch (IOException e) { e.printStackTrace(); } } } }
服务器端
public class Test1 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { serverSocket = new ServerSocket(6666); socket = serverSocket.accept(); //阻塞 in = new DataInputStream(socket.getInputStream()); ServerRead serverRead = new ServerRead(in); serverRead.start(); // System.out.println(in.readUTF()); out = new DataOutputStream(socket.getOutputStream()); //阻塞 Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.nextLine(); out.writeUTF("server:"+s); } } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } class ServerRead extends Thread{ DataInputStream in = null; public ServerRead(DataInputStream in){ this.in = in; } public void run(){ while (true){ try { System.out.printf("%30s\n",in.readUTF()); } catch (IOException e) { e.printStackTrace(); } } } }
这篇关于网络编程的服务端和客户端的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南