Socket (Java)

Source: Internet
Author: User

Client. Java

Public class client <br/>{< br/> Socket socket; <br/> bufferedreader in; <br/> printwriter out; <br/> Public client () <br/>{< br/> try {<br/> system. out. println ("try to connect to 127.0.0.1: 10000"); <br/> socket = new socket ("127.0.0.1", 10000); <br/> system. out. println ("the server connected! "); <Br/> system. out. println ("Please enter some character:"); <br/> bufferedreader line = new bufferedreader (New inputstreamreader (system. in); <br/> out = new printwriter (socket. getoutputstream (), true); <br/> out. println (line. readline (); <br/> In = new bufferedreader (New inputstreamreader (socket. getinputstream (); <br/> system. out. println (in. readline (); <br/> out. close (); <br/> in. close (); <br/> socket. close (); <br/>}< br/> catch (ioexception e) <br/>{< br/> out. println ("wrong"); <br/>}</P> <p> Public static void main (string [] ARGs) <br/>{< br/> new client (); <br/>}< br/>}

 

Server. Java

Public class server <br/>{< br/> private serversocket SS; <br/> private Socket socket; <br/> private bufferedreader in; <br/> private printwriter out; <br/> public server () <br/> {<br/> try {<br/> SS = new serversocket (10000); <br/> while (true) {<br/> socket = ss. accept (); <br/> string remoteip = socket. getinetaddress (). gethostaddress (); <br/> string remoteport = ":" + socket. getlocalport (); <br/> S Ystem. Out. println ("a client come in! IP: "+ remoteip + remoteport); <br/> In = new bufferedreader (New inputstreamreader (socket. getinputstream (); <br/> string line = in. readline (); <br/> system. out. println ("client send is:" + line); <br/> out = new printwriter (socket. getoutputstream (), true); <br/> out. println ("Your Message received ed! "); <Br/> out. println (""); <br/> out. close (); <br/> in. close (); <br/> socket. close (); <br/>}< br/> catch (ioexception e) {<br/> out. println ("wrong"); <br/>}</P> <p> Public static void main (string [] ARGs) <br/>{< br/> new server (); <br/>}< br/>}

 

After the server program is started, the client transmits strings

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.