• java socket


    1.服务器

    2.客户端

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Test6 {
        public static void main(String[] args) {
            int port = 99;
            try {
                ServerSocket serverSocket = new ServerSocket(port);
    
                while (true) {
                    Socket accept = serverSocket.accept();
                    //输入流
                    InputStream inputStream = accept.getInputStream();
                    byte[] bs = new byte[1024];
                    inputStream.read(bs);
    
                    String t1 = new String(bs);
    
                    System.out.println("收到:" + t1);
    
                    //输出流
                    OutputStream outputStream = accept.getOutputStream();
                    outputStream.write("给你:".getBytes());
    
                    accept.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            System.out.println("hello,world");
        }
    }
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class Test7 {
        public static void main(String[] args) {
    
            String host = "127.0.0.1";
            int port = 99;
    
            try {
                Socket socket = new Socket(host,port);
                OutputStream outputStream = socket.getOutputStream();
                outputStream.write("12345".getBytes());
    
                InputStream inputStream = socket.getInputStream();
                byte[] bs = new byte[1024];
                inputStream.read(bs);
    
                String t1 = new String(bs);
    
                System.out.println("收到:" + t1);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            System.out.println("hello,world");
        }
    }
    天生我材必有用,千金散尽还复来
  • 相关阅读:
    memcache启动程序/etc/sysconfig/memcached
    shell中的点号
    mysql监控
    secureCRT 中文乱码
    memcache key
    杀死所以数据库进程
    导出表记录
    重建二叉树
    从尾到头打印链表
    二维数组中的查找
  • 原文地址:https://www.cnblogs.com/ligenyun/p/14531735.html
Copyright © 2020-2023  润新知