Socket connection case for TCP under Java

Source: Internet
Author: User

 PackageCn.stat.p4.ipdemo;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classServerdemo {/**     * @paramargs *@throwsIOException*/     Public Static voidMain (string[] args)throwsIOException {//creating server-to-imageServerSocket ss=NewServerSocket (1000); //gets the client connected to the image .Socket s=ss.accept (); //gets the input stream through the socket object to read the data sent by the client \BufferedReader bufin=NewBufferedReader (NewInputStreamReader (S.getinputstream ())); //4. Get the output stream of the socket and decorate it. PrintWriter out =NewPrintWriter (S.getoutputstream (),true); String Line=NULL;  while((Line=bufin.readline ())! =NULL) {System.out.println (line); //Sendout.println (Line.touppercase ());        } s.close ();                    Ss.close (); }}
 PackageCn.stat.p4.ipdemo;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket;Importjava.net.UnknownHostException; Public classClentdemo {/**     * @paramargs *@throwsIOException *@throwsunknownhostexception*/     Public Static voidMain (string[] args)throwsunknownhostexception, IOException {//Create a connection pair likeSocket socket=NewSocket ("192.168.1.103", 1000); //get keyboard inputBufferedReader bufr=NewBufferedReader (NewInputStreamReader (system.in)); //gets the output stream pair imagePrintWriter pw=NewPrintWriter (Socket.getoutputstream (),true); //Get Return DataBufferedReader bufin=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Line=NULL;  while((Line=bufr.readline ())! =NULL)        {            if(Line.equals ("Over"))                 Break;            System.out.println (line);                        Pw.println (line); String Uptext=Bufin.readline ();                    System.out.println (Uptext);    } socket.close (); }}

Socket connection case for TCP under 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.