TCP Simple Communication

Source: Internet
Author: User

Client code:
 Packagecom.kaige123.net01;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.Socket;ImportJava.net.UnknownHostException;/*** Edith * /  Public  class Client {Public   static void main(string[] args) throws Exception {//If the other server does not exist, it will errorSocket socket =NewSocket ("127.0.0.1",8080); InputStream input = Socket.getinputstream (); OutputStream output = Socket.getoutputstream (); Output.write ("Hello server, I am Kay, do you remember me?" ". GetBytes ()); Output.flush ();//Quickly export the content to the other side byte[] b=New byte[1024x768];intLen=input.read (b); System.out.println ("The server said:"+NewString (b,0, Len)); Output.close (); Input.close (); Socket.close ();}}
Service-Side code:
 Packagecom.kaige123.net01;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** Edith * /  Public  class Server {Public   static void main(string[] args) throws Exception{//If all is occupied then it will throw an exception //Establish good one server monitor 8080 portServerSocket Server =NewServerSocket (8080);//wait for the client to connect to the serverSocket socket = server.accept ();//code to run to this sentence will jam the main blocked waitInputStream input = Socket.getinputstream (); OutputStream output = Socket.getoutputstream ();byte[] B =New byte[1024x768*5];intLen = Input.read (b); String s =NewString (b,0, Len); System.out.println (s); s ="Hello Edith, I am back from Dongguan beautiful!"; Output.write (S.getbytes ()); Output.close (); Input.close (); Socket.close ();}}

TCP Simple 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.