本文共 3825 字,大约阅读时间需要 12 分钟。
我在Java中使用UDP协议在客户端和服务器之间进行通信时遇到问题 . 我制作了Client和Server类以及两个SingleCom负责发送和接收数据包的类 . 我试图测试连接(我在一台计算机和另一台服务器上运行客户端;两者都在同一端口上创建DatagramSocket)只需从客户端发送“START”字符串到服务器,然后该服务器应在控制台上打印字符串并发送返回“ACCEPTED”,但客户端和服务器都在其控制台上打印任何内容 . 有谁能告诉我发生了什么,为什么?下面我展示了简化的类(我在其他数据包中分离了服务器和客户端):
//Client:
public class Client
{
protected static InetAddress serverAddress;
protected static InetAddress clientAdr;
protected static int port;
protected static SingleCom connection;
public static final int PACKET_SIZE = 1024;
public static void main(String[] args) throws IOException, InterruptedException
{
//get server address and port typed from console
load();
connection = new SingleCom();
connection.initilize();
Thread privcastTread = new Thread(connection);
privcastTread.start();
//sending the simplest packet
connection.sendPacket("START", serverAddress);
}
public static synchronized void handlePacket(DatagramPacket packet, String msg) throws IOException
{
if (msg.startsWith("ACCEPTED"))
{
System.out.println("Accepted");
}
}
}
//SingleCom for Client
public class SingleCom implements Runnable
{
DatagramSocket udpSocket;
@Override
public void run()
{
try
{
byte[] buffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
udpSocket.receive(packet);
String msg = new String(buffer, 0, packet.getLength());
Client.handlePacket(packet, msg);
packet.setLength(buffer.length);
}
}
catch (SocketException e)
{
System.exit(1);
}
catch (IOException e)
{
System.exit(1);
}
}
public void initilize() throws IOException
{
udpSocket = new DatagramSocket(Client.port, InetAddress.getLocalHost());
}
public void sendPacket(String text, InetAddress target) throws IOException
{
byte[] sendedMsg = text.getBytes();
DatagramPacket sendedPacket = new DatagramPacket(sendedMsg, sendedMsg.length, target, Client.port);
udpSocket.send(sendedPacket);
}
public void sendPacket(String text) throws IOException
{
sendPacket(text, Client.serverAddress)
}
public void sendPacket(byte[] content) throws IOException
{
//byte[] sendedMsg = text.getBytes();
DatagramPacket sendedPacket = new DatagramPacket(content, content.length, Client.serverAddress, Client.port);
udpSocket.send(sendedPacket);
}
}
//Server
public class Server
{
protected static InetAddress serverAdr;
protected static InetAddress clientAdr;
protected static SingleCom connection;
protected static int port;
protected static int speed; //in KB/s
private static FileOutputStream fos;
public static void main(String[] args) throws IOException
{
load();
connection = new SingleCom();
connection.initilize();
Thread privcastTread = new Thread(connection);
privcastTread.start();
}
public static synchronized void handlePacket(DatagramPacket pakiet, String msg) throws IOException
{
String[] pieces;
if (msg.startsWith("START"))
{
System.out.println(pieces[0]);
connection.sendPacket("ACCEPTED", clientAdr);
}
}
}
//connection for Server
public class ServerCom implements Runnable
{
DatagramSocket udpSocket;
@Override
public void run()
{
try
{
byte[] buffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
udpSocket.receive(packet);
String msg = new String(buffer, 0, packet.getLength());
Server.handlePacket(packet, msg);
packet.setLength(buffer.length);
}
}
catch (SocketException e)
{
System.exit(1);
}
catch (IOException e)
{
System.exit(1);
}
}
public void initilize() throws IOException
{
udpSocket = new DatagramSocket(Server.port, InetAddress.getLocalHost());
}
public void sendPacket(String text, InetAddress target) throws IOException
{
byte[] sendedMsg = text.getBytes();
DatagramPacket sendedPacket = new DatagramPacket(sendedMsg, sendedMsg.length, target, Server.port);
udpSocket.send(sendedPacket);
}
public void sendPacket(String text) throws IOException
{
sendPacket(text, Server.clientAdr);
}
}
转载地址:http://jkyhp.baihongyu.com/