ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I’m new to socket programming please be gentle with responses but I feel like I have a dumb question but here it goes, so If have the following code, each time the case is called am I going to recreate the socket ? I have a loop prior to the switch.
case COUPLED_MODE :
{
cout << "creating new socket"<< endl;
//setting up another socket
if ((nu=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
cout <<"setting new destination address " << endl;
//setting up another destination ddress
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(NEW_GROUP);
addr.sin_port=htons(NEW_PORT);
I can't intelligently comment any further without seeing the rest of your code or at least an outline of what how your code works.
I can make a stab in the dark and say that if then entire second block of code gets executed every time you receive a packet, then you are going to create a socket every time you receive a packet.
Ideally, you want to create your sockets in some sort of initialization routine, use them for the life of your program, and then close them in a cleanup routine. If you are not using this approach, and your program runs for a long time, you will eventually use up all of the sockets available for your application.
/* allow multiple sockets to use the same PORT number */
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
/*setting up destination address*/
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(INTRA_IOS_PORT);
// bind to receive address
if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
perror("bind");
exit(1);
}
/* use setsockopt() to request that the kernel join a multicast group */
mreq.imr_multiaddr.s_addr=inet_addr(INFO_GROUP);
cout << INFO_GROUP <<endl;
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
perror("setsockopt");
exit(1);
}
switch (s_mapModeValues[msgbuf])
{
case COUPLED_MODE :
{
bool on = true;
//setting up another socket
if ((nu=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
//setting up another destination ddress
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(NEW_GROUP);
addr.sin_port=htons(NEW_PORT);
1. However/wherever you open your datagram socket, make sure there's a corresponding "close ()". Each successful call to "socket()" should be matched by exactly one "close()".
2. You can "sendto()" or "recvfrom()" your open datagram socket as many times as you like until you "close()" it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.