这里是关于“Java网络通信技术之简单聊天小程序”的完整攻略。
简介
本篇攻略将为大家介绍如何使用Java网络通信技术开发简单聊天小程序。
聊天小程序主要由客户端和服务端两个部分组成,它们之间通过网络通信进行交互。在Java中,可以使用Socket实现网络通信。
下面我们将由客户端和服务端两个方面详细讲解。
客户端
客户端主要负责向服务端发送信息,并接收服务端的返回信息。
首先,客户端需要创建一个Socket对象,指定服务器IP地址和端口号:
Socket socket = new Socket("127.0.0.1", 8888);
通过这个Socket对象,我们就可以向服务器发送信息:
OutputStream os = socket.getOutputStream();
String message = "Hello Server!";
os.write(message.getBytes());
同样,客户端也可以通过Socket对象接收服务端返回的信息:
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = is.read(buffer);
String message = new String(buffer, 0, length);
System.out.println("Received message: " + message);
服务端
服务端主要负责接收客户端发送来的信息,并向客户端返回信息。
服务端需要创建一个ServerSocket对象,并指定端口号:
ServerSocket serverSocket = new ServerSocket(8888);
服务端可以通过ServerSocket对象接收客户端的连接请求:
Socket socket = serverSocket.accept();
一旦服务端接收到客户端的连接请求,就可以通过Socket对象接收客户端发送过来的信息:
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = is.read(buffer);
String message = new String(buffer, 0, length);
System.out.println("Received message: " + message);
服务端也可以通过Socket对象向客户端发送信息:
OutputStream os = socket.getOutputStream();
String message = "Hello Client!";
os.write(message.getBytes());
示例说明
下面给出两个代码示例来帮助理解:
示例一
客户端
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
String message = "Hello Server!";
os.write(message.getBytes());
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = is.read(buffer);
String response = new String(buffer, 0, length);
System.out.println("Received message from server: " + response);
socket.close();
}
}
服务端
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = is.read(buffer);
String message = new String(buffer, 0, length);
System.out.println("Received message from client: " + message);
OutputStream os = socket.getOutputStream();
String response = "Hello Client!";
os.write(response.getBytes());
socket.close();
serverSocket.close();
}
}
示例二
客户端
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
String message = "How are you?";
os.write(message.getBytes());
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = is.read(buffer);
String response = new String(buffer, 0, length);
System.out.println("Received message from server: " + response);
socket.close();
}
}
服务端
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = is.read(buffer);
String message = new String(buffer, 0, length);
System.out.println("Received message from client: " + message);
OutputStream os = socket.getOutputStream();
String response = "I'm fine!";
os.write(response.getBytes());
socket.close();
serverSocket.close();
}
}
以上就是关于Java网络通信技术之简单聊天小程序的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java网络通信技术之简单聊天小程序 - Python技术站