[英]Server client simple program
So I try to create a simple server client application. 因此,我尝试创建一个简单的服务器客户端应用程序。
I have the server, which multicast messages to the subscribed clients. 我有服务器,该服务器将消息多播到订阅的客户端。
The clients can obviously subscribe or unsubscribe. 客户显然可以订阅或取消订阅。
Here is the server side code for the socket 这是套接字的服务器端代码
try // create DatagramSocket for sending and receiving packets
{
socket = new DatagramSocket( 6666 );
} // end try
catch ( SocketException socketException )
{
System.exit( 1 );
} // end catch
I have also created a thread which waits for incoming pockets 我还创建了一个线程,等待传入的口袋
while(true)
{
try // receive packet, display contents, return copy to client
{
byte[] data = new byte[ 100 ]; // set up packet
DatagramPacket receivePacket =
new DatagramPacket( data, data.length );
socket.receive( receivePacket ); // wait to receive packet
String Message = receivePacket.getData().toString();
.........
Here is the client side 这是客户端
try {
socket = new DatagramSocket();
} catch (SocketException ex) {
displayArea.append( ex + "\n" );
}
Here I try to send the packet 在这里我尝试发送数据包
byte[] data = message.getBytes(); // convert to bytes
try {
// create sendPacket
DatagramPacket sendPacket = new DatagramPacket( data,
data.length, InetAddress.getLocalHost(), 6666 );
} catch (UnknownHostException ex) {
displayArea.append( ex + "\n" );
}
Well, the problem is that I don't think the packet reaches the server, I know the thread is waiting for the packet, I can see in debug that it does reaches the socket.receive
part, however, nothing happens. 好吧,问题在于我不认为数据包到达服务器,我知道线程正在等待数据包,我可以在调试中看到它确实到达了
socket.receive
部分,但是什么也没发生。
I'm using the client and server on the same computer. 我在同一台计算机上使用客户端和服务器。
Any idea what am I doing wrong? 知道我在做什么错吗?
Thanks. 谢谢。
byte[] data = message.getBytes(); // convert to bytes
try {
DatagramPacket sendPacket =
new DatagramPacket( data, data.length, InetAddress.getLocalHost(), 6666 );
socket.send( sendPacket ); //<<<<<<<<<<<<<<<<<<< REQUIRED
}
catch( UnknownHostException ex ) {
displayArea.append( ex + "\n" );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.