TCP and UDP programming for Java

Source: Internet
Author: User

Tag: request ACK style row Tput get put info system

Tcp

Client:

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket; Public classmyclient { Public Static voidMain (string[] args)throwsexception{Socket Socket=NULL; BufferedReader in=NULL; PrintWriter out=NULL; BufferedReader input=NULL; //server requesting the specified IP and port numberSocket =NewSocket ("127.0.0.1", 3333);  while(true) { in=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); out=NewPrintWriter (Socket.getoutputstream (),true); //input to the receiving consoleinput =NewBufferedReader (NewInputStreamReader (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 ();    }}

Service side:

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classMyServices { Public Static voidMain (string[] args)throwsexception{ServerSocket SS=NULL; Socket Socket=NULL; BufferedReader in=NULL; PrintWriter out=NULL; BufferedReader input=NULL; //listening on port 3333SS =NewServerSocket (3333); //waiting for a request to receive a clientSocket =ss.accept ();  while(true){            //gets the input stream of the connection objectin =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //get input from the clientString str =In.readline (); Input=NewBufferedReader (NewInputStreamReader (system.in)); System.out.println ("Server Display--Client input data:" +str); out=NewPrintWriter (Socket.getoutputstream (),true); //outputting data to the client//out.println ("hehe");String info =Input.readline ();        OUT.PRINTLN (info); }//in.close ();//Out.flush ();//out.close ();    }}

Udp

Client:

ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket; Public classUdpClient { Public Static voidMain (string[] args)throwsException {datagramsocket ds=NULL; Datagrampacket DP=NULL; byte[] buf =New byte[1024]; DS=NewDatagramsocket (3333); DP=NewDatagrampacket (BUF, 1024); //receive data, put in a datagramDs.receive (DP); //extracting data from a datagramString info =NewString (Dp.getdata (), 0, Dp.getlength ()); System.out.println ("The message Received is:" +info); }}

Service side:

ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress; Public classUdpserver { Public Static voidMain (string[] args)throwsException {datagramsocket ds=NULL; Datagrampacket DP=NULL; //building a Send objectDS =NewDatagramsocket (); //package data into a datagramString info = "Hello world!"; DP=NewDatagrampacket (Info.getbytes (), Info.length (), Inetaddress.getbyname ("localhost"), 3333); //Issue DatagramsDs.send (DP); }}

TCP and UDP programming for Java

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.