JAVA--TCP Transmission

Source: Internet
Author: User

Demonstrates mutual access of the client and server side of the transport of TCP

Import Java.io.*;import java.net.*;/* Requirements: The client sends data to the server, and after the server receives it, it feeds back the information to the client. *//* Client: 1, set up socket service, specify to connect host and port. 2. Gets the output stream in the socket stream and writes the data to the stream. Sent over the network to the service side. 3. Get the input stream in the socket stream, get the data from the service side and print it. 4. Close Client Resources */Class tcpclient2{ Public Static void Main(string[] args) throws Exception {Socket s =NewSocket ("192.168.10.111",10005); OutputStream out= S.getoutputstream (); out. Write ("Hello, service side!" ". GetBytes ()); InputStreaminch= S.getinputstream ();byte[] buf =New byte[1024x768];intLen =inch. read (BUF); System. out. println (NewString (BUF,0, Len));    S.close (); }}class tcpserver2{ Public Static void Main(string[] args) throws Exception {ServerSocket SS =NewServerSocket (10005);        Socket s = ss.accept ();        String IP = s.getinetaddress (). gethostaddress (); System. out. println (ip+"... connected"); InputStreaminch= S.getinputstream ();byte[] buf =New byte[1024x768];intLen =inch. read (BUF); System. out. println (NewString (BUF,0, Len)); OutputStream out= S.getoutputstream (); Thread.Sleep (10000); out. Write ("Dude, you got it, too.". GetBytes ());        S.close ();    Ss.close (); }}

JAVA--TCP Transmission

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.