0
Hy,我必须做一个客户端 - 服务器DataGram通信,我有一个小问题。发送-recive方法工作良好,但如果我尝试从客户端发送谢胜利时,服务器将不recive再整个邮件:DataGram客户端 - 服务器Java - 消息传输问题
Server Started and listening to the port 10000
Recive from client: Send me a datagram
Send to client: I am Server!
Recive from client: Send me a da -> HERE is the PROBLEM
Send to client: I am Server!
在TCP传输我kwnow,我要刷新缓冲区,但在DataGram中我需要做什么?
DGSServerT:
public class DGSServerT {
public static void main (String [] args) throws IOException {
final int port = 10000;
// Create a datagram socket bound to port 10000. Datagram packets sent from client programs arrive at this port.
DatagramSocket s = new DatagramSocket (port);
System.out.println("Server Started and listening to the port " + port);
// Create a byte array to hold data contents of datagram packet.
byte [] data = new byte [100];
// Create a DatagramPacket object that encapsulates a reference to the byte array and destination address information. The
// DatagramPacket object is not initialized to an address because it obtains that address from the client program.
DatagramPacket dgp = new DatagramPacket (data, data.length);
// Enter an infinite loop. Press Ctrl+C to terminate program.
while (true) {
// Receive a datagram packet from the client program.
s.receive (dgp);
// Display contents of datagram packet.
System.out.println ("Recive from client: " + new String (data));
InetAddress address = dgp.getAddress();
int clPort = dgp.getPort();
data = new String ("I am Server!").getBytes();
dgp = new DatagramPacket (data, data.length, address, clPort);
// Echo datagram packet back to client program.
s.send (dgp);
System.out.println ("Send to client: " + new String (data));
}
}
}
DGSClientT:
public class DGSClientT {
public static void main (String [] args) {
String host = "localhost";
// If user specifies a command-line argument, that argument represents the host name.
if (args.length == 1) {
host = args [0];
}
DatagramSocket s = null;
try {
s = new DatagramSocket();
// Create a byte array that will hold the data portion of a datagram packet's message
byte [] buffer = new String ("Send me a datagram").getBytes();
InetAddress ia = InetAddress.getByName (host);
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia, 10000);
// Send the datagram packet over the socket.
s.send (dgp);
System.out.println("Send to server: " + new String (dgp.getData()));
// Create a byte array to hold the response from the server program
byte [] buffer2 = new byte [100];
// Create a DatagramPacket object that specifies a buffer to hold the server program's response, the IP address of
// the server program's computer, and port number 10000.
DatagramPacket dgp2 = new DatagramPacket (buffer2, buffer.length, ia, 10000);
// Receive a datagram packet over the socket.
s.receive (dgp2);
// Print the data returned from the server program and stored in the datagram packet.
System.out.println ("Recive from Server: " + new String (dgp2.getData()));
} catch (IOException e) {
System.out.println (e.toString());
} finally {
if (s != null) {
s.close();
}
}
}
}
我在while循环中移动了这一行,但它不起作用...同样的问题... – Doro
我用你的帮助解决了:) ...除了你提到的那一行外,我还移动了“byte [] data =新字节[100];“在一段时间内,现在没关系。谢谢 :) – Doro