Java Socket Network Programming

Source: Internet
Author: User

Server-side:

1 Importjava.net.*;2 ImportJava.io.*;3  Public classSocketserverextendsThread {4     PrivateServerSocket ServerSocket;5      PublicSocketserver (intPortthrowsIOException6     {7serversocket=NewServerSocket (port);8         //waiting for customers to connect for no more than 6 seconds9Serversocket.setsotimeout (100000);Ten     } One      Public voidRun () A     { -          while(true) -         { the             Try -             { -System.out.println ("Wait for remote connection, port number:" +Serversocket.getlocalport ()); -Socket Server =serversocket.accept (); +SYSTEM.OUT.PRINTLN ("Remote host address" +server.getremotesocketaddress ()); -                 //read data from client +DataInputStream in=NewDataInputStream (Server.getinputstream ()); A System.out.println (In.readutf ()); at                 //sending data to the client -DataOutputStream out=NewDataOutputStream (Server.getoutputstream ()); -Out.writeutf ("Thank You for connecting me:" +server.getlocalsocketaddress () + "\ngoodbye!"); - server.close (); -             } -             Catch(socketexception s) in             { -System.out.println ("Socket timed out"); to                  Break; +             } -             Catch(IOException e) the             { * e.printstacktrace (); $                  Break;Panax Notoginseng             } -         } the     } +      Public Static voidMain (string[] args) A     { the         intport=6066; +         Try -         { $Thread t=NewSocketserver (port); $ T.run (); -         } -         Catch(IOException e) the         { - e.printstacktrace ();Wuyi         } the     } -}

Client:

1 Importjava.net.*;2 ImportJava.io.*;3  Public classsocketclient {4      Public Static voidMain (string[] args)5     {6         //Server Address7String servername= "localhost";8         //Server port number9         intport=6066;Ten         Try One         { ASYSTEM.OUT.PRINTLN ("Connect to Host:" +servername+ ", port number:" +port); -Socket client=NewSocket (servername,port); -SYSTEM.OUT.PRINTLN ("Remote host Address:" +client.getremotesocketaddress ()); the             //sending messages to the server -OutputStream outtoserver=Client.getoutputstream (); -DataOutputStream out=NewDataOutputStream (outtoserver); -Out.writeutf ("Hello from" +client.getlocalsocketaddress ()); +              -             //get the information returned by the server +InputStream infromserver=Client.getinputstream (); ADataInputStream in=NewDataInputStream (infromserver); atSYSTEM.OUT.PRINTLN ("Server response:" +In.readutf ()); - client.close (); -         } -         Catch(IOException e) -         { - e.printstacktrace (); in         } -     } to  +}

Java Socket Network Programming

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.