博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用多线程完成Socket
阅读量:4699 次
发布时间:2019-06-09

本文共 3733 字,大约阅读时间需要 12 分钟。

服务器端代码:

public class Service {  //服务器        public static void main(String[] args) { ServerSocket serverSocket=null; Socket socket=null; try { //创建一个超市 serverSocket=new ServerSocket(8800); while(true){ //超市开门 等待顾客上门购物 怎么保证超市是24消失营业的?? socket = serverSocket.accept(); //使用多线程来实现多个顾客能同时购物 同时结账 ServiceThread thread=new ServiceThread(socket); thread.start(); } } catch (IOException e) { e.printStackTrace(); } } }

服务器需要的线性类:

public class ServiceThread extends Thread {     //没启动一个线程 就相当于有一个顾客 进入 超市 Socket socket=null; public ServiceThread(Socket socket) { this.socket=socket; } @Override public void run() { InputStream is=null; OutputStream os=null; ObjectInputStream ois=null; //反序列化 try { //拿出钱包,推上购物车 is=socket.getInputStream(); os=socket.getOutputStream(); //反序列化 获取 顾客的信息 ois=new ObjectInputStream(is); User user=(User) ois.readObject(); //读到进入超市的顾客信息 if (user!=null) { System.out.println("服务器说:您的姓名是:"+user.getUserName()); } //给顾客一个回应 os.write("欢迎您的光临".getBytes()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally{ try { os.close(); ois.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } } }

客户端代码:

 

public class Client1 {  //第一个顾客    public static void main(String[] args) { Socket socket=null; InputStream is=null; OutputStream os=null; ObjectOutputStream oos=null; //接收服务器的信息 读 BufferedReader br=null; try { //进入了我们指定的 超市购物 socket=new Socket("localhost", 8800); is=socket.getInputStream(); os=socket.getOutputStream(); //序列化对象 oos=new ObjectOutputStream(os); User user=new User("小黑黑1", "admin"); oos.writeObject(user); //购物完毕 shutdownOutput 与 close socket.shutdownOutput(); //接收到服务器给你说的 欢迎光临 br=new BufferedReader(new InputStreamReader(is)); String line=null; while((line=br.readLine())!=null){ System.out.println("我是客户端:服务器 对我说:"+line); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { br.close(); oos.close(); os.close(); is.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }

基于UDP的客户端代码:

 

public class Service { // 服务器    public static void main(String[] args) { DatagramPacket dp = null; //打包 和 拆包数据 DatagramSocket ds = null; // 接收和 发送数据 //创建一个包 给客户端响应 DatagramPacket dpTo=null; try { byte [] buf=new byte[1024]; //创建数据包的对象 dp=new DatagramPacket(buf, buf.length); ds=new DatagramSocket(8800); //接收 ds.receive(dp); //显示接收的信息 拆包 String msg=new String(dp.getData(), 0, dp.getLength()); //获取 对方的地址 客户端的信息 System.out.println(dp.getAddress().getHostAddress()+"说====:"+msg); //回复给 客户端 byte[] reply="您好!航母已经发货!".getBytes(); // dp就是从客户端传来的信封 信封上肯定有 寄件人的地址 SocketAddress address=dp.getSocketAddress(); //打包成功 dpTo=new DatagramPacket(reply, reply.length,address); //发送 ds.send(dpTo); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

 

基于UDP的客户端代码:

public class Client {    //客户端    public static void main(String[] args) { DatagramPacket dp = null; //打包 和 拆包数据 DatagramSocket ds = null; // 接收和 发送数据 //创建一个包 给服务器响应 DatagramPacket dpTo=null; InetAddress address=null; try { //你在网上购物 要不要给客服 说你的地址 byte[] say="买个航空母舰!".getBytes(); //获取本机的地址! address = InetAddress.getByName("localhost"); //打包 dp=new DatagramPacket(say, say.length,address,8800); //发送 ds=new DatagramSocket(); ds.send(dp); //接收 byte [] buf=new byte[1024]; dpTo=new DatagramPacket(buf, buf.length); ds.receive(dpTo); //我们看客服给我们回复了 什么 拆包 String reply=new String(dpTo.getData(),0,dpTo.getLength()); System.out.println(dpTo.getAddress().getHostAddress()+"说===》"+reply); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

转载于:https://www.cnblogs.com/xiaobaizhang/p/7761722.html

你可能感兴趣的文章
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>
用户画像展示
查看>>
C#中StreamReader读取中文出现乱码
查看>>
使用BufferedReader的时候出现的问题
查看>>
批处理文件中的路径问题
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
配置NRPE的通讯
查看>>
匹配两个空格之间的字符。。。
查看>>
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>