Java TCP and UDP programming, javaTCPUDP Programming
TCP
Client:
Import java. io. bufferedReader; import java. io. inputStreamReader; import java. io. printWriter; import java.net. socket; public class MyClient {public static void main (String [] args) throws Exception {Socket socket = null; BufferedReader in = null; PrintWriter out = null; BufferedReader input = null; // The server socket requesting the specified ip address and port number = new Socket ("127.0.0.1", 3333); while (true) {in = new BufferedReader (new InputStr EamReader (socket. getInputStream (); out = new PrintWriter (socket. getOutputStream (), true); // receiving console input = new BufferedReader (new InputStreamReader (System. in); // out. println ("this is client info! "); String info = input. readLine (); out. println (info); String str = in. readLine (); System. out. println ("client display --" server information: "+ str);} // in. close (); // out. close ();}}
Server:
Import java. io. bufferedReader; import java. io. inputStreamReader; import java. io. printWriter; import java.net. serverSocket; import java.net. socket; public class MyServices {public static void main (String [] args) throws Exception {ServerSocket ss = null; Socket socket = null; BufferedReader in = null; PrintWriter out = null; bufferedReader input = null; // listening port 3333 ss = new ServerSocket (3333); // waiting for receiving client request socket = ss. accept (); while (true) {// get the input stream of the connection object in = new BufferedReader (new InputStreamReader (socket. getInputStream (); // obtain the input information of the client String str = in. readLine (); input = new BufferedReader (new InputStreamReader (System. in); System. out. println ("server display --> client input data:" + str); out = new PrintWriter (socket. getOutputStream (), true); // output data to the client // out. println ("hehe"); String info = input. readLine (); out. println (info);} // in. close (); // out. flush (); // out. close ();}}
UDP
Client:
Import java.net. datagramPacket; import java.net. datagramSocket; public class UDPClient {public static void main (String [] args) throws Exception {DatagramSocket ds = null; DatagramPacket dp = null; byte [] buf = new byte [1024]; ds = new DatagramSocket (3333); dp = new DatagramPacket (buf, 1024); // receives data and puts it into the datagram ds. receive (dp); // retrieve data from the datagram String info = new String (dp. getData (), 0, dp. getLength (); System. out. println ("received information:" + info );}}
Server:
Import java.net. datagramPacket; import java.net. datagramSocket; import java.net. inetAddress; public class UDPServer {public static void main (String [] args) throws Exception {DatagramSocket ds = null; DatagramPacket dp = null; // construct the sending object ds = new DatagramSocket (); // package the data --> into the datagram String info = "hello world! "; Dp = new DatagramPacket (info. getBytes (), info. length (), InetAddress. getByName ("localhost"), 3333); // sends a datagram ds. send (dp );}}