# 前言
突然看到这个东西,简单的记录了一下,一些基础的计算机网络原理的知识这里不在记录,毕竟也是比较简单,不太熟悉的可以去翻一下之前写的 Python 网络化编程 Python 网络化编程 - Python | Clown の Blog = (xcu.icu),本篇主要就记录一下,篇幅不会太长
# Socket 连接
网络化编程避不开的一个话题 ---Socket,或许这篇命名为 Socket 比较好,但是因为一些强迫症的小原因,整个分类我还是习惯这样命名,而且 Socket 编程的覆盖面太大,对于本篇更侧重的是使用 java 来实现网络通信
在 java 中对于 Socket 的支持有这一套完整的封装,使用还是很方便的,当然 Java 也是需要一个客户端一个服务端来进行连接,先看一个简单的例子
package JavaWeb; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
/** | |
* @BelongsProject: study_java | |
* @BelongsPackage: JavaWeb | |
* @Author: Clown | |
* @CreateTime: 2023-08-10 09:54 | |
*/ | |
public class ServerTest { | |
public static void main(String[] args) { | |
try { | |
while (true){ | |
ServerSocket serverSocket = new ServerSocket(2444); | |
System.out.println("Loading·····"); | |
Socket socket = serverSocket.accept(); // 等待连接 | |
System.out.println("Connection succeeded!,ip:"+socket.getInetAddress().getHostAddress()); | |
serverSocket.close(); // 关闭连接,防止端口占用 | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
上面的是服务端内容
package JavaWeb; | |
import java.io.IOException; | |
import java.net.Socket; | |
/** | |
* @BelongsProject: study_java | |
* @BelongsPackage: JavaWeb | |
* @Author: Clown | |
* @CreateTime: 2023-08-10 09:55 | |
*/ | |
public class ClientTest { | |
public static void main(String[] args) { | |
try { | |
Socket socket = new Socket("127.0.0.1",2444); | |
System.out.println("Successful!"); | |
} catch (IOException e) { | |
System.out.println("Failure!!!"); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
这里可以看见就建立了一个 tcp 连接,当然每次连接完成后都会中断等待下一次连接
# Socket 数据传输
前面通过 Socket 来进行连接,当然我们可以使用对应的 IO 流来通过 Socket 来进行数据的传输,这里简单改写一下
package JavaWeb.Socket; | |
import java.io.*; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
/** | |
* @BelongsProject: study_java | |
* @BelongsPackage: JavaWeb.Socket | |
* @Author: Clown | |
* @CreateTime: 2023-08-14 10:28 | |
*/ | |
public class ServerDataTest { | |
public static void main(String[] args) throws IOException { | |
ServerSocket serverSocket = new ServerSocket(2444); | |
System.out.println("Loading·····"); | |
Socket socket = serverSocket.accept(); // 等待连接 | |
System.out.println("Connection succeeded!,ip:"+socket.getInetAddress().getHostAddress()); | |
System.out.println("ClientData: "); | |
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); | |
System.out.println(new BufferedReader(inputStreamReader).readLine()); | |
socket.close(); | |
} | |
} |
package JavaWeb.Socket; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.net.Socket; | |
/** | |
* @BelongsProject: study_java | |
* @BelongsPackage: JavaWeb.Socket | |
* @Author: Clown | |
* @CreateTime: 2023-08-14 10:29 | |
*/ | |
public class ClientDataTest { | |
public static void main(String[] args) { | |
try { | |
Socket socket = new Socket("127.0.0.1",2444); | |
System.out.println("Successful!"); | |
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); | |
outputStreamWriter.write("hello\n"); | |
outputStreamWriter.flush(); | |
socket.close(); | |
} catch (IOException e) { | |
System.out.println("Failure!!!"); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
这里可以看到客户端中将数据 Hello 发送到了服务端,当然数据的传输并不是单项的,服务端向客户端发送数据也很简单
package JavaWeb.Socket; | |
import java.io.*; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
/** | |
* @BelongsProject: study_java | |
* @BelongsPackage: JavaWeb.Socket | |
* @Author: Clown | |
* @CreateTime: 2023-08-14 10:28 | |
*/ | |
public class ServerDataTest { | |
public static void main(String[] args) throws IOException { | |
ServerSocket serverSocket = new ServerSocket(2444); | |
System.out.println("Loading·····"); | |
Socket socket = serverSocket.accept(); // 等待连接 | |
System.out.println("Connection succeeded!,ip:"+socket.getInetAddress().getHostAddress()); | |
System.out.println("ClientData: "); | |
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); | |
System.out.println(new BufferedReader(inputStreamReader).readLine()); | |
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); | |
outputStreamWriter.write("hello!\n"); | |
outputStreamWriter.flush(); | |
socket.close(); | |
} | |
} |
package JavaWeb.Socket; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.net.Socket; | |
/** | |
* @BelongsProject: study_java | |
* @BelongsPackage: JavaWeb.Socket | |
* @Author: Clown | |
* @CreateTime: 2023-08-14 10:29 | |
*/ | |
public class ClientDataTest { | |
public static void main(String[] args) { | |
try { | |
Socket socket = new Socket("127.0.0.1",2444); | |
System.out.println("Successful!"); | |
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); | |
outputStreamWriter.write("hello\n"); | |
outputStreamWriter.flush(); | |
System.out.println("Loading..."); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
System.out.println(bufferedReader.readLine()); | |
socket.close(); | |
} catch (IOException e) { | |
System.out.println("Failure!!!"); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
当然上面的示例中都是最简单的利用,目的还是为了了解一下通过 Socket 来传输数据,具体有关细节的实现还有很多,这里不在赘述