Hy,我必须进行客户端 - 服务器DataGram通信,我遇到了一些问题。 send-recive方法运行良好,但如果我尝试从客户端发送secound时间,服务器将不会再次收到整个消息:
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传输中我知道我必须刷新缓冲区,但是在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 ();
}
}
}
}
答案 0 :(得分:0)
你必须移动第一行:
DatagramPacket dgp = new DatagramPacket (data, data.length);
进入“while”循环。 “dgp”在while循环结束时构造为缓冲区长度为12个字节,同时发送服务器答案。