zoukankan      html  css  js  c++  java
  • 03-Socket代码实现

    客户端实现(java.net.Socket)

    构造方法:

    Socket(String host,int port)   host:服务器主机名称/服务器的IP地址       port:端口号

    用到的成员方法:

    OutputStream getOutputStream()   返回socket的输出流

    InputStream getInputStream()      返回socket的输入流

    void close()     关闭socket

    实现步骤:

    1.创建一个客户端对象Socket,构造方法绑定服务器的IP地址以及端口号

    2.使用Socket对象的getOutputStream()获取网络字节输出流OutputStream对象

    3.使用OutputStream对象的write(),给服务器发送数据

    4.使用Socket对象的getInputStream() 获取网络字节输入流InputStream对象

    5.使用InputStream对象的read(),读取服务器回写的数据

    6.释放资源(Socket  OutputStream    InputStream)

    注意:

    1.客户端与服务器进行交互,必须使用Socket中提供的网络流,不能使用自己创建的流对象

    2.当创建一个客户端对象Socket时,就会去请求服务器,和服务器经过3次握手建立连接通路,如果这时服务器未开启,则会报出异常。

    package com.socket.TCPSocket;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    /*
        客户端:向服务器发送请求,给服务器发送数据,读服务器回写的数据
        Socket(套接字):包含了IP地址和端口号的网络单位
    */
    public class Client {
        public static void main(String[] args) {
            try {
                //1.创建一个客户端对象Socket,构造方法绑定服务器的IP地址以及端口号
                Socket client=new Socket("127.0.0.1",8989);
                //2.使用Socket对象的getOutputStream()获取网络字节输出流OutputStream对象
                OutputStream os=client.getOutputStream();
                //3.使用OutputStream对象的write(),给服务器发送数据
                os.write("你好服务器".getBytes());
                //4.使用Socket对象的getInputStream() 获取网络字节输入流InputStream对象
                InputStream is=client.getInputStream();
                //5.使用InputStream对象的read(),读取服务器回写的数据
                byte[] bytes=new byte[1024];
                int len=is.read(bytes);
                System.out.println(new String(bytes,0,len));
                //6.释放资源(Socket  OutputStream    InputStream)
                os.close();
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }

    服务器端实现(java.net.ServerSocket)

    构造方法:

    ServerSocket(int port)    绑定客户端端口号

    成员方法:

    Socket accept()    侦听并接收到socket的连接

    实现步骤:

    1.创建服务器ServerSocket对象和系统要指定的端口号

    2.使用ServerSocket的accept(),获取到请求的客户端对象Socket

    3.使用Socket对象的getInputStream() 获取网络字节输入流InputStream对象

    4.使用InputStream对象的read(),读取客户端发送的的数据

    5.使用Socket对象的getOutputStream()获取网络字节输出流OutputStream对象

    6.使用OutputStream对象的write(),给服务器发送数据

    7.释放资源(Socket  InputStream    OutputStream)

    package com.socket.TCPSocket;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    /*
        接收客户端的请求,读取客户端发送的信息,给客户端回写数据
    */
    public class Server {
        public static void main(String[] args)  {
            ServerSocket serverSocket= null;
            try {
                //1.创建服务器ServerSocket对象和系统要指定的端口号
                serverSocket = new ServerSocket(8989);//参数是端口号
                //2.使用ServerSocket的accept(),获取到请求的客户端对象Socket
                Socket server=serverSocket.accept();//与客户端建立连接
                //3.使用Socket对象的getInputStream() 获取网络字节输入流InputStream对象
                InputStream is=server.getInputStream();
                //4.使用InputStream对象的read(),读取客户端发送的的数据
                byte[] bytes=new byte[1024];
                int len=is.read(bytes);
                System.out.println(new String(bytes,0,len));
                //5.使用Socket对象的getOutputStream()获取网络字节输出流OutputStream对象
                OutputStream os=server.getOutputStream();
                //6.使用OutputStream对象的write(),给服务器发送数据
                os.write("服务器已收到数据".getBytes());
                //7.释放资源(Socket  InputStream    OutputStream)
                server.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
  • 相关阅读:
    [2017BUAA软工助教]结对项目小结
    DPDK flow_filtering 源码阅读
    DPDK flow_classify 源码阅读
    阅读源代码,查出某个宏定义在哪个头文件内的方法
    pktgen-dpdk 实战
    pktgen-dpdk 运行 run.py 报错 Config file 'default' not found 解决方法
    DPDK RX / TX Callbacks 源码阅读
    DPDK skeleton basicfwd 源码阅读
    DPDK helloworld 源码阅读
    DPDK实例程序:testpmd
  • 原文地址:https://www.cnblogs.com/rongrui/p/13976612.html
Copyright © 2011-2022 走看看