Java uses Socket to implement simple server-to-client communication

Source: Internet
Author: User

The specific functions are as follows:

Write a C/S program using the Socket class and ServerSocket class to implement C/S communication.

The client sends the Time command to the server. After receiving the string, the server returns the current Time of the server to the client. The client sends the Exit command to the server, the server returns "Bye" to the client and then exits.

The question is relatively simple. Let's look at the Code directly:

The first is the server implementation:

 Import java.net. serverSocket; <br/> import java.net. socket; <br/> import java. io. *; <br/> import java. text. dateFormat; <br/> import java. util. date; // time <br/> // import java. util. calendar; <br/> public class Server {<br/> public static void main (String args []) {<br/> try {<br/> ServerSocket server = new ServerSocket (11111); // create a server socket <br/> System. out. println ("server started"); </p> <p> System. out. println ("waiting for client connection... "); <Br/> Socket socket = server. accept (); // wait for client connection </p> <p> BufferedReader reader = new BufferedReader (new InputStreamReader (socket. getInputStream (); // obtain the input stream of the client <br/> // OutputStreamWriter out = new OutputStreamWriter (socket. getOutputStream (); // obtain the output stream of the client <br/> PrintWriter out = new PrintWriter (new OutputStreamWriter (socket. getOutputStream (), true); // obtain the client output stream) <br/> if (socket. isConnected () {<br/> System. o Ut. println ("client name:" + socket. getInetAddress (). getHostAddress () + "connection successful! "); <Br/> // out. write ("connection successful/n"); <br/> out. println ("connection successful"); <br/>}</p> <p> while (true) {<br/> String str = reader. readLine (); <br/> if (str. equals ("Time") {// client request Time data <br/> Date date = new Date (); <br/> // Calendar cal = Calendar. getInstance (); <br/> System. out. println ("current time of client request"); <br/> DateFormat format = DateFormat. getDateInstance (); <br/> // out. write (format. format (date); // format the output time <br/> out. println (format. format (date); <br/>}< br/> else if (str. equals ("exit") {// exit <br/> // out. write ("bye"); <br/> out. println ("bye"); <br/> System. out. println ("disconnected"); <br/> break; <br/>}< br/> else {<br/> System. out. println ("data:" + str); <br/>}< br/>}catch (IOException e) {<br/> e. printStackTrace (); <br/>}< br/>

After configuring the server side, we will continue to write the client program:

Import java.net. socket; <br/> import java. io. *; </p> <p> public class Client {<br/> public static void main (String args []) <br/> try {<br/> Socket client = new Socket ("localhost", 11111 ); // create a client socket </p> <p> OutputStream out = client. getOutputStream (); // get the output stream <br/> BufferedReader reader = new BufferedReader (new InputStreamReader (client. getInputStream (); // obtain the data returned by the server from the input stream <br/> BufferedReader localMessage = n Ew BufferedReader (new InputStreamReader (System. in); // receives information input from the client's keyboard </p> <p> System. out. println ("slave server" + client. getRemoteSocketAddress () + "Returned message:" + reader. readLine (); </p> <p> while (true) {// transmit data from the client to the server <br/> String message, str; <br/> message = localMessage. readLine () + "/n"; // read a row of data from the screen <br/> // System. out. print (message); <br/> out. write (message. getBytes (); // transfer to server </p> <p> str = reader. readLine (); <br/> System. out. Println ("slave server" + client. getRemoteSocketAddress () + "Returned message:" + str); <br/> if (str. equals ("Bye") {// The server returns the end data <br/> System. out. println ("disconnected! "); <Br/> break; <br/>}< br/>} catch (IOException e) {<br/> e. printStackTrace (); <br/>}< br/>

 

Related Article

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.