How do I initiate a 4 billion TCP long connection from one client to the server?

Source: Internet
Author: User
Tags htons

Original link: http://blog.yeyuzhen.cn/?p=193

This is by no means the title party. In fact, this is a "popular science" article, about "socket= ((Client_ip:client_port)-(Server_ip:server_port), Pocotol)" a general misunderstanding.

Because of this misunderstanding, I have turned a test task into a "social activity." To test the performance of the newly developed TCP long connection server, we need to have a test server maintain a 100w+ long connection. A client can initiate a maximum number of long connections of 65535 (this is common sense.) )。 After a panel discussion, we need to find 17 test clients by initiating a 6W long connection with a client. So we put the test server on the company intranet only one server. wrote a benchmark program under Windows, distributing a colleague through a variety of "social channels". Kungfu not negative, finally pressure to 100w+ above the TCP long connection. So happy~~~

Back to summarize the test task-we have a misunderstanding of the socket, the collective silly B once.

In fact, by carefully scrutinizing the definition of the socket, we can find that the maximum number of TCP long connections that a client initiates to a service function should be 4 billion + (65535*65535), rather than the generally accepted 65535. Because:

+-------------+         +-------------+
| Client Host |         | Server Host |
+-------------+         +-------------+
|             |    A | | | |             /-------Port 54000    |
|    Port 59000/| | |             \        |             |
|             | \-------Port 54001    | | | B    |             |
+-------------+         +-------------+

From the definition of the socket "socket= ((Client_ip:client_port)-(Server_ip:server_port), pocotol)" We can know that A and B are actually different connections, This shows that a client's port is a different port that can be connected to the server. If the Server host opens 65,535 service-side listening ports, then a Client Host port can initiate 65,535 connections. The client Host has 65,535 ports in total, so the maximum number of connections that clients can theoretically initiate to the server should be 65535*65535≈40 billion +.

Let's actually verify that:

Socket Server #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #inc Lude <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <pthread.h> using S

Td::memset;

	void *sockethandle (void *_socket) {int client_socket = * (int *) _socket);
	struct sockaddr_in sa_client;
	unsigned int sa_client_len = sizeof (sa_client);

	memset (&sa_client, 0x00, Sa_client_len);
	Getpeername (client_socket, struct sockaddr *) &sa_client, (socklen_t *) &sa_client_len);
	Char Ip[inet_addrstrlen + 1] = {0};
	memset ((void *) IP, 0x00, sizeof (IP));
	Inet_ntop (Af_inet, (void *) & (sa_client.sin_addr), IP, Inet_addrstrlen);
	unsigned short int port = Ntohs (Sa_client.sin_port);

	printf ("Client%s:%d\n", IP, Port);
	Char recv_buf[2048] = {0};
	memset (&recv_buf, 0x00, sizeof (RECV_BUF));

	int recv_size = 0; while (true) {if (recv_size = recv (Client_socket, Recv_buf, 2040, 0)) > 0) {printf ("recv From [%s:%d]:%s\n, IP, port, recv_buf);
			memset (&recv_buf, 0x00, sizeof (RECV_BUF));
			if (!strcmp (Recv_buf, "Close")} {break;
			else if (0 = recv_size) {printf ("Client%s:%d disconnected.\n", IP, Port);
			Fflush (stdout);
		Break
			else//-1 {printf ("Recv failed.\n");
		Break

	} shutdown (Client_socket, shut_rdwr);
return (NULL);
		int main (int argc, char *argv[]) {if (ARGC < 2) {printf ("Usage:./socketserver <listen_port>\n");
	return (1);
	int listen_port = atoi (argv[1]);
	int server_socket = socket (af_inet, sock_stream, 0);
	if ( -1 = server_socket) {printf ("Could not create server socket.\n");
	int reuse = 1;

	SetSockOpt (Server_socket, Sol_socket, so_reuseaddr, &reuse, sizeof (reuse));
	struct sockaddr_in sa_server;
	memset (&sa_server, 0x00, sizeof (Sa_server));
	sa_server.sin_family = af_inet;
	SA_SERVER.SIN_ADDR.S_ADDR = Inaddr_any;

	Sa_server.sin_port = htons (Listen_port); if (Bind (Server_sockeT, (struct sockaddr *) &sa_server, sizeof (sa_server)) < 0) {printf ("Bind Server Socket fail!\n");
	return (1);

	} Listen (Server_socket, 10);
	struct sockaddr_in sa_client;
	int sa_client_len = sizeof (sa_client);

	memset (&sa_client, 0x00, Sa_client_len);
	int client_socket = 0; while (true) {Client_socket = Accept (Server_socket, (struct sockaddr *) &sa_client, (socklen_t *) &sa_client_len)
		;
			if (Client_socket < 0) {printf ("Accept fail!");
		return (1);
		} pthread_t thread_id;
		pthread_attr_t thread_attr;
		Pthread_attr_init (&AMP;THREAD_ATTR);
		Pthread_attr_setdetachstate (&thread_attr, pthread_create_detached);
		Pthread_create (&thread_id, &thread_attr, SocketHandle, (void *) &client_socket);
	Pthread_attr_destroy (&AMP;THREAD_ATTR);
return (0);
 }
Socket Client #include <cstdio> #include <cstdlib> #include <cstring> #include <unistd.h> #inc Lude <sys/socket.h> #include <arpa/inet.h> int main (int argc, char *argv[]) {if (ARGC < 4) {printf ("U
		Sage:./socketclient <client_port> <server_ip> <server_port>\n ");
	return (1);
	} unsigned short int client_port = Atoi (argv[1]);

	unsigned short int server_port = Atoi (argv[3]);
	int client_socket = socket (af_inet, sock_stream, 0);
	if ( -1 = client_socket) {printf ("Create client Socket fail.\n");
	int reuse = 1;

	SetSockOpt (Client_socket, Sol_socket, so_reuseaddr, &reuse, sizeof (reuse));
	struct sockaddr_in sa_client;
	memset (void *) &sa_client, 0x00, sizeof (sa_client));
	sa_client.sin_family = af_inet;

	Sa_client.sin_port = htons (Client_port); if (Bind (Client_socket, (struct sockaddr *) &sa_client, sizeof (sa_client)) < 0) {printf ("Bind client socket fail!
		\ n ");
	return (1); } struct SOCKADDR_INSa_server;
	memset (&sa_server, 0x00, sizeof (Sa_server));
	SA_SERVER.SIN_ADDR.S_ADDR = inet_addr (argv[2]);
	sa_server.sin_family = af_inet;

	Sa_server.sin_port = htons (Server_port); If Connect (client_socket, (struct sockaddr *) &sa_server, (socklen_t) sizeof (sa_server)) < 0) {printf ("Connectin
		G to server fail.\n ");
	return (1);
	} char send_buf[1024] = {0};
	memset (&send_buf, 0x00, sizeof (SEND_BUF));
	Char recv_buf[2048] = {0};

	memset (&recv_buf, 0x00, sizeof (RECV_BUF));

	strcpy (SEND_BUF, "Hello socket server.");
			while (true) {if (Send (Client_socket, send_buf, sizeof (SEND_BUF), 0) < 0) {printf ("Send fail.\n");
			Fflush (stdout);
		Break
		int recv_size = 0;
			if ((Recv_size = recv (Client_socket, Recv_buf, 2040, 0)) > 0) {printf ("recv:%s\n", recv_buf);
		memset (&recv_buf, 0x00, sizeof (RECV_BUF));
			else if (0 = recv_size) {printf ("Client socket disconnected.\n");
			Fflush (stdout);
		Break else//-1 {priNTF ("Recv failed.\n");
		Break
	} shutdown (Client_socket, shut_rdwr);
return (0);

 }

The verification program is simple, the key is the client side to do bind operations, so that multiple clients use the same port to initiate the connection.

As you can see from the log run, the 54000 and 54001 server terminals have received a connection originating from Port 59000.

The misunderstanding of the basic knowledge of the computer, the influence is very far-reaching. Perhaps a misunderstanding directly limits your mind to a dead end. Perhaps a correct and profound understanding will allow you to think of a breakthrough solution.

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.