java数据包只能发送不能接受,为什么我不能使用UDP在客户端和服务器之间发送数据包...

我在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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值