zoukankan      html  css  js  c++  java
  • 屌丝与女神聊天,已读不回~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();
            }
        
            
        }
    }
  • 相关阅读:
    PHP基础知识系列:预定义变量
    PHP基础知识系列:拦截器方法
    【PHP设计模式】创建型之单例(Singleton)
    PHP基础知识系列:cookie与session
    select问题
    柳传志给杨元庆的信
    PHP基础知识系列:多态性
    PHP基础知识系列:面向对象基础
    PHP处理字符串翻转问题总结
    PHP基础知识系列:静态
  • 原文地址:https://www.cnblogs.com/shan333/p/14803888.html
Copyright © 2011-2022 走看看