美文网首页
socket_tcp_demo

socket_tcp_demo

作者: 09c72470861c | 来源:发表于2018-08-29 22:46 被阅读0次

    实现客户端和服务器的一问一答,tcp比较可靠,demo中体现在先启客户端再启服务器会报refuse错误等。

    服务器端代码:

    package socket_demo1;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class SocketServer {
        public static void main(String[] args) throws IOException {
            //设置服务器端口
            ServerSocket ss = new ServerSocket(10086);
            //阻塞,以接收客户端数据
            Socket socket = ss.accept();
            //获取输入流
            InputStream is = socket.getInputStream();
            //通过InputStreamReader设置编码
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
    
            int i = 0;
            StringBuffer sb = new StringBuffer();
            //当读数据读取到最后,返回-1
            while ((i = isr.read()) != -1) {
                System.out.print((char) i);
                sb.append((char) i);
            }
            //shutdown输入流,可理解这一步为切换IO流
            socket.shutdownInput();
            //获取输入流
            OutputStream os = socket.getOutputStream();
            if ("chen".equals(sb.toString())) {
                os.write("牛逼!".getBytes());
            } else {
                os.write("what?".getBytes());
            }
            System.out.println();
            
            socket.shutdownOutput();
            isr.close();
            os.close();
            is.close();
        }
    }
    

    客户端代码:

    package socket_demo1;
    
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class ClientSocket {
        public static void main(String[] args) throws Exception {
            Scanner sc = new Scanner(System.in);
            // 不是多线程 他也没有 accept的监听
    
            //设置请求地址和端口
            Socket socket = new Socket("127.0.0.1", 10086);
            OutputStream os = socket.getOutputStream();
            System.out.println("输入消息:");
            String msg = sc.next();
            os.write(msg.getBytes());
    
            socket.shutdownOutput();
    
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
    
            int i = 0;
            while ((i = isr.read()) != -1) {
                System.out.print((char) i);
            }
            System.out.println();
            socket.shutdownInput();
    
            isr.close();
            os.close();
            is.close();
        }
    }
    

    先启动服务器,再启动客户端,在客户端的控制台输入“chen”,或者其他消息,服务器可返回不同的消息。如:

    输入消息:
    chen
    牛逼!
    

    但是先启动客户端再启动服务器,就会报错:

    Exception in thread "main" java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at java.net.Socket.<init>(Socket.java:434)
        at java.net.Socket.<init>(Socket.java:211)
        at socket_demo1.ClientSocket.main(ClientSocket.java:16)
    

    相关文章

      网友评论

          本文标题:socket_tcp_demo

          本文链接:https://www.haomeiwen.com/subject/uxdlwftx.html