TCP Transport protocol in Java

Source: Internet
Author: User

Class TcpClient { Public Static void Main(string[] args) throws Exception {//Create Client Socket service, specify destination host and PortSocket s =NewSocket ("192.168.1.10",10003);//In order to send data, get the output stream in the socket streamJava.io.OutputStream out= S.getoutputstream (); out. Write ("Hello TCP". GetBytes ());    S.close (); }}class TCPServer { Public Static void Main(string[] args) throws Exception {//Set up a service-side socket service and listen to a portServerSocket SS =NewServerSocket (10003);//Get Connected client objects through the Accept method .Socket s = ss.accept (); String IP = s.getinetaddress (). gethostaddress ();//Get the data sent by the client, use the read stream of the client object to read the data .InputStreaminch= S.getinputstream ();byte[] buf =New byte[1024x768];intLen =inch. read (BUF); System. out. println (NewString (BUF,0, Len));//Close clientS.close (); }}class TcpClient2 { Public Static void Main(string[] args) throws Exception {//Establish socket service, specify connected host and portSocket s =NewSocket ("192.168.1.10",10004);//Gets the output stream in the socket stream. Writes data to the stream, which is sent over the network to the serverOutputStream out= (OutputStream) s.getoutputstream (); out. Write ("Hello Tcp". GetBytes ());//Gets the input stream in the socket stream, obtains the data from the service-side feedback, and printsInputStreaminch= 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 (10004); Socket s = ss.accept ();//Service end-of-receipt informationInputStreaminch= S.getinputstream (); String IP = s.getinetaddress (). gethostaddress ();byte[] buf =New byte[1024x768];intLen =inch. read (BUF); System. out. println (NewString (BUF,0, Len));//service-side informationOutputStream out= (OutputStream) s.getoutputstream (); out. Write ("have receive". GetBytes ());        S.close ();    Ss.close (); }}/* Requirements: Set up a text conversion server client to send text to the service side, the service will turn the text into uppercase and then return to the client */Class TcpClient3 { Public Static void Main(string[] args) throws Exception {Socket s =NewSocket ("192.168.1.4",10005);//Define stream object to read keyboard data, read keyboard input textBufferedReader BUFR =NewBufferedReader (NewInputStreamReader (System.inch));//Get the output stream in the socket stream and transfer it over the network to the serverBufferedWriter bufout =NewBufferedWriter (NewOutputStreamWriter (S.getoutputstream ()));//Define a socket read stream to read uppercase information returned by the serverBufferedReader Bufin =NewBufferedReader (NewInputStreamReader (S.getinputstream ())); String line =NULL; while((Line=bufr.readline ())! =NULL) {if("Over". Equals (line)) Break;            Bufout.write (line); Bufout.newline ();//end tagBufout.flush ();            String str = bufin.readline (); System. out. println ("Server:"+STR);        } bufr.close ();        S.close (); }}class TcpServer3 { Public Static void Main(string[] args) throws Exception {ServerSocket SS =NewServerSocket (10005); Socket s = ss.accept ();//Read the data from the socket read streamBufferedReader Bufin =NewBufferedReader (NewInputStreamReader (S.getinputstream ()));//socket output stream, writes uppercase text to the socket output stream, and sends it to the clientBufferedWriter bufout =NewBufferedWriter (NewOutputStreamWriter (S.getoutputstream ())); String line =NULL; while((Line=bufin.readline ())! =NULL) {Bufout.write (Line.touppercase ()); Bufout.newline ();//end tagBufout.flush ();        } s.close ();    Ss.close (); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

TCP Transport protocol in 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.