java实现socket完整代码
时间: 2023-09-18 15:05:37 浏览: 93
java socket 源码
### 回答1:
好的,以下是 Java 实现 socket 的完整代码:
服务器端代码:
```
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received: " + inputLine);
out.println(inputLine);
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
```
客户端代码:
```
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket clientSocket = null;
try {
clientSocket = new Socket("localhost", 4444);
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("Server: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
clientSocket.close();
}
}
```
这是一个简单的 socket 代码,其中客户端可以将信息发送到服务器端,服务器端可以接收并返回客户端的信息。
### 回答2:
下面是一个使用Java实现Socket网络通信的完整代码示例:
```java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
// 创建ServerSocket对象,并指定监听的端口号
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("等待客户端连接...");
// 监听客户端请求,返回一个Socket对象
Socket socket = serverSocket.accept();
System.out.println("客户端已连接...");
// 创建输入流和输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
// 读取客户端发送的数据
String request = reader.readLine();
System.out.println("客户端请求:" + request);
// 向客户端发送数据
writer.println("Hello, Client!");
writer.flush();
// 关闭流和Socket连接
reader.close();
writer.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
```java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
// 创建Socket对象,并指定服务器的IP地址和端口号
Socket socket = new Socket("localhost", 8888);
// 创建输入流和输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
// 向服务器发送数据
writer.println("Hello, Server!");
writer.flush();
// 读取服务器返回的数据
String response = reader.readLine();
System.out.println("服务器响应:" + response);
// 关闭流和Socket连接
reader.close();
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码实现了一个简单的Socket通信,服务器端监听指定端口,等待客户端连接;客户端连接服务器,并向服务器发送数据。服务器接收到数据后,返回一个响应给客户端。代码中使用了Java提供的Socket类、ServerSocket类、InputStreamReader类、OutputStreamWriter类等来实现Socket通信的功能。
### 回答3:
下面是一个简单的Java实现Socket的完整代码示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketExample {
public static void main(String[] args) {
// 启动服务器
new Thread(new Server()).start();
// 启动客户端
new Thread(new Client()).start();
}
// 服务器
static class Server implements Runnable {
@Override
public void run() {
try {
// 创建服务器Socket,监听指定端口
ServerSocket serverSocket = new ServerSocket(8888);
// 等待客户端连接
Socket clientSocket = serverSocket.accept();
System.out.println("客户端已连接");
// 创建输入流
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// 创建输出流
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
// 读取客户端发送的数据
String message = in.readLine();
System.out.println("收到客户端消息:" + message);
// 发送响应给客户端
out.println("已收到消息");
// 关闭连接
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客户端
static class Client implements Runnable {
@Override
public void run() {
try {
// 创建客户端Socket,连接服务器
Socket socket = new Socket("localhost", 8888);
// 创建输入流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 创建输出流
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 向服务器发送消息
out.println("Hello, Server!");
// 读取服务器响应
String response = in.readLine();
System.out.println("服务器响应:" + response);
// 关闭连接
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
此代码实现了一个简单的Socket通信示例:启动一个服务器和一个客户端。服务器等待客户端连接,客户端连接后向服务器发送消息,服务器接收并发送响应给客户端,然后关闭连接。通过运行此代码,可以在控制台看到客户端和服务器之间的消息交互。
阅读全文