UDP 中的服务器-客户端
Server-Client in UDP
我的数据报简单客户端服务器程序正在提供 error.Client 从服务器获取信息,服务器也从客户端获取信息。
这是我的服务器:
package udp;
import java.net.*;
public class DatagramServer implements Runnable {
DatagramPacket pack;
DatagramSocket sock;
DatagramServer() {
new Thread(this).start();
}
public void run() {
try {
while (true) {
recieve();
send();
}
} catch (Exception e) {
System.out.println(e);
}
}
public void send() throws Exception {
byte data[] = "This is a \n datagram packet".getBytes();
InetAddress add = InetAddress.getByName("localhost");
pack = new DatagramPacket(data, data.length, add, 8000);
sock = new DatagramSocket();
sock.send(pack);
sock.close();
}
public void recieve() throws Exception {
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
sock = new DatagramSocket(8000);
sock.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
}
public static void main(String[] args) {
new DatagramServer();
}
}
这是我的客户:
package udp;
import java.net.*;
public class DatagramClient {
DatagramPacket pack;
DatagramSocket sock;
DatagramClient() {
Thread t1 = new Thread() {
public void run() {
while (true) {
try {
receive();
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
}
}
};
t1.start();
Thread t2 = new Thread() {
public void run() {
while (true) {
try {
send();
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
}
}
};
t2.start();
}
public void receive() throws Exception {
byte data[] = new byte[1000];
pack = new DatagramPacket(data, data.length);
sock = new DatagramSocket(8000);
sock.receive(pack);
System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
System.out.println("Port::" + pack.getPort());
sock.close();
}
public void send() throws Exception {
String s = "KOLI SDF DFEF XFFFSS";
byte[] b = new byte[1000];
b = s.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
sock= new DatagramSocket(8000);
sock.send(dp);
}
public static void main(String[] args) {
new DatagramClient();
}
}
我收到如下错误:
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bindBUILD STOPPED (total time: 4 seconds)
我该怎么办?
您的客户端和服务器存在一些问题,例如在不同的线程中打开相同的端口 8000,因此我已在以下代码中修复了这些问题。
客户
package udp;
import java.net.*;
public class DatagramClient {
DatagramPacket pack;
DatagramSocket sock;
DatagramClient() {
Thread t1 = new Thread() {
public void run() {
while (true) {
try {
receive();
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
}
}
};
t1.start();
}
public void receive() throws Exception {
byte data[] = new byte[1000];
pack = new DatagramPacket(data, data.length);
sock = new DatagramSocket(8000);
sock.receive(pack);
System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
System.out.println("Port::" + pack.getPort());
sock.close();
}
public void send() throws Exception {
String s = "KOLI SDF DFEF XFFFSS";
byte[] b = new byte[1000];
b = s.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
sock= new DatagramSocket(8000);
sock.send(dp);
}
public static void main(String[] args) {
new DatagramClient();
}
}
服务器
package udp;
import java.net.*;
public class DatagramServer implements Runnable {
DatagramPacket pack;
DatagramSocket sock;
DatagramServer() {
new Thread(this).start();
}
public void run() {
try {
while (true) {
//recieve();
send();
}
} catch (Exception e) {
System.out.println(e);
}
}
public void send() throws Exception {
byte data[] = "This is a \n datagram packet".getBytes();
InetAddress add = InetAddress.getByName("localhost");
pack = new DatagramPacket(data, data.length, add, 8000);
sock = new DatagramSocket();
sock.send(pack);
sock.close();
}
public void recieve() throws Exception {
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
sock = new DatagramSocket(8000);
sock.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
}
public static void main(String[] args) {
new DatagramServer();
}
}
问题是您的服务器代码在每次调用 receive () 时都试图绑定到 8000 端口。在 while(true) 循环中重复调用 receive。所以它一次又一次地尝试绑定到同一个端口。
要修复它,请移除 UDP 套接字实例化,即
sock = new DatagramSocket(8000);
从 receive 方法开始并将其放在 while 循环开始之前。
我的数据报简单客户端服务器程序正在提供 error.Client 从服务器获取信息,服务器也从客户端获取信息。
这是我的服务器:
package udp;
import java.net.*;
public class DatagramServer implements Runnable {
DatagramPacket pack;
DatagramSocket sock;
DatagramServer() {
new Thread(this).start();
}
public void run() {
try {
while (true) {
recieve();
send();
}
} catch (Exception e) {
System.out.println(e);
}
}
public void send() throws Exception {
byte data[] = "This is a \n datagram packet".getBytes();
InetAddress add = InetAddress.getByName("localhost");
pack = new DatagramPacket(data, data.length, add, 8000);
sock = new DatagramSocket();
sock.send(pack);
sock.close();
}
public void recieve() throws Exception {
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
sock = new DatagramSocket(8000);
sock.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
}
public static void main(String[] args) {
new DatagramServer();
}
}
这是我的客户:
package udp;
import java.net.*;
public class DatagramClient {
DatagramPacket pack;
DatagramSocket sock;
DatagramClient() {
Thread t1 = new Thread() {
public void run() {
while (true) {
try {
receive();
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
}
}
};
t1.start();
Thread t2 = new Thread() {
public void run() {
while (true) {
try {
send();
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
}
}
};
t2.start();
}
public void receive() throws Exception {
byte data[] = new byte[1000];
pack = new DatagramPacket(data, data.length);
sock = new DatagramSocket(8000);
sock.receive(pack);
System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
System.out.println("Port::" + pack.getPort());
sock.close();
}
public void send() throws Exception {
String s = "KOLI SDF DFEF XFFFSS";
byte[] b = new byte[1000];
b = s.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
sock= new DatagramSocket(8000);
sock.send(dp);
}
public static void main(String[] args) {
new DatagramClient();
}
}
我收到如下错误:
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bindBUILD STOPPED (total time: 4 seconds)
我该怎么办?
您的客户端和服务器存在一些问题,例如在不同的线程中打开相同的端口 8000,因此我已在以下代码中修复了这些问题。
客户
package udp;
import java.net.*;
public class DatagramClient {
DatagramPacket pack;
DatagramSocket sock;
DatagramClient() {
Thread t1 = new Thread() {
public void run() {
while (true) {
try {
receive();
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
}
}
};
t1.start();
}
public void receive() throws Exception {
byte data[] = new byte[1000];
pack = new DatagramPacket(data, data.length);
sock = new DatagramSocket(8000);
sock.receive(pack);
System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
System.out.println("Port::" + pack.getPort());
sock.close();
}
public void send() throws Exception {
String s = "KOLI SDF DFEF XFFFSS";
byte[] b = new byte[1000];
b = s.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
sock= new DatagramSocket(8000);
sock.send(dp);
}
public static void main(String[] args) {
new DatagramClient();
}
}
服务器
package udp;
import java.net.*;
public class DatagramServer implements Runnable {
DatagramPacket pack;
DatagramSocket sock;
DatagramServer() {
new Thread(this).start();
}
public void run() {
try {
while (true) {
//recieve();
send();
}
} catch (Exception e) {
System.out.println(e);
}
}
public void send() throws Exception {
byte data[] = "This is a \n datagram packet".getBytes();
InetAddress add = InetAddress.getByName("localhost");
pack = new DatagramPacket(data, data.length, add, 8000);
sock = new DatagramSocket();
sock.send(pack);
sock.close();
}
public void recieve() throws Exception {
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
sock = new DatagramSocket(8000);
sock.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
}
public static void main(String[] args) {
new DatagramServer();
}
}
问题是您的服务器代码在每次调用 receive () 时都试图绑定到 8000 端口。在 while(true) 循环中重复调用 receive。所以它一次又一次地尝试绑定到同一个端口。
要修复它,请移除 UDP 套接字实例化,即
sock = new DatagramSocket(8000);
从 receive 方法开始并将其放在 while 循环开始之前。