Socket Long connection Short connection heartbeat JAVA socket programming __ios

Source: Internet
Author: User

The simple explanation is:

Short connection: Establish a connection, send a packet, close the connection

Long connection: Establish a connection, send a packet, send a heartbeat packet, send a packet, send a heartbeat packet, send a heartbeat packet ...


Therefore, frequent data sending and receiving, the short connection will frequently create a TCP connection, and for long connections, always use the same TCP connection


Package com.tree.demo.socket;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;

Import Java.net.Socket;
	
	public class Serversocketdemo {private static final int PORT = 1234;
	
	
	private static final int buffer_size = 1024; public static void Main (string[] args) {//TODO auto-generated Method Stub try {serversocket SS = new Serversocke
			T (PORT);
			Socket s = ss.accept ();
			System.out.println ("This is the server, listening to the local" +port+ "Port");
			byte[] Recdata = null;
			InputStream in = S.getinputstream ();
			OutputStream out = S.getoutputstream ();
				while (true) {recdata = new byte[buffer_size];
				int r = in.read (Recdata);
				int r = in.read (Recdata);
					if (r>-1) {String data = new string (recdata);
					if (Data.trim (). Equals ("over")) {s.close ();
					System.out.println ("read to the data sent by the client:" +data);
					Out.write ("This is the data that the server sends to the client:". GetBytes ());
				Out.write (Recdata); }else {System.out.println ("The data read is complete.
					");
					S.close ();
					System.exit (0);
				Ss.close ();
		A catch (IOException e) {e.printstacktrace ());
 }
	}

}

The above service-read data is blocked, and if the client has no data to write, the server will be stuck there.

Client open a Telnet to test the server socket program, of course, you can also write a socket client, as follows:

Package socket;

Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;

public class Clientsocketdemo {

	private static final String HOST = "192.168.8.14";

	private static final int PORT = 1234;

	public static void Main (string[] args) throws Unknownhostexception,
			IOException {
		Socket client = new Socket (HOST , PORT);
		OutputStream out = Client.getoutputstream ();
		try {while
			(true) {
				thread.sleep (1000);
				System.out.println ("Send heartbeat Packet");
				Out.write ("Send heart beat Data package!"). GetBytes ());
			}
		catch (Exception e) {
			e.printstacktrace ();
			Out.close ();
			Client.close ();}}


At the same time this is an example of a heartbeat, heartbeat as long as there is no exception, you can directly use the client connection to send packets, do not need a new socket connection.


Accept () waits for the connection, which blocks the current thread until the connection is established


The above server-side network programs can only be connected to one client program and deny connections to multiple clients.

We can solve this problem by threading: Whenever the program creates a new socket connection, that is, when the Accept method is invoked, a new thread is started to handle the connection between the server and the client, and the main program returns immediately and waits for the next connection.


Please see the specific content: http://blog.csdn.net/simonchi/article/details/40785441



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.