[Excerpt from-socket-Learning] Reuse of socket port in TCP&UDP communication

Source: Internet
Author: User
Tags sprintf strcmp usleep htons
In many cases, we need to use a fixed IP and port number to communicate, and by default, the client side of the port number is not fixed, by the system automatically allocate idle port number to communicate;
But in many cases, some services to write more stringent, only to receive fixed IP and port number of data, we all know that IP address we can guarantee that the same, but the port number by default is not possible to achieve the same, this time we need to the port number for special treatment;
Think of serve slogan fixed, the default we think of using the BIND function binding port number for data communication, the idea is correct, but we actually write code to test the time we found wrong;
Bind cannot bind to the same IP and port number as the server. But you can bind the port number and then send, this time really implemented the client-side fixed IP and port number to send data, then the question is, how do we implement and listen to the service using the same IP and port number. How do I get bind to take effect both on the listener and the sender?
Baidu on the socket communication in the port number of some knowledge, found a special word "reuse", yes, the socket can be used for IP address and port number reuse, that is, listening and sending use of the same IP and port number;
Found the data to start Baidu How to reuse the port number operation, found a key function: setsockopt
What is the meaning of this function can be the use of Baidu, in addition to note that the IP and port number multiplexing parameters are: so_reuseaddr
Note: When listening and sending, the function of setsockopt must be used at the same time.
The UDP service listens for sample code:
Static void* create_udpserver_tread (void *point) {int sockfd; int len; int recvlen; unsigned char buf[buf_size]; Char Lo G[buf_size]; Char Temp[8]; struct sockaddr_in adr_inet; struct sockaddr_in adr_clnt; Bzero (&adr_inet, sizeof (adr_inet)); adr_inet.sin_family = af_inet; if (strcmp (server_ip, (char*) "NULL") = = 0 | | strlen (SERVER_IP) <= 0) {adr_inet.sin_addr.s_addr = htonl (Inaddr_any);} El se {adr_inet.sin_addr.s_addr = inet_addr (server_ip);} adr_inet.sin_port = Htons (atoi (Server_port)); len = sizeof (ADR_CLNT); SOCKFD = socket (af_inet, SOCK_DGRAM, 0); if (sockfd = = 1) {memset (log,0,sizeof (log)); sprintf (log, "UDP socket init fail! sockfd=%d\n", SOCKFD); Printflog (log,1); return NULL; }else{memset (log,0,sizeof (log)); sprintf (log, "UDP socket init success! sockfd=%d\n", SOCKFD); Printflog (log,0); bool Breuseaddr = true; int optret=setsockopt (sockfd,sol_socket,so_reuseaddr,&breuseaddr,sizeof (&AMP;BREUSEADDR)); if (optret!=0) {return NULL;} int ret = bind (SOCKFD, struct sockAddr *) &adr_inet, sizeof (adr_inet)); if (ret = = 1) {memset (log,0,sizeof (log)); sprintf (log, "UDP socket bind fail! sockfd=%d\n", SOCKFD); Printflog (log,1); return NULL; else {memset (log,0,sizeof (log)); sprintf (log, UDP socket bind success! sockfd=%d\n, SOCKFD); Printflog (log,0); while (1) {Recvlen = Recvfrom (sockfd, buf, sizeof (BUF), 0, (struct sockaddr *) &adr_clnt, (socklen_t *) &len); if ( Recvlen < 0) {usleep; continue;} char* ClientIP = Inet_ntoa (ADR_CLNT.SIN_ADDR); Long ClientPort = Ntohs (Adr_clnt.sin_port); memset (log, 0, sizeof (log)); sprintf (log, "UDP clientip=%s,clientport=%d,recvlen=%d,recvdate=", ClientIP, Clientport,recvlen); for (int i=0; i<recvlen;i++) {memset (temp, 0, sizeof (temp)); sprintf (temp, "%02x", Buf[i]); strcat (log, temp); } strcat (log, "\ n"); if (strcmp (Client_port, (char*) "NULL")!= 0) {clientport = Atoi (Client_port);} Checkrecvinfo (BUF, Recvlen,-1, ClientIP, ClientPort, (char*) "UDP"); Usleep (10); Close (SOCKFD); return NULL; }
UDP Sender Sample code: int senddatabyudp (char* clientip, int clientport, int serverport, unsigned char* msg, int len) {int sockfd; I NT result = 0; struct sockaddr_in adr_srvr, adr_client; Bzero (&adr_srvr, sizeof (ADR_SRVR)); adr_srvr.sin_family = af_inet; ADR_SRVR.SIN_ADDR.S_ADDR = inet_addr (ClientIP); Adr_srvr.sin_port = htons (ClientPort); adr_client.sin_family = af_inet; ADR_CLIENT.SIN_ADDR.S_ADDR = htonl (Inaddr_any); Adr_client.sin_port = htons (ServerPort); SOCKFD = socket (af_inet, SOCK_DGRAM, 0); if (sockfd = = 1) {return-2} bool breuseaddr = true; int optret=setsockopt (sockfd,sol_socket,so_reuseaddr,&breuseaddr,sizeof (&AMP;BREUSEADDR)); if (optret!=0) {return-3} int ret=bind (SOCKFD, struct sockaddr *) &adr_client, sizeof (adr_client)); if (ret<0) {return-4} result = SendTo (SOCKFD, MSG, Len, 0, (struct sockaddr*) &adr_srvr, sizeof (ADR_SRVR)); If (Result >= 0) {close (SOCKFD); }

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.