|
Recently read a book, which contains an example on network programming, as follows:
1. Server-side DatagramServer1:
import java.io. *;
import java.net. *;
import java.util. *;
/ **
* <p> Title: </ p>
*
* <p> Description: </ p>
*
* <p> Copyright: Copyright (c) 2017 </ p>
*
* <p> Company: </ p>
*
* @author not attributable
* @version 1.0
* /
public class DatagramServer1 extends Thread {
DatagramSocket socket;
DatagramPacket packet;
int client;
public DatagramServer1 (DatagramSocket socket, DatagramPacket packet, int client) {
this.socket = socket;
this.packet = packet;
this.client = client;
this.start ();
}
public void run () {
try {
byte [] received = packet.getData ();
System.out.println ("Received from clinet =" + client + "" + new String (received));
String response = "You are client" + client;
byte [] buf = received;
InetAddress address = packet.getAddress ();
int port = packet.getPort ();
packet = new DatagramPacket (buf, buf.length, address, port);
socket.send (packet);
} catch (IOException ex) {
ex.printStackTrace ();
}
}
public static void main (String [] args) throws Exception {
DatagramSocket socket = new DatagramSocket (4444);
System.out.println ("Server Started");
byte [] buf = new byte [200];
int i = 1;
try {
while (true) {
DatagramPacket packet = new DatagramPacket (buf, buf.length);
System.out.println ("Waiting for client =" + i);
socket.receive (packet);
new DatagramServer1 (socket, packet, i);
i ++;
}
} catch (Exception ex) {
ex.printStackTrace ();
} finally {
socket.close ();
}
}
}
Client
import java.io. *;
import java.net. *;
import java.util. *;
/ **
* <p> Title: </ p>
*
* <p> Description: </ p>
*
* <p> Copyright: Copyright (c) 2017 </ p>
*
* <p> Company: </ p>
*
* @author not attributable
* @version 1.0
* /
public class DatagramClient {
public DatagramClient () {
}
public static void main (String [] args) throws IOException {
if (args.length! = 2) {
System.out.println ("Usage:" + "java DatagramClient <hostname> <message>");
return;
}
DatagramSocket socket = new DatagramSocket ();
System.out.println ("My message =" + args [1]);
byte [] buf = new byte [200];
buf = args [1] .getBytes ();
InetAddress address = InetAddress.getByName (args [0]);
DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4444);
socket.send (packet);
packet = new DatagramPacket (buf, buf.length);
socket.receive (packet);
String received = new String (packet.getData ());
System.out.println ("The server returns my message:" + received);
socket.close ();
}
}
The problem is:
For example: java DatagramClient hostName abc (hostName is the host name)
The client output 3 characters abc to the server for the first time, both the server and the client display normally
Second time: java DatagramClient hostName 12, the client is sincere, but the server shows 12c
The server program does not seem to clear the tone! !! ?
Related statement: byte [] received = packet.getData ();
System.out.println ("Received from clinet =" + client + "" + new String (received));
As the question, I am very confused about this problem soon after I started, please ask prawn ~ Thank you! !! |
|