Java网络编程 Socket套接字

注意
本文最后更新于 2024-05-23,文中内容可能已过时。

Socket

https://blog.meowrain.cn/api/i/2024/01/11/uhjt1b-3.webp

https://blog.meowrain.cn/api/i/2024/01/11/ukd5w8-3.webp

https://blog.meowrain.cn/api/i/2024/01/11/ukgqn0-3.webp

Socket有两种编程方式

  • tcp编程
  • udp编程

https://blog.meowrain.cn/api/i/2024/01/11/um5tsz-3.webp

Java Socket建立连接

Server.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8080); // 8080 is the port number
            System.out.println("Server is running... waiting for client to connect");
            Socket socket = server.accept();// wait for client to connect
            System.out.println("Client connected" + socket.getInetAddress().getHostAddress());// get the ip address of the client
            server.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Cilent.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Cilent {
    public static void main(String[] args) {
    try(Socket socket = new Socket()) {
        socket.connect(new InetSocketAddress("localhost", 8080));
        System.out.println("Connected to server");
    }catch (IOException e) {
        e.printStackTrace(); //
    }
    }
}

通过Socket进行数据传输

SocketServer.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.meowrain.tcpserver;

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

public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8080);
        System.out.println("Server is running... waiting for client to connect");
        Socket socket = server.accept();
        System.out.println("Connet successfully");
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, readLen));
        }
        server.close();
    }
}

https://blog.meowrain.cn/api/i/2023/03/04/zkqquu-3.webp

SocketCilent.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.meowrain.tcpserver;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SocketTCP01Cilent {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 8080);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("Hello, I am client".getBytes());
        socket.close();
        System.out.println("Connet over");
    }
}

https://blog.meowrain.cn/api/i/2023/03/04/zkt9oo-3.webp

0%