首页 🍄Java

由于TCP需要两个用户之间建立连接才可以发送消息所以如果像UDP那样直接开启两个线程一个发送一个接受的话,用户一就会因为发送端开启但是连接不到接收端而无法发送。
所以就要在发送端加一个条件,当socket连接上接收端都才能进行下面的操作。

TCP实现:

Client端:
此类实现客户端套接字(也可以就叫“套接字”)。套接字是两台机器间通信的端点。
1:创建客户端的套接字Socket
2:获取输出流
3:输出流写数据
4:关闭套接字

Server端
此类实现服务器套接字。服务器套接字等待请求通过网络传入。它基于该请求执行某些操作,然后可能向请求者返回结果。
1:创建服务器的套接字ServerSocket
2:监听客户端的套接字
3:获取输入流读取内容
4:关闭套接字

发送端:

package net.tcp;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Send implements Runnable {
    private String address;
    private int port;
    private String send;
    private Socket socket;

    public Send(String address, int port) throws IOException {
        this.address = address;
        this.port = port;
        socket = null;
    }

    @Override
    public void run() {
        try {
            while (socket == null) {
                socket = new Socket(address, port);
                Thread.sleep(1000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                send = new Scanner(System.in).nextLine();
                if (send != null) {
                    dataOutputStream.writeUTF(send);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

接收端:

package net.tcp;

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

public class Server implements Runnable {
    private int port;
    private ServerSocket serverSocket;
    private Socket socket;
    private DataInputStream dataInputStream;

    public Server(int port) throws IOException {
        this.port = port;
        serverSocket = new ServerSocket(port);
    }

    @Override
    public void run() {
        try {
            socket = serverSocket.accept();
            System.out.println("连接成功");
            dataInputStream = new DataInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                String receiver = dataInputStream.readUTF();
                System.out.println(receiver);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

用户一

package net.tcp;

import java.io.IOException;

public class Test_1 {
    public static void main(String[] args) throws IOException {
        Send send = new Send("127.0.0.1", 8888);
        Server server = new Server(8989);
        new Thread(server).start();
        new Thread(send).start();
    }
}

用户二

package net.tcp;

import java.io.IOException;

public class Test_2 {
    public static void main(String[] args) throws IOException {
        Send send = new Send("127.0.0.1", 8989);
        Server server = new Server(8888);
        new Thread(server).start();
        new Thread(send).start();
    }
}



文章评论

目录