微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java--Socket通信客户端服务端双向

这篇文章主要介绍了Java--Socket通信(客户端服务端双向),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

新建两个工程,一个客户端,一个服务端,先启动服务端再启动客户端

两个工程的读写操作线程类基本上完全相同

服务端:

import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.socket; public class Server { public static final int PORT = 8000;//监听的端口号 public static void main(String[] args) { Server server = new Server(); server.init(); } public void init() { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(PORT); while (true) { Socket client = serverSocket.accept(); //一个客户端连接就开户两个线程处理读写 new Thread(new ReadHandlerThread(client)).start(); new Thread(new WriteHandlerThread(client)).start(); } } catch (Exception e) { e.printstacktrace(); } finally{ try { if(serverSocket != null){ serverSocket.close(); } } catch (IOException e) { e.printstacktrace(); } } } } /* *处理读操作的线程 */ class ReadHandlerThread implements Runnable{ private Socket client; public ReadHandlerThread(Socket client) { this.client = client; } @Override public void run() { DataInputStream dis = null; try{ while(true){ //读取客户端数据 dis = new DataInputStream(client.getInputStream()); String reciver = dis.readUTF(); System.out.println("客户端发过来的内容:" + reciver); } }catch(Exception e){ e.printstacktrace(); }finally{ try { if(dis != null){ dis.close(); } if(client != null){ client = null; } } catch (IOException e) { e.printstacktrace(); } } } } /* * 处理写操作的线程 */ class WriteHandlerThread implements Runnable{ private Socket client; public WriteHandlerThread(Socket client) { this.client = client; } @Override public void run() { DataOutputStream dos = null; BufferedReader br = null; try{ while(true){ //向客户端回复信息 dos = new DataOutputStream(client.getoutputStream()); System.out.print("请输入:t"); // 键盘录入 br = new BufferedReader(new InputStreamReader(system.in)); String send = br.readLine(); //发送数据 dos.writeUTF(send); } }catch(Exception e){ e.printstacktrace(); }finally{ try { if(dos != null){ dos.close(); } if(br != null){ br.close(); } if(client != null){ client = null; } } catch (IOException e) { e.printstacktrace(); } } } }

客户端:

import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.socket; public class Client { public static final String IP = "localhost";//服务器地址 public static final int PORT = 8000;//服务器端口号 public static void main(String[] args) { handler(); } private static void handler(){ try { //实例化一个Socket,并指定服务器地址和端口 Socket client = new Socket(IP, PORT); //开启两个线程,一个负责读,一个负责写 new Thread(new ReadHandlerThread(client)).start(); new Thread(new WriteHandlerThread(client)).start(); } catch (Exception e) { e.printstacktrace(); } } } /* *处理读操作的线程 */ class ReadHandlerThread implements Runnable{ private Socket client; public ReadHandlerThread(Socket client) { this.client = client; } @Override public void run() { DataInputStream dis = null; try { while(true){ //读取服务器端数据 dis = new DataInputStream(client.getInputStream()); String receive = dis.readUTF(); System.out.println("服务器端返回过来的是: " + receive); } } catch (IOException e) { e.printstacktrace(); } finally{ try { if(dis != null){ dis.close(); } if(client != null){ client = null; } } catch (IOException e) { e.printstacktrace(); } } } } /* * 处理写操作的线程 */ class WriteHandlerThread implements Runnable{ private Socket client; public WriteHandlerThread(Socket client) { this.client = client; } @Override public void run() { DataOutputStream dos = null; BufferedReader br = null; try { while(true){ //取得输出流 dos = new DataOutputStream(client.getoutputStream()); System.out.print("请输入: t"); //键盘录入 br = new BufferedReader(new InputStreamReader(system.in)); String send = br.readLine(); //发送数据 dos.writeUTF(send); } } catch (IOException e) { e.printstacktrace(); } finally{ try{ if(dos != null){ dos.close(); } if(br != null){ br.close(); } if(client != null){ client = null; } }catch(Exception e){ e.printstacktrace(); } } } }

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐