• 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要加油
  • 相关阅读:
    关于IP4上WIFI设置静态IP的一点经验
    迷你MVVM框架 avalonjs 1.1发布
    r.js合并实践
    IE6的checkbox, radio是通过defaultChecked决定是否选中
    2013年最后的收成:avalon1.0正式发布
    还要多少年, 前端开发才能像后端那样轻松
    JavaScript Promise:去而复返
    JavaScript 18岁生日快乐
    web界面上的字体兼容方案
    键盘钩子原理----开发按键发音程序
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14293457.html
Copyright © 2020-2023  润新知