Home  >  Article  >  类库下载  >  socket programming

socket programming

高洛峰
高洛峰Original
2016-10-20 11:33:181614browse

First, let us take a look at the simplest socket client and server examples:

Client

public class MyClient {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream bos = null;
        Socket client = null;
        try {
            People p = new People("2","yangyu","4","5","6");
            oos = new ObjectOutputStream(bos = new ByteArrayOutputStream()); //初始化object输出流
            oos.writeObject(p);  //将People对象写入输出流
            byte[] bytes = bos.toByteArray();  //获取People对象的byte数组(也就是序列化People)

            client = new Socket("127.0.0.1",20007);   //连接127.0.0.1的20007端口
            client.setSoTimeout(10000);   //设置超时时间

            client.getOutputStream().write(bytes);   //向server发送byte[]数组
            byte[] bytes1 = IOUtils.readFully(client.getInputStream(),18,false);  //获取server返回数据

            System.out.println(new String(bytes1));
            System.out.println(bytes1.length);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
                bos.close();
                System.out.println(client.isClosed());
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Server

public class MyServer {
    public static void main(String[] args) {
        ServerSocket server = null;
        Socket client = null;
        ObjectInputStream ois = null;
        ByteArrayInputStream bis = null;

        try {
            server = new ServerSocket(20007);  //启动Socket server,监听20007端口
            client = server.accept(); //阻塞并等待接收客户端发送数据并生成client

            byte[] bytes = IOUtils.readFully(client.getInputStream(),-1,false);//获取客户端发送过来的数据
            bis = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bis);
            People people = (People) ois.readObject();//反序列化
            System.out.println("people name:"+people.getName());

            String res = "消息已经收到";
            client.getOutputStream().write(res.getBytes());//向客户端发送数据

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bis.close();
                ois.close();
                client.close();
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

The above is a Client and a Server, the simplest example, but embodies socket programming.

If you need the Server to always listen to the port, you only need to loop (server.accept() will block waiting for requests). As for high-concurrency responses, let the Server handle the data business to the thread pool. .


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:JAVA WEB scopeNext article:JAVA WEB scope