博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java数据包只能发送不能接受,为什么我不能使用UDP在客户端和服务器之间发送数据包...
阅读量:4955 次
发布时间:2019-06-12

本文共 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/

你可能感兴趣的文章
在Position:absolute下居中设置
查看>>
python递归及斐波那契数列
查看>>
设计模式概述
查看>>
python random使用生成随机字符串
查看>>
[2017.02.24] 学习《正则表达式必知必会》
查看>>
两个已排序的整型数组,求交集,最快算法
查看>>
福建工程学院第十四届ACM程序设计大赛 - E - 外传:小晋逃生记
查看>>
【瞎搞】HDU 3257 Hello World!
查看>>
利用运行时给分类添加属性
查看>>
利用运行时,查看一个类的所有子类
查看>>
python Gunicorn
查看>>
maven package,clean,install,compile命令
查看>>
Python 排错UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法
查看>>
洛谷 P5276 模板题(uoi)
查看>>
ORACLE之PACKAGE-包、存储过程、函数
查看>>
select remove option safari 兼容
查看>>
常用的工具类所属的包
查看>>
设计模式-观察者模式(Observer Pattern)
查看>>
JVM 内部运行线程介绍
查看>>
2. DVWA亲测文件包含漏洞
查看>>