嗨,我要做一个客户端服务器的DataGram通信,我有一个小问题。发送-接收方法运行良好,但是如果我试图从客户端发送安全时间,服务器将不会再次接收整个消息:
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 ();
}
}
}
}发布于 2016-05-09 18:27:55
你必须移动第一行:
DatagramPacket dgp = new DatagramPacket (data, data.length);进入“时间”循环。"dgp“是在发送服务器应答时在while循环结束时使用12字节的缓冲区长度构造的。
https://stackoverflow.com/questions/37122648
复制相似问题