TCP transport mode

TCP

Client

Create socket object is created tcp client, clear the server address and port, it must correspond with the server, or else the other party can not receive the message, as an erroneous express shipping address.

Socket s = new Socket("127.255.255.25", 10003);

Then there will be a connection is established after the passage of IO stream socket, as a client we are sending data, we will send the data to the server to handle it, so we have obtained is OutPutStream, the data is written to the server

OutputStream out = s.getOutputStream();

Then the rest is up to the server to write data, and these operations are in fact the same as a piece of IO streams, such as we are below this

out.write("hello,TCP来了".getBytes());

Finally, we must remember to close the socket resource

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class TcpClient {
    public static void main(String[] args) throws IOException, IOException {

        System.out.println("客户端运行。。。");
        /**
         *  需求:通过tcp传输数据发送给服务器
         *  思路:
         *      1、建立tcp客户端socket.明确服务端的地址和端口
         *      2、如果通道建立成功就会出现socket io流
         *          客户端需要做的就是获取socket流中输出流将数据发送目的地服务端
         *      3、通过socket输出流将数据发送
         *      4、关闭
         */
        //1、建立tcp客户端socket.明确服务端的地址和端口

        Socket s = new Socket("127.255.255.25", 10003);

        //2、如果通道建立成功就会出现socket io流      客户端需要做的就是获取socket流中输出流将数据发送目的地服务端
        //3、通过socket输出流将数据发送

        OutputStream out = s.getOutputStream();
        out.write("hello,TCP来了".getBytes());
        //4、关闭
        s.close();
    }
}

When you have finished sending data to the server, the server will likely give you back some data, this time need to go receive the data sent by the server to the client, only

InputStream in = s.getInputStream();

Get read the stream, and then the same operation as the data stream IO read return can be a

package tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


public class TCPClient2 {

    public static void main(String[] args) throws IOException{
        System.out.println("客户端2   run...");
        /**
         *  案例二:实现客户端和服务端的收发过程 
         */
        //创建客户端socket对象,明确服务端地址和端口
        Socket s = new Socket("127.255.255.25",10009);
        
        //发送数据通过socket输出流完成
        OutputStream out = s.getOutputStream();
        out.write("服务端,我来了".getBytes());
        
        //读取服务器端返回的数据
        InputStream in = s.getInputStream();
        byte[] buf = new byte[1024];
        int len= in.read(buf);
        System.out.println(new String(buf,0,len));
//      int len = 0;
//      while((len = in.read(buf)) != -1)
//      {
//          System.out.println(new String(buf,0,len));
//      }
        
        
        //关闭资源
        s.close();
        
    }
}

Server

To end the server is connected to the client, you must create a server Serversocket objects and clear the port, the port must be consistent socket port connected to the client, and then use a socket object to receiving client socket

 ServerSocket ss = new ServerSocket(10003);//创建服务端的socket。需要明确端口(监听一个端口),要不然客户端无法连接
 Socket s = ss.accept();//服务端只要获取到连接过来的客户端就可以和指定的客户端通信了

The receiving client socket object which encapsulates only s data content, as well as host IP data sources and so forth.
Then get a stream object, the client reads data transmitted

 InputStream in = s.getInputStream();

Then it and IO operations like data processing can be.

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) throws IOException {
        System.out.println("服务端。。。");
        /**
         *  需求:获取客户端的数据并显示在屏幕上
         *
         *  思路:
         *      1、创建服务端的socket。需要明确端口(监听一个端口),要不然客户端无法连接
         *      2、服务端只要获取到连接过来的客户端就可以和指定的客户端通信了
         *      3、通过获取客户端的读取流对象读取客户端发来的数据
         *      33、并显示在屏幕上
         *      5、关闭资源
         */

        //1、创建服务端的socket。需要明确端口(监听一个端口),要不然客户端无法连接

        ServerSocket ss = new ServerSocket(10003);

        //2、服务端只要获取到连接过来的客户端就可以和指定的客户端通信了

        Socket s = ss.accept();
        String ip = s.getInetAddress().getHostAddress();
        System.out.println(ip+"...connected");
        //3、通过获取客户端的读取流对象读取客户端发来的数据
        InputStream in = s.getInputStream();

        //33、并显示在屏幕上
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = in.read(buf)) != -1)
        {
            System.out.println(new String(buf, 0, len));
        }

        //5、关闭资源
        s.close();
        //ss.close();   这一般不关
    }
}

General service termination after receiving the data, it will return some client feedback, we can continue to get the stream read and write, to write to the client, that is feedback.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer2 {
    public static void main(String[] args) throws IOException {

        System.out.println("服务端2   run...");
        /**
         *  案例二:实现客户端和服务端的收发过程 
         *  服务器端
         */
        
        //创建tcp服务端socket明确端口
        ServerSocket ss = new ServerSocket(10009);
        while(true)
        {
            //获取客户端对象
            Socket s = ss.accept();
            System.out.println(s.getInetAddress().getHostAddress()+"...connected");

            //读取客户端发送过来的数据
            InputStream in = s.getInputStream();
            byte[] buf = new byte[1024];
            int len = in.read(buf);         //读取到数据之前,线程处于阻塞状态
            System.out.println(new String(buf,0,len));
//          int len = 0;
//          while((len = in.read(buf)) != -1)
//          {
//              System.out.println(new String(buf,0,len));
//          }
//          
//          System.out.println("阻塞了吗?");
            
            //给客户端回馈数据
            OutputStream out = s.getOutputStream();
            out.write("客户端。我已收到。哦耶!".getBytes());

            //关闭客户端
            s.close();
        }
        
        //关闭服务端
        //ss.close();   如果是不断的获取客户端,就不用关闭服务器
    }
}

The results demonstrate:

First open the service end
Here Insert Picture Description
server receives data
Here Insert Picture Description
client receives feedback
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/vfdxvffd/p/11703323.html