JAVAソケットクライアントとサーバーはメッセージを交換します

3.1サーバーコード

インポートjava.io.IOException;
インポートjava.io.InputStream;
インポートjava.io.OutputStream;
インポートjava.net.ServerSocket;
インポートjava.net.Socket;
java.util.Scannerをインポートします。

public class Server_Test extends Thread { ServerSocket server = null; ソケットソケット= null; public Server_Test(int port){ try { server = new ServerSocket(port); } catch(IOException e){ e.printStackTrace(); } } @Override public void run(){










    super.run();
    try{
        System.out.println("wait client connect...");
        socket = server.accept();
        new sendMessThread().start();//连接并返回socket后,再启用发送消息线程
        System.out.println(socket.getInetAddress().getHostAddress()+"SUCCESS TO CONNECT...");
        InputStream in = socket.getInputStream();
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len=in.read(buf))!=-1){
            System.out.println("client saying: "+new String(buf,0,len));
        }

    }catch (IOException e){
        e.printStackTrace();
    }
}


class sendMessThread extends Thread{
    @Override
    public void run(){
        super.run();
        Scanner scanner=null;
        OutputStream out = null;
        try{
            if(socket != null){
                scanner = new Scanner(System.in);
                out = socket.getOutputStream();
                String in = "";
                do {
                    in = scanner.next();
                    out.write(("server saying: "+in).getBytes());
                    out.flush();//清空缓存区的内容
                }while (!in.equals("q"));
                scanner.close();
                try{
                    out.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }

    }

}

//函数入口
public static void main(String[] args) {
    Server_Test server = new Server_Test(1234);
    server.start();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
3.2クライアントコード

インポートjava.io.IOException;
インポートjava.io.InputStream;
インポートjava.io.OutputStream;
インポートjava.net.Socket;
インポートjava.net.UnknownHostException;
java.util.Scannerをインポートします。

パブリッククラスクライアントはスレッドを拡張します{

//定义一个Socket对象
Socket socket = null;

public Client(String host, int port) {
    try {
        //需要服务器的IP地址和端口号,才能获得正确的Socket对象
        socket = new Socket(host, port);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

@Override
public void run() {
    //客户端一连接就可以写数据个服务器了
    new sendMessThread().start();
    super.run();
    try {
        // 读Sock里面的数据
        InputStream s = socket.getInputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = s.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

//往Socket里面写数据,需要新开一个线程
class sendMessThread extends Thread{
    @Override
    public void run() {
        super.run();
        //写操作
        Scanner scanner=null;
        OutputStream os= null;
        try {
            scanner=new Scanner(System.in);
            os= socket.getOutputStream();
            String in="";
            do {
                in=scanner.next();
                os.write((""+in).getBytes());
                os.flush();
            } while (!in.equals("bye"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        scanner.close();
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//函数入口
public static void main(String[] args) {
    //需要服务器的正确的IP地址和端口号
    Client clientTest=new Client("127.0.0.1", 1234);
    clientTest.start();
}

}

おすすめ

転載: blog.csdn.net/qq_35577329/article/details/105748383