如何用Java实现客户端与服务器一问一答
1. 建立TCP连接
- 客户端调用
Socket
类的构造方法建立与服务器端的连接。在构造方法中需要传入服务器端的IP地址和端口号,示例代码如下:
java
String serverHost = "127.0.0.1"; // 服务器IP地址
int serverPort = 8888; // 服务器端口号
Socket socket = null;
try {
socket = new Socket(serverHost, serverPort);
} catch (IOException e) {
e.printStackTrace();
}
- 服务器端通过调用
ServerSocket
类的构造方法监听指定的端口号。示例代码如下:
java
int port = 8888; // 监听端口号
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
2. 通过Input/OutputStream进行读写操作
在建立了TCP连接之后,客户端与服务器端可以通过流对象进行数据的传输。
- 客户端可以使用
socket
对象的getOutputStream()
方法获取输出流,通过输出流将数据发送给服务器端。示例代码如下:
java
OutputStream outputStream = socket.getOutputStream();
String message = "Hello, server!";
outputStream.write(message.getBytes());
- 服务器端可以使用
Socket
类的getInputStream()
方法获取输入流,通过输入流获取客户端发送的数据。示例代码如下:
java
InputStream inputStream;
try {
inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, StandardCharsets.UTF_8));
}
System.out.println("Message from client: " + sb);
} catch (IOException e) {
e.printStackTrace();
}
3. 示例1:客户端发送消息给服务端
在这个示例中,客户端将用户输入的字符串发送给服务器,服务器端将收到的字符串原样发送回客户端。
客户端代码
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
String serverHost = "127.0.0.1";
int serverPort = 8888;
Socket socket = null;
try {
socket = new Socket(serverHost, serverPort);
System.out.println("Connect to server: " + serverHost + ":" + serverPort);
OutputStream outputStream = socket.getOutputStream();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Input message: ");
String message = scanner.nextLine();
outputStream.write(message.getBytes(StandardCharsets.UTF_8));
if ("bye".equalsIgnoreCase(message)) {
break;
}
}
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端代码
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Server {
public static void main(String[] args) {
int port = 8888;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("Server listening on port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New connection accepted: " + socket.getInetAddress() + ":" + socket.getPort());
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
String message = new String(bytes, 0, len, StandardCharsets.UTF_8);
System.out.println("Message from client: " + message);
outputStream.write(message.getBytes(StandardCharsets.UTF_8));
}
inputStream.close();
outputStream.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4. 示例2:服务器向客户端发送消息
在这个示例中,客户端连接服务器后,服务器向客户端发送消息。
客户端代码
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Client {
public static void main(String[] args) {
String serverHost = "127.0.0.1";
int serverPort = 8888;
Socket socket = null;
try {
socket = new Socket(serverHost, serverPort);
System.out.println("Connect to server: " + serverHost + ":" + serverPort);
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
String message = new String(bytes, 0, len, StandardCharsets.UTF_8);
System.out.println("Message from server: " + message);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端代码
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Server {
public static void main(String[] args) {
int port = 8888;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("Server listening on port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New connection accepted: " + socket.getInetAddress() + ":" + socket.getPort());
OutputStream outputStream = socket.getOutputStream();
String message = "Welcome to the server!";
outputStream.write(message.getBytes(StandardCharsets.UTF_8));
outputStream.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上就是用Java实现客户端与服务器一问一答的教程了。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:教你怎么用java实现客户端与服务器一问一答 - Python技术站