![]() Terminate the server:A server should be always running, waiting for incoming requests from clients. Close the client connection:Invoke the close() method on the client Socket to terminate the connection with the client: socket.close() This method also closes the socket’s InputStream and OutputStream, and it can throw IOException if an I/O error occurs when closing the socket.We recommend you to use the try-with-resource structure so you don’t have to write code to close the socket explicitly.Of course the server is still running, for serving other clients. Writer.println(“This is a message sent to the server”) The argument true indicates that the writer flushes the data after each method call (auto flush). String line = reader.readLine() // reads a line of text Send data to the client:Use the OutputStream associated with the Socket to send data to the client, for example: OutputStream output = socket.getOutputStream() As the OutputStream provides only low-level methods (writing data as a byte array), you can wrap it in a PrintWriter to send data in text format, for example: PrintWriter writer = new PrintWriter(output, true) Int character = reader.read() // reads a single characterYou can also wrap the InputStream in a BufferedReader to read data as String, for more convenient: BufferedReader reader = new BufferedReader(new InputStreamReader(input)) So if you want to read the data at higher level, wrap it in an InputStreamReader to read data as characters: InputStreamReader reader = new InputStreamReader(input) Read data from the client:Once a Socket object is returned, you can use its InputStream to read data sent from the client like this: InputStream input = socket.getInputStream() The InputStream allows you to read data at low level: read to a byte array. And the connection is represented by the returned Socket object. Listen for a connection:Once a ServerSocket instance is created, call accept() to start listening for incoming client requests: Socket socket = serverSocket.accept() Note that the accept() method blocks the current thread until a connection is made. For example, the following line of code creates a server socket and binds it to the port number 6868: ServerSocket serverSocket = new ServerSocket(6868) Note that these constructors can throw IOException if an I/O error occurs when opening the socket, so you have to catch or re-throw it. ![]() So when to use which?Use the first constructor for a small number of queued connections (less than 50) and any local IP address available.Use the second constructor if you want to explicitly specify the maximum number of queued requests.And use the third constructor if you want to explicitly specify a local IP address to be bound (in case the computer has multiple IP addresses).And of course, the first constructor is preferred for simple usage. ServerSocket(int port, int backlog, InetAddress bindAddr): creates a server socket and binds it to the specified port number and a local IP address. ServerSocket(int port, int backlog): creates a server socket that is bound to the specified port number and with the maximum number of queued connections is specified by the backlog parameter. The maximum number of queued incoming connections is set to 50 (when the queue is full, new connections are refused). ServerSocket(int port): creates a server socket that is bound to the specified port number. Create a Server Socket:Create a new object of the ServerSocket class by using one of the following constructors: And each new connection should be handled by a separate thread.Let’s dive into each step in details. Close the connection with the client.The steps 3 and 4 can be repeated many times depending on the protocol agreed between the server and the client.The steps 1 to 5 can be repeated for each new client. Send data to the client via the client socket’s OutputStream.ĥ. Read data from the client via an InputStream obtained from the client socket.Ĥ. This results in a client socket is created for the connection.ģ. Listen for a connection from the client and accept it. Create a server socket and bind it to a specific port numberĢ. ![]() ![]() Here are the typical steps involve in developing a server program:ġ. ServerSocket APIThe ServerSocketclass is used to implement a server program. You will also learn how to create a multi-threaded server.First, let’s understand about the workflow and the API. In this Java network programming tutorial, you will learn how to develop a socket server program to implement fully functional network client/server application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |