(RPM) in Linux network UDP communication, the understanding of whether the client is bound

Source: Internet
Author: User
Tags htons

A recent example is done with the RTSP protocol. The server has already, just need to do the client to do the line, in the process of the discovery of a number of problems, is about the UDP client binding problem.

Perhaps everyone in the book to see that most of the UDP client does not need to bind, can directly communicate with the server, at first I also think so, and I also do so, but do do to find that there is a problem.


In UDP communication, we set up a server, bind, wait for the client connection request, now the server code to paste out briefly:

server.c********************************************/////////////////// /////////////////

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
#define Port 8090


int main ()
{
Char Buf[buf_size],buf1[buf_size];
int Fd,n,len;
Char buf2[20]= "server:\n";
struct sockaddr_in serv,serfrom;
if ((Fd=socket (af_inet,sock_dgram,0) <0))
{
printf ("sock err\n");
Exit (1);
}
printf ("sock ok\n");


Serv.sin_family=af_inet;
Serv.sin_addr.s_addr=inaddr_any;
Serv.sin_port=htons (port);


if (Bind (FD, (struct sockaddr*) &serv,sizeof (serv)) <0)
{
printf ("Bind err\n");
Exit (1);
}


Len=sizeof (Serfrom);


while (1)
{
N=recvfrom (Fd,buf,sizeof (BUF), 0, (struct sockaddr*) &serfrom,&len);
if (n<0)
{
printf ("recv err\n");
Exit (1);
}
buf[n]= ' + ';
printf ("%s\n", buf);


if (strncmp (buf, "Bye", 3) ==0)
{

Break

}
Else
{
printf ("input:\n");
scanf ("%s", BUF1);
strcat (BUF2,BUF1);
SendTo (Fd,buf2,sizeof (BUF2), 0, (struct sockaddr*) &serfrom,len);
memset (buf2+10,0,sizeof (buf2+10));
}


}

Close (FD);

return 0;

}

client.c**********************************///////////////////////// //////////


#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
#define Port 8090


int main (int argc,char *argv[])
{
Char Buf[buf_size],buf1[buf_size],buf2[buf_size];
int Fd,n,len;
struct sockaddr_in serfrom;
if ((Fd=socket (af_inet,sock_dgram,0) <0))
{
printf ("sock err\n");
Exit (1);
}
printf ("sock ok\n");


Bzero (&serfrom,sizeof (Serfrom));
Serfrom.sin_family=af_inet;

if (Inet_pton (AF_INET,ARGV[1],&SERFROM.SIN_ADDR) <0)
{
printf ("inet err\n");
Exit (1);
}
Serfrom.sin_port=htons (port);
Bzero (& (Serfrom.sin_zero), 8);


Len=sizeof (Serfrom);
while (1)
{
printf ("input:\n");
scanf ("%s", BUF1);
N=sendto (Fd,buf1,sizeof (BUF1), 0, (struct sockaddr*) &serfrom,len);
if (n<0)
{
printf ("Send err\n");
Exit (1);
}
Else
{
N=recvfrom (Fd,buf2,sizeof (BUF2), 0, (struct sockaddr*) &serfrom,&len);
buf[n]= ' + ';
printf ("%s\n", buf2);

}

}

Close (FD);
return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////


The above is the UDP server and client code, you can see that the client is not bound, on the server side bound, why the client is not bound, and the server can send data to the client? The crux of the problem is on the client's sendto () function, N=sendto (fd,buf1,sizeof (BUF1), 0, (struct sockaddr*) &serfrom,len), in his fifth parameter struct Sockaddr, (in SendTo, the Serfrom should have the destination IP and port). Before the client has to send data to the server, the server is unaware of its existence, where I did not bind him, but behind this, the system has done some binding operation. The system in the current system to find a non-occupied port to it, and then bind, so that the client after SendTo (), on the server has N=recvfrom (fd,buf,sizeof (BUF), 0, (struct sockaddr*) & Serfrom,&len), (in Recvfrom, Serfrom should have the IP and port number of the data source), the IP address of the client and the port number selected by the system will be populated in this struct sockaddr struct (i.e. serfrom), This allows the server to know the presence of the client, including its IP as the port number. So they can communicate. However, this is the case where the client first sends information to the server (that is, the client sendto first, then the server recvfrom). If the client does not send a message to the server at the beginning, the function of the client is simply to receive the message from the server. At this time the server does not know the client's IP and port number, so how they should communicate??? This is also the problem I have encountered in this project. In the RTSP communication, the first established is TCP and the server to connect, and then use two UDP port to accept the server's data, when the TCP connection is completed and closed, and UDP to accept data, this is equivalent to the real UDP communication,  However, the client is not sending the data to the server (as in the case above), so how does the server know which client to send? Only the client is bound. I've changed the code above,


server.c************************////////////////////////////// ///////


#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
#define Port 8090


int main ()
{
Char Buf[buf_size],buf1[buf_size];
int fd,n,len,i=0;
Char buf2[20]= "server:\n";
struct sockaddr_in serv,serfrom;
if ((Fd=socket (af_inet,sock_dgram,0)) <0)
{
printf ("sock err\n");
Exit (1);
}
printf ("sock ok\n");


Bzero (&serv,sizeof (serv));
Serv.sin_family=af_inet;
Serv.sin_addr.s_addr=inaddr_any;
Serv.sin_port=htons (port);
Bzero (&serv.sin_zero,8);


Bzero (&serfrom,sizeof (Serfrom));
Serfrom.sin_family=af_inet;
Serfrom.sin_port=htons (4681);
Serfrom.sin_addr.s_addr=inaddr_any;
Bzero (&serfrom.sin_zero,8);


if (Bind (FD, (struct sockaddr*) &serv,sizeof (serv)) <0)
{
printf ("Bind err\n");
Exit (1);
}

Len=sizeof (Serfrom);

while (1)
{
SendTo (Fd,buf2,sizeof (BUF2), 0, (struct sockaddr*) &serfrom,len);
Sleep (0.1);
}

Close (FD);
return 0;
}
In the above code, the server does not accept data only send data to the client, send the destination IP and port in Serfrom, IP address by default, the port number is 4681, that is, the server to the IP 4681 port to send data, and then look at the following client's code:

server.c***************************///////////// //////////////////////////////////////////////////////

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
#define Port 4681


int main (int argc,char *argv[])
{
Char Buf[buf_size],buf1[buf_size],buf2[buf_size];
int Fd,n,len;
struct sockaddr_in serfrom,self;
if ((Fd=socket (af_inet,sock_dgram,0)) <0)
{
printf ("sock err\n");
Exit (1);
}
printf ("sock ok\n");

Len=sizeof (self);

Bzero (&serfrom,sizeof (Serfrom));
Serfrom.sin_family=af_inet;
Serfrom.sin_addr.s_addr=inaddr_any;

Serfrom.sin_port=htons (port);
Bzero (& (Serfrom.sin_zero), 8);


if (Bind (FD, (struct sockaddr*) &serfrom,sizeof (serfrom)) <0)
{
printf ("Bind err\n");
Exit (1);
}

while (1)
{

N=recvfrom (Fd,buf2,sizeof (BUF2), 0, (struct sockaddr*) &serfrom,&len);
buf[n]= ' + ';
printf ("%s\n", buf2);


}

Close (FD);
return 0;

}

I have the client bound with Port 4681, as long as the server has data to send, the client can accept, but this can only handle fixed-point clients.


In RTSP, we specify the client's accept port in the Setup command, and when the client sends a play command over TCP to the server, the server starts sending data to the port we specify in Setup, and we can accept the data sent by the server just by binding the client to those ports. So, in UDP, when to bind, when do not need to bind, we have to see the situation, because for the later stage of RTSP, the client does not send data to the server, only let the server to the specified port, and then we bind the client there, so that it can be .... OK, I should go to bed, too,

Reference:

1, in the Linux network UDP communication, the understanding of whether the client is bound

http://blog.csdn.net/chenyu123123/article/details/8518642

(RPM) in Linux network UDP communication, the understanding of whether the client is bound

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.