Java Basic Knowledge Enhancement Network programming note 06:tcp TCP protocol send data and receive data

Source: Internet
Author: User

1. The TCP protocol sends data and receives data

The TCP protocol receives data:
• Create a Socket object on the receiving end
• listen for client connections. Returns a corresponding socket object
• get input stream, read data displayed in console
• Freeing resources

The TCP protocol sends data:

• Create a socket object on the sending side
• This step, if successful, indicates that the connection has been established successfully.
• get output stream, write data
• Freeing resources

2. Code implementation:

(1) Sending side:

1  Packagecn.itcast_06;2 3 Importjava.io.IOException;4 ImportJava.io.OutputStream;5 ImportJava.net.Socket;6 7 /*8 * The TCP protocol sends data:9 * A: Create a Socket object on the sending sideTen * This step, if successful, indicates that the connection has been established successfully .  One * B: Get output stream, write data A * C: Release Resources -  *  - * connection is denied. The TCP protocol must first open the server .  the * Java.net.ConnectException:Connection Refused:connect -  */ -  Public classClientdemo { -      Public Static voidMain (string[] args)throwsIOException { +         //create a Socket object on the send side -         //Socket (inetaddress address, int port) +         //Socket (String host, int port) A         //socket s = new socket (Inetaddress.getbyname ("192.168.12.92"), 8888); atSocket s =NewSocket ("192.168.12.92", 8888); -  -         //get output stream, write data -         //Public OutputStream Getoutputstream () -OutputStream OS =S.getoutputstream (); -Os.write ("hello,tcp, here I Come". GetBytes ()); in  -         //Freeing Resources to s.close (); +     } -}

(2) Receiving end:

1  Packagecn.itcast_06;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 ImportJava.net.ServerSocket;6 ImportJava.net.Socket;7 8 /*9 * TCP protocol receives data:Ten * A: Create a Socket object on the receiving end One * B: Listens for client connections. Returns a corresponding socket object A * C: Get input stream, read data display in console - * D: Releasing Resources -  */ the  Public classServerdemo { -      Public Static voidMain (string[] args)throwsIOException { -         //create a socket object on the receiving end -         //serversocket (int port) +ServerSocket SS =NewServerSocket (8888); -  +         //listens for client connections. Returns a corresponding socket object A         //Public Socket Accept () atSocket s = ss.accept ();//listens for and accepts connections to this socket. This method is blocked until the connection is passed in.  -  -         //get input stream, read data display in console -InputStream is =S.getinputstream (); -  -         byte[] Bys =New byte[1024]; in         intLen = Is.read (bys);//Blocking Method -String str =NewString (bys, 0, Len); to  +String IP =s.getinetaddress (). gethostaddress (); -  theSYSTEM.OUT.PRINTLN (IP + "---" +str); *  $         //Freeing ResourcesPanax Notoginseng s.close (); -         //ss.close ();//this should not be closed . the     } +}

run the effect as follows:

Java Basic Knowledge Enhancement Network programming note 06:tcp TCP protocol send data and receive data

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.