• 屌丝与女神聊天,已读不回~socket简单小案例


    屌丝与女神聊天,已读不回~socket简单小案例

    代码:

    屌丝代码(客户端Socket)

    package TCPDemo;
    
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    import java.util.Scanner;
    
    
    
    /**
     * 客户端Socket
     * @author Huangyujun
     *
     */
    public class TCPSocket {
        public static void main(String[] args) throws IOException {
            Scanner scanner = new Scanner(System.in);
            Socket socket = new Socket("localhost", 9527);    //连接服务端
            //输出流、输入流
            OutputStream outStream = socket.getOutputStream();
            InputStream inStream =  socket.getInputStream();
            while(true) {
    //            OutputStream outStream = socket.getOutputStream();
                System.out.println("屌丝发送消息:");
                String content  = scanner.next();
                content += "
    ";
                //发送消息
                outStream.write(content.getBytes());
                if("退出".equals(content)) {
                    break;
                }
                outStream.flush();
            }
        
            
        }
    }

    女神代码(服务端ServerSocket)

    package TCPDemo;
    
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    import java.util.Scanner;
    
    
    
    /**
     * 客户端Socket
     * @author Huangyujun
     *
     */
    public class TCPSocket {
        public static void main(String[] args) throws IOException {
            Scanner scanner = new Scanner(System.in);
            Socket socket = new Socket("localhost", 9527);    //连接服务端
            //输出流、输入流
            OutputStream outStream = socket.getOutputStream();
            InputStream inStream =  socket.getInputStream();
            while(true) {
    //            OutputStream outStream = socket.getOutputStream();
                System.out.println("屌丝发送消息:");
                String content  = scanner.next();
                content += "
    ";
                //发送消息
                outStream.write(content.getBytes());
                if("退出".equals(content)) {
                    break;
                }
                outStream.flush();
            }
        
            
        }
    }
  • 相关阅读:
    身份证验证
    图片赖加载js代码
    cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass'
    qt undefined reference to `vtable for subClass'
    .net 与directX
    小代码,大错误
    基类子类在Qt信号量机制下的思考
    QT linux下显示中文的问题
    在QT中引用Shark Machine Learning library
    MVC 5 on Windows Server 2008/IIS 7
  • 原文地址:https://www.cnblogs.com/shan333/p/14803888.html
Copyright © 2020-2023  润新知