Java TCP and UDP programming, javaTCPUDP Programming

Source: Internet
Author: User

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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.