zoukankan      html  css  js  c++  java
  • Tom猫-Socket

    • Client
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    public class TestSockClient {
        public static void main(String[] args) {
            DataInputStream dis = null;
            DataOutputStream dos = null;
            Socket socket = null;
            Scanner scan = null;
            String  sendStr;
            String receiveStr;
            try {
                socket = new Socket("localhost", 8888);
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream( socket.getOutputStream());
                scan = new Scanner(System.in);
                do {
                    System.out.print("我:");
                    sendStr = scan.nextLine();
                    dos.writeUTF(sendStr);
                    if ((receiveStr = dis.readUTF()) != null) {
                        System.out.println(receiveStr);
                    }
                } while (!sendStr.equals("88"));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                    dis.close();
                    dos.close();
                    socket.close();
                    scan.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • Server
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TestSockServer {
        public static void main(String[] args) {
            ServerSocket server = null;
            Socket socket = null;
            DataOutputStream dos = null;
            DataInputStream dis = null;
            try{
                server = new ServerSocket(8888);
                System.out.println("服务器已开启");
                socket = server.accept();
                dos = new DataOutputStream(socket.getOutputStream());
                dis = new DataInputStream(socket.getInputStream());
                String receiveStr ;
                while((receiveStr = dis.readUTF())!= null) {
                    System.out.println("客户端发送:"+receiveStr);
                    dos.writeUTF("Tom:"+receiveStr);
                }
    
            } catch(EOFException e) {
                System.out.println("服务器已关闭");
            }catch(IOException e){
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try{
                    dis.close();
                    dos.close();
                    socket.close();
                    server.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 运行结果
      image.png
      image.png
    ljm要加油
  • 相关阅读:
    超全面的vue.js使用总结
    Python3 [字典】类型 学习笔记
    Python3 [集合]类型 学习笔记
    Python 希尔排序法
    Python 堆排序法
    Python 归并排序法
    Python 冒泡排序法
    Python 选择排序法
    Python 快速排序法(转)
    Python 插入排序法
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14293457.html
Copyright © 2011-2022 走看看