Socket Sample Code demo program (threaded)

Source: Internet
Author: User

Client code, such as the following:

Import Java.io.*;import java.net.*;p ublic class Dailyadviceclient {public void Go () {try {socket s=new socket ("127.0.0.1" , 4242);//Establish a socket connection to the server//client read data from the server//establish a inputstreamreader//to connect to the low-level input stream on the socket InputStreamReader is a bridge between the lower and upper streams//s.getinputstream () gets the input stream from the socket InputStreamReader streamreader=new InputStreamReader (S.getinputstream ());//build BufferedReader to read BufferedReader reader=new BufferedReader (streamReader ); String Advicestring=reader.readline (); System.out.println ("Today You should:" +advicestring);//client sends data to the server//creates PrintWriter objects without their own active row refreshes. Assuming that the second parameter is added to True, the active refresh for itself does not need to write Writer.flush () This line of code PrintWriter writer=new PrintWriter (S.getoutputstream ()); Writer.println ("Hello,i am client2!"); Writer.flush ();//Flush Send data System.out.println ("Remote address for this socket connection:" +s.getinetaddress (). gethostaddress ()); SYSTEM.OUT.PRINTLN ("Local address of this socket binding:" +s.getlocaladdress (). gethostaddress ()); SYSTEM.OUT.PRINTLN ("Local port for this socket binding:" +s.getlocalport ()); SYSTEM.OUT.PRINTLN ("The remote port this socket is connected to:" +s.getport ()); S.close ();//Close Socket} catch (ioexceptIon e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public static void Main (String[]args) {dailyadviceclient client=new dailyadviceclient (); Client.go ();}}
Server-side code such as the following:

Import Java.io.*;import java.net.*;p ublic class Dailyadviceserver {string[] advicelist={"Take smaller Bites", "one word: Inappropriate "," "+" Just for today,be Honest "," Tell Your Boss "};p ublic void Go () {try {//server application creates a ServerSocket object for a specific port This causes the server application to listen for client requests from 4242port ServerSocket serversocket=new serversocket (4242);//the server enters an infinite loop waiting for the client's request while ( true) {System.out.println ("Wait for Client connection ...");//After receiving a user connection request. This method returns a socket. To facilitate client communication//The port number of this socket and the port number of the ServerSocket socket sock=serversocket.accept ();// This method will clog until a connection request arrives at SYSTEM.OUT.PRINTLN ("Address of this socket connection:" +sock.getinetaddress (). gethostaddress ()); SYSTEM.OUT.PRINTLN ("Local address of this socket binding:" +sock.getlocaladdress (). gethostaddress ()); SYSTEM.OUT.PRINTLN ("Local port for this socket binding:" +sock.getlocaladdress ()); SYSTEM.OUT.PRINTLN ("This socket is connected to a remote port:" +sock.getport ());//The server sends data to the client PrintWriter writer=new printwriter ( Sock.getoutputstream ()); String Advicestring=getadvice (); writer.println (advicestring); Writer.flush (); System.out.println (advicestring);//server receives data sent by client InputStreamReader strEamreader=new InputStreamReader (Sock.getinputstream ()); BufferedReader reader=new BufferedReader (StreamReader); String Advicestringrec=reader.readline (); System.out.println ("REC:" +advicestringrec);}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Public String Getadvice () {int random= (int) (Math.random () *advicelist.length); return advicelist[random];} public static void Main (String []args) {dailyadviceserver server=new dailyadviceserver (); Server.go ();}}


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Socket Sample Code demo program (threaded)

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.