Java Network Programming: TCP and UDP

Source: Internet
Author: User

TCP:

Import java.net. serversocket; import java.net. socket; import Java. io. dataoutputstream; public class j_server {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubtry {serversocket Server = new serversocket (5000); While (true) {socket S = server. accept (); system. out. println ("server receiving connection"); dataoutputstream dataout = new dataoutputstream (S. getoutputstream (); dataout. writeutf ("Greetings from servers"); dataout. close (); S. close () ;}} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

Import java.net. socket; import java.net. unknownhostexception; import Java. io. datainputstream; public class j_client {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubtry {socket S = new socket ("localhost", 5000); datainputstream dataIn = new datainputstream (S. getinputstream (); system. out. println ("client receipt:" + dataIn. readutf (); dataIn. close (); S. close ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

UDP:

Import java.net. datagramsocket; import java.net. datagrampacket; import java.net. socketexception; import java.net. inetaddress; public class j_udpserver {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub1_ramsocket dsocket; datagrampacket inpacket; datagrampacket outpacket; inetaddress caddr; int cport; int COUNT = 0; try {dsocket = new datagramsocket (8000); While (true) {count ++; inpacket = new datagrampacket (New byte [100], 100); dsocket. receive (inpacket); // receives data packets and stores them in inpacketcaddr = inpacket. getaddress (); cport = inpacket. getport (); string S = new string (inpacket. getdata (), 0, inpacket. getdata (). length); system. out. println ("Number" + Count + "Device Access"); system. out. println ("message:" + S); system. out. println ("clientaddr:" + caddr. gethostname (); system. out. println ("clientport:" + cport); string outdata = "returnback _" + String. valueof (count); outpacket = new datagrampacket (outdata. getbytes (), outdata. getbytes (). length, caddr, cport); dsocket. send (outpacket) ;}} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

Import java.net. datagramsocket; import java.net. datagrampacket; import java.net. socketexception; import java.net. inetaddress; public class j_udpclient {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub1_ramsocket dsocket; datagrampacket inpacket; datagrampacket outpacket; inetaddress saddr; try {dsocket = new datagramsocket (); saddr = inetaddress. getbyname ("localhost"); string outdata = "request connection... "; outpacket = new datagrampacket (outdata. getbytes (), outdata. getbytes (). length, saddr, 8000); dsocket. send (outpacket); // send data packet inpacket = new datagrampacket (New byte [100], 100); dsocket. receive (inpacket); // receives data packets and stores them in inpacketstring S = new string (inpacket. getdata (), 0, inpacket. getdata (). length); system. out. println ("message:" + S); system. out. println ("serveraddr:" + inpacket. getaddress (). gethostname (); system. out. println ("SERVERPORT:" + inpacket. getport (); dsocket. close ();} catch (exception e) {// todo auto-generated catch blocke. 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.