Network Programming-TCP Program instance (client end Heserver communication)

Source: Internet
Author: User

1  PackageCom.yyq;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 ImportJava.io.OutputStream;6 Importjava.net.InetAddress;7 ImportJava.net.Socket;8 9 Ten /* One * Demonstrate TCP's transfer of client and server visits A * Requirements: The client sends data to the server to send back the data to the client after retrieving the information. -  */ - /* the * Client - * 1. Set up the socket service. Specify the host and port to receive - * 2. Get the output stream in the socket stream, write the data to the six, send it over the network to the server - * 3. Gets the input stream in the socket stream, obtains the data from the service-side feedback, and prints + * 4. Close the client resource.  -  *  +  */ A  Public classTcpClient2 { at      Public Static voidMain (string[] args)throwsException { -Socket s =NewSocket (Inetaddress.getlocalhost (), 4321); -OutputStream out =S.getoutputstream (); -Out.write ("Hello, service side". GetBytes ()); -InputStream in =S.getinputstream (); -         byte[] buf =New byte[1024]; in         intLen =In.read (BUF); -System.out.println (NewString (buf,0, Len)); to     } + } -  the  *  PackageCom.yyq; $ Panax Notoginseng Importjava.io.IOException; - ImportJava.io.InputStream; the ImportJava.io.OutputStream; + ImportJava.net.ServerSocket; A ImportJava.net.Socket; the  +  Public classTcpServer2 { -      Public Static voidMain (string[] args)throwsException { $ServerSocket SS =NewServerSocket (4321); $Socket s =ss.accept (); -InputStream in =S.getinputstream (); -         byte[] buf =New byte[1024]; the         intLen =In.read (BUF); -System.out.println ("IP:" +s.getinetaddress (). gethostaddress ());WuyiSystem.out.println (NewString (buf,0, Len)); theOutputStream out =S.getoutputstream (); -Thread.Sleep (10000); WuOut.write ("You're good too."). GetBytes ()); -     } About}

Network Programming-TCP Program instance (client end Heserver communication)

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.