The client code is as follows:
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 reads data from the server//the Inputstreamreader//inputstreamreader that connects to the low-level input stream on the socket is a bridge between the lower and upper stream streams/ S.getinputstream () get input stream from socket InputStreamReader streamreader=new InputStreamReader (S.getinputstream ());// Establish BufferedReader to read BufferedReader reader=new BufferedReader (StreamReader); String Advicestring=reader.readline (); System.out.println ("Today You should:" +advicestring);//client sends data to server//creates PrintWriter object without automatic row refresh,//If add second parameter true, Is automatic refresh, do 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 ();}}The server-side code is as follows:
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 Port 4242 serversocket serversocket=new serversocket (4242);//the server enters an infinite loop waiting for the client's request while (true) { System.out.println ("Waiting for client connections ...");//When a user connection request is received, this method returns a socket so that the client communicates//The port number of this socket and the port number of the ServerSocket are different from the socket Sock=serversocket.accept ();//This method blocks 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 the 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);//The server receives data sent by the 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 ();}}
Socket Programming code example (single thread)