欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java socket小例子

程序员文章站 2022-04-09 13:12:15
...

服务器

package ws.util.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentLinkedQueue;

public class server {
    public static void main(String[] args) throws Exception {
        ConcurrentLinkedQueue<Socket> concurrentLinkedQueue=new ConcurrentLinkedQueue<>();
        new Thread(()->{
            while(true){
                Socket s = concurrentLinkedQueue.poll();

                if(s==null)
                    continue;
                try {
                    InputStream inputStream = s.getInputStream();
                    if(inputStream.available()>0){
                        byte[] b=new byte[inputStream.available()];
                        inputStream.read(b);
                        System.out.println("server: "+new String(b));
                        OutputStream outputStream=s.getOutputStream();
                        outputStream.write("server recive info".getBytes());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                concurrentLinkedQueue.offer(s);
            }

        }).start();

        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(8888));
        /**
         * accept()将仅阻塞此时间量。如果超时过期,java.net.SocketTimeoutException尽管ServerSocket仍然有效。
         * 零被解释为无限时,默认就是0
         */
        serverSocket.setSoTimeout(0);
        while(true){
            Socket accept = serverSocket.accept();
            /**
             * 如果为TCP套接字设置了keepalive选项,并且两个小时内没有任何数据在套接字的任何方向上交换(注意:实际值取决于实现),
             * TCP会自动向对方发送keepalive探测。预计将有三种答复之一:
             * 1对等机以预期的ACK响应。因为一切正常。TCP将在不活动2小时后发送另一个探测。
             * 2对等方使用RST进行响应,RST告诉本地TCP对等主机已崩溃并重新启动。Socket已关闭。
             * 3对方没有回应。Socket已关闭
             */
            accept.setKeepAlive(true);
            /**
             * 套接字关联的InputStream的read()调用将仅阻塞此时间量
             * 超时为零被解释为无限超时
             */
            accept.setSoTimeout(0);
            concurrentLinkedQueue.offer(accept);

        }
    }
}

客户端

package ws.util.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

public class client {
    public static void main(String[] args) throws Exception {
        Scanner s=new Scanner(System.in);
        Socket socket=new Socket();
        //3000指的是连接超时时间毫秒,和read超时不是一个配置
        socket.connect(new InetSocketAddress("localhost",8888),3000);
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        while(true){
            System.out.println("write:");
            String s1 = s.nextLine();
            outputStream.write(("my client into "+s1).getBytes());
            byte[] b=new byte[1024];
            inputStream.read(b);
            System.out.println("client: "+new String(b));
        }
    }
}

相关标签: java socket java