TCP

TCP implementation chat

The service side

package com.zr.lesson02;

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

/ / the server
public class TcpServerDemo01 {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;// elevate the scope
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // An address is required
            serverSocket = new ServerSocket(9999);
            // Wait for the client to connect
            socket = serverSocket.accept();
            // Read messages from the client
            is = socket.getInputStream();
            / / pipe flow
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while((len=is.read(buffer))! = -1){
               baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());

            /*bytes[] buffer = new bytes[1024]; int len; while((len=is.read(buffer))! =-1){ StringBuffer msg = new StringBuffer(buffer,0,len); System.out.println(msg); } * /
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // Close the resource
            if(baos! =null) {try {
                    baos.close();
                } catch(IOException e) { e.printStackTrace(); }}if(is! =null) {try {
                    is.close();
                } catch(IOException e) { e.printStackTrace(); }}if(socket! =null) {try {
                    socket.close();
                } catch(IOException e) { e.printStackTrace(); }}if(serverSocket! =null) {try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

The client

package com.zr.lesson02;

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

/ / the client
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        // Have the server address
        try {
            // Get the server address port number
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            // Create a Socket connection
            socket = new Socket(serverIP,port);
            // Send a message
            os = socket.getOutputStream();
            os.write("Hello, welcome!.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(socket! =null) {
                try {
                    socket.close();
                } catch(IOException e) { e.printStackTrace(); }}if(os! =null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code