[Java Study Notes] tcp client/server

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Client:

Import java.net. Socket;
Import java.net. inetaddress;
Import java.net. unknownhostexception;
Import java. Io. outputstream;
Import java. Io. bufferedreader;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. printwriter;
Import java. Io. ioexception;
Public class tcpclient {
Public static void main (string [] ARGs) throws ioexception {
Try {
Byte A [] = {127,0, 0, 1 };
Inetaddress address = inetaddress. getbyaddress ();
Socket socket = new socket (address, 9876 );
Outputstream OS = socket. getoutputstream ();
Printwriter out = new printwriter (OS, true );
Inputstream is = socket. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader in = new bufferedreader (ISR );
String line;
Line = "A line sent by the client ";
Out. println (line );
Line = in. Readline ();
System. Out. println ("client got:" + line );
Out. Close ();
In. Close ();
Socket. Close ();
} Catch (unknownhostexception e ){
System. Out. println (E );
} Catch (ioexception e ){
System. Out. println (E );
}
}
}

Server:

Import java.net. serversocket;
Import java.net. Socket;
Import java. Io. bufferedreader;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. Io. printwriter;
Public class tcpserver {
Public static void main (string [] ARGs ){
Try {
Serversocket = new serversocket (9876 );
Socket socket =Serversocket. Accept ();
/* Same as client */
Inputstream is = socket. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader in = new bufferedreader (ISR );
Outputstream OS = socket. getoutputstream ();
Printwriter out = new printwriter (OS, true );
String line;
While (line = in. Readline ())! = NULL ){
System. Out. println ("server got:" + line );
Line = "A line sent by the server ";
Out. println (line );
}
Out. Close ();
In. Close ();
Socket. Close ();
Serversocket. Close ();
} Catch (ioexception e ){
System. Out. println (E );
System. Exit (1 );
}
}
}

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.