Java Network Programming 2: a solution for the server to process multiple user requests (return information entered by multiple users)

Source: Internet
Author: User
Tags connect socket

1. using multiple threads to provide services for multiple customers at the same time is the most common method to improve the server's concurrent performance. How can we provide services for multiple users?

There are three main methods:

2. To better understand how the server works, we will use the first method to process multiple user requests. If you want to use the second and third methods, refer to touch's blog:

Java multithreading Summary 5: principles and implementation of thread pools

In the following example, the client inputs information. The server replies with the same information. You can enable multiple clients at the same time and enter the information. The source code is as follows:

Server code:

Package demo.net; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. ioexception; import java.net. serversocket; import java.net. socket;/*** the server returns the information entered by the user to provide services for multiple users: assign a working thread to each customer */public class chatservers {private int Port = 8189; // Default Server Port Public chatservers () {}// create public chatservers (INT port) {This. port = port;} public void Service () {int I = 0; try {// establish a server connection and set customer connection Length of the Request queue serversocket Server = new serversocket (port, 3); While (true) {// wait for the customer to connect Socket socket = server. accept (); I ++; system. out. println ("Number" + I + "customer connected successfully! "); New thread (New serverthread (socket )). start () ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {New chatservers (). service () ;}} class serverthread implements runnable {private Socket socket; Public serverthread (Socket socket) {This. socket = socket;} // The task is to provide services for a user @ overridepublic void run () {try {// datainputstreamdatainputstream in = new datainputstream (socket. getinputstream (); // dataoutputstreamdataoutputstream out = new dataoutputstream (socket. getoutputstream (); While (true) {// read information from the client string accpet = in. readutf (); out. writeutf ("server:" + accpet) ;}} finally {// if the connection fails to be established, no socket is executed. close (); socket. close () ;}} catch (ioexception e) {e. printstacktrace ();}}}

Client code:

Package demo.net; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. ioexception; import java.net. socket; import Java. util. optional;/*** note that the input and output streams datainputstream and dataoutputstream are used in pairs. It is best to use words to throttle * // client class public chatclients {private string host = "localhost "; // connect to the local private int Port = 8189 by default; // The default connection port is 8189 public chatclients () {}// connect to the specified host and Port Public chatclients (string host, int port) {This. host = host; this. port = port;} public void chat () {try {// connect to the server Socket socket = new socket (host, Port ); try {// datainputstreamdatainputstream in = new datainputstream (socket. getinputstream (); // dataoutputstreamdataoutputstream out = new dataoutputstream (socket. getoutputstream (); // enter the standard input stream for decoration. in); While (true) {string send = success. nextline (); // send the information obtained from the console to the server out. writeutf (send); // read the information from the Server String accpet = in. readutf (); system. out. println (accpet) ;}} finally {socket. close () ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {New chatclients (). chat ();}}


 

Running result:

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.