1. 使用TCP协议实现服务端与客户端之间的网络通信
服务端TCPServer代码如下:
package cn.tu.tcp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//TCP服务端
public class TCPServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader reader = null;
try {
serverSocket = new ServerSocket(1001);
System.out.println("TCP服务已启动......");
//接收客户端的请求 (套接字是两台机器之间通讯的端点)
socket = serverSocket.accept();
//获得套接字的输入流
//流 -> 服务器 (对于服务器来说是输入流)
InputStream inputStream = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
while (true){
//客户端要使用writer.newLine();来表示一条消息输入完成
String line = reader.readLine();
//从套接字中获得InetAddress
InetAddress address = socket.getInetAddress();
//客户端的IP
String hostAddress = address.getHostAddress();
//如果服务端从客户端接收到的是 exit 则会中断连接
if("exit".equals(line)){
break;
}
System.out.println(hostAddress + "发送的数据是:" + line);
}
System.out.println("断开TCP服务!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(reader!=null){
reader.close();
}
if(socket!=null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}*
客户端TCPClient代码如下:
package cn.tu.tcp;
import java.io.*;
import java.net.Socket;
//TCP客户端
public class TCPClient {
public static void main(String[] args) {
Socket socket = null;
BufferedWriter writer = null;
BufferedReader reader = null;
//UDP服务器的IP:192.168.0.103 此处服务器在本机
try {
socket = new Socket("192.168.0.103", 1001);
//获得套接字的输出流
//客户端 -> 流 (对于客户端来说是输出流)
OutputStream outputStream = socket.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
//控制台 -> 客户端
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("发送消息:");
while (true) {
String line = reader.readLine();
writer.write(line);
//产生新行
//(因为服务端使用String line = reader.readLine();所以这一行不能少,否则服务端会认为客户端还没有输入完)
writer.newLine();
writer.flush();
//输入exit表示退出客户端
if ("exit".equals(line)) {
break;
}
}
System.out.println("退出TCP客户端!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
if(reader!=null){
reader.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意:记得先启动服务端,再运行客户端
TCP服务端运行效果图:
TCP客户端运行效果图:
2. 使用UDP协议实现服务端与客户端之间的网络通信
服务端UDPServer代码如下:
package cn.tu.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//UDP服务器端
public class UDPServer {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(1000);
byte[] bytes = new byte[1024];
//数据报
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
System.out.println("UDP服务器已启动......");
while (true){
//接收来自客户端的数据报
socket.receive(packet);
//从接收的数据报中获得InetAddress
InetAddress address = packet.getAddress();
//客户端的IP
String hostAddress = address.getHostAddress();
//从接收的数据报中获得数据
byte[] data = packet.getData();
//从接收的数据报中获得数据的长度
int len = packet.getLength();
String str = new String(data, 0, len);
System.out.println(hostAddress + "发送的数据是:" + str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}
客户端UDPClient代码如下:
package cn.tu.udp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//UDP客户端
public class UDPClient {
public static void main(String[] args) {
DatagramSocket socket = null;
BufferedReader reader = null;
try {
socket = new DatagramSocket();
//String str = "世界你好";
//byte[] bytes = str.getBytes();
//控制台 -> 客户端
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("发送消息:");
String line = null;
while ((line = reader.readLine()) != null) {
//输入exit表示退出客户端
if("exit".equals(line)){
break;
}
byte[] bytes = line.getBytes();
//创建数据报
//UDP服务器的IP:192.168.0.103 此处服务器在本机
DatagramPacket packet = new DatagramPacket(bytes, bytes.length,
InetAddress.getByName("192.168.0.103"), 1000);
//向服务器发送数据报
socket.send(packet);
}
System.out.println("退出UDP客户端!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (socket != null) {
socket.close();
}
}
}
}
注意:记得先启动服务端,再运行客户端
UDP服务端运行效果图:
UDP客户端运行效果图: