Two Java Network Programming demos (TCP + UDP)

Source: Internet
Author: User

TCP Server

package org.bruce.basic;import java.io.DataInputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class TestClient {public static void main(String[] args) {Socket s = null;try {s = new Socket("127.0.0.1", 8888);DataInputStream dis = new DataInputStream(s.getInputStream());System.out.println(dis.readUTF());dis.close();s.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

Tcp client

package org.bruce.basic;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class TestServer {public static void main(String[] args) {ServerSocket ss = null;try {ss = new ServerSocket(9999);} catch (IOException e) {e.printStackTrace();}try {while(true) {Socket s = ss.accept();System.out.println("a client connect!");DataOutputStream dos = new DataOutputStream(s.getOutputStream());dos.writeUTF("IPAddress = " + s.getInetAddress() + "Port = " + s.getPort());dos.close();s.close();}} catch (IOException e) {e.printStackTrace();}}}

UDP Server

Package Org. bruce. basic; import Java. io. ioexception; import java.net. datagrampacket; import java.net. datagramsocket; import java.net. socketexception; // UDP transmission is a connectionless data transmission method. Compared with TCP transmission, it is also a public class udpserver {public static void main (string [] ARGs) {// a piece of memory is opened in the form of a "byte array" to cache the received UDP packet byte [] buffer = new byte [1024]; // although the buffer size opened up is 1024 bytes, you can also set a cache space smaller than this value to receive data packets; new round rampacket dp = new round rampacket (buff Er, buffer. length); datagramsocket DS = NULL; try {// listen to UDP port 9999 DS = new datagramsocket (9999);} catch (socketexception e) {e. printstacktrace () ;}while (true) {try {// The receive () method is a obstructive method! DS. receive (DP);} catch (ioexception e) {e. printstacktrace ();} // you can learn from the next line of code a method for constructing string objects using Byte Arrays: system. out. println (new string (buffer, 0, DP. getlength ()));}}}

UDP client

Package Org. bruce. basic; import Java. io. ioexception; import java.net. datagrampacket; import java.net. datagramsocket; import java.net. inetsocketaddress; import java.net. socketexception; public class udpclient {public static void main (string [] ARGs) {// set "Hello! "Encapsulated as a string object, when calling its getbytes () the method is to convert it into a byte array // to a byte array to facilitate the transmission of byte [] buffer = (new string ("Hello! ")). Getbytes (); datagrampacket dp = NULL; try {// set the data to be sent, the address to be sent, and compress it into a datagrampacket packet package dp = new datagrampacket (buffer, buffer. length, new inetsocketaddress ("127.0.0.1", 9999);} catch (socketexception e) {e. printstacktrace ();} datagramsocket DS = NULL; try {// a new datagramsocket object (that is, opening a UDP port and preparing to send packets from it) ds = new datagramsocket (9998); DS. send (DP); DS. close ();} catch (socketexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}}

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.