Network programming TCP (II) __ Programming

Source: Internet
Author: User
Tags telnet program
package com.server;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import Java.net.ServerSocket;

Import Java.net.Socket;
		public class TCPServer {public static void main (string[] args) {ServerSocket server=null;
		Socket Socket=null;
		BufferedReader In=null;
		PrintWriter Out=null;
			try {//server listens for Server=new ServerSocket (9000) on port 9000;
			Receive Client connection socket=server.accept ();
			Get the client input information in=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
			Output information to the client, true to automatically flush the buffer out=new PrintWriter (Socket.getoutputstream (), true);
			Out.print ("Established connection to server");
			String Info=in.readline ();
		SYSTEM.OUT.PRINTLN ("Client input is:" +info);
		catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
				}finally{try {in.close ();
				Out.close ();
				Socket.close ();
			Server.close ();
			catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }
		}
	}
}

This example creates a ServerSocket object that waits for a connection on port 9000. After receiving the client's connection request, the program obtains the input output stream from the socket object that the client establishes a connection to, and passes the input and output to the client to output the "connected to the server" information and reads the information through the output stream.

To verify that the server-side program works correctly, the program also needs to provide a client program to communicate with the server. The Telnet program in the Windows system is a system-led TCP client that can use Telnet to test communication with the server side. When you run Telnet, specify the IP address and port number of the connection server, connect one but build, the Telnet program window can be entered to send to the server, and the data received from the server can be displayed, and the test steps are as follows:

(1) Running server-side programs

(2) running Telnet 127.0.0.1 9000 in a DOS window because the server-side program and the client program are on the same machine, you can use 127.0.0.1 to represent this machine.

Figure 2 here would have been "established connected", but may be garbled for the sake of not showing, like I sent to the server terminal "AAA" is not shown (here I did enter AAA)

Figure 3 is the "AAA" I output in the cmd command, which proves that the communication was successful.

The above cases can only accept connection requests from a single client.

So..... Brother. Here's the problem. I want the server side to be able to accept connection requests from multiple clients.

Solution: Good to do. , a multithreaded mechanism can be used to refactor a program to enable the server to accept connection requests from multiple clients. The principle is to start a separate thread for each client's connection to the server.

Watch out. Brother. Calling once the Accept () method can only accept a single connection, accepting multiple connections requires that the method be placed in a circular statement, and the data input and output of each connection should also be placed in a circular statement in order to achieve a constant exchange of server-side data with the client. The following figure:

Don't stare at the words that even I can't see clearly ... ok ... The word is cumputer1 number, corresponding to the following is number 2nd, you can have 3rd, 5th, 100th, how much we think how much.

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.