LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-18-2009, 11:37 AM   #1
mhogue
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Rep: Reputation: 0
Am I continuously creating a socket?


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);

if(sendto(nu, &msg,sizeof (msg),0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
{
perror("sendto");
exit(1);
}

}



Or should my code be set up this way instead


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);




case COUPLED_MODE :
{

if(sendto(nu, &msg,sizeof (msg),0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
{
perror("sendto");
exit(1);
}

}



Any insight would be great

Last edited by mhogue; 11-18-2009 at 12:31 PM.
 
Old 11-18-2009, 01:17 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,285
Blog Entries: 1

Rep: Reputation: 103Reputation: 103
Yes, the first piece of code creates a socket for each execution of the case.
Yes, you should use the second approach.
 
Old 11-19-2009, 06:22 AM   #3
mhogue
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Hmm interesting but when i run the second approach each time I receive from the socket I still see the

cout << "creating new socket"<< endl;

cout <<"setting new destination address " << endl;

prior to the sending to my next socket,any thoughts?
 
Old 11-20-2009, 09:10 AM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,285
Blog Entries: 1

Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by mhogue View Post
any thoughts?
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.
 
Old 11-23-2009, 06:31 AM   #5
mhogue
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Heres my code currently and this works fine for as long as im running it


void *OPEN_INFO(void *arg)
{
struct sockaddr_in addr;
int fd, nu, nbytes,addrlen,loc = 0, sentSize = 0;
struct ip_mreq mreq;
char msgbuf[MSGBUFSIZE];
INFO_STRUCT* infoOutputs= &stIntI.DATA->INFO;
char buffer[500];
memset(buffer, 0, 500);

init();

u_int yes=1;

/* create UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}


/* 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);
}

while (1) {

addrlen=sizeof(addr);
if ((nbytes=recvfrom(fd,msgbuf,MSGBUFSIZE,0,
(struct sockaddr *) &addr, (socklen_t*) &addrlen)) < 0) {
perror("recvfrom");
exit(1);
sleep(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);


memset(&intraInfo,0, sizeof(STEVE_MSG_HEADER));

intraInfo.opcode = 1;
intraInfo.version = 1;
intraInfo.sysID = 3;

int structSize = sizeof(infoOutputs);

float tempSize = (float) structSize/ (float)sizeof(intraInfo.data);
tempSize += 0.5;

intraInfo.totalMsgs = static_cast<int>( round(tempSize));


for (int i=1; i <=intraInfo.totalMsgs; i++)
{


memset (&intraInfo.data, 0, sizeof(intraInfo.data));

intraInfo.msgNum = i;


char *initPtrVal = (char *) &infoOutputs;
int *ptrVal = (int *)&infoOutputs;
loc = (int)ptrVal - (int)initPtrVal;

// intraInfo.memOffset = (i-1)*sizeof(intraInfo.data);
intraInfo.msgNum = i;


char *initPtrVal = (char *) &infoOutputs;
int *ptrVal = (int *)&infoOutputs;
loc = (int)ptrVal - (int)initPtrVal;
);
intraInfo.memOffset = 0;

if (i == intraInfo.totalMsgs)
{
intraInfo.byteCount = sizeof(on);
memcpy(&intraInfo.data, &on, intraInfo.byteCount);
else
{
intraInfo.byteCount = sizeof(intraInfo);
memcpy(&intraInfo.data, initPtrVal + sentSize, intraInfo.byteCount);
}


if(sendto(nu, &intraInfo,sizeof (intraInfo),0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
{
perror("sendto");
exit(1);
}
close(2);
}
}// closes Coupled case
break;
default :

break;

} //closes the switch
}
};

Last edited by mhogue; 11-23-2009 at 06:35 AM.
 
Old 11-26-2009, 12:40 PM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,285
Blog Entries: 1

Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by mhogue View Post
Heres my code currently and this works fine for as long as im running it
Since you do not close the socket descriptors anywhere, it looks like your code leaks resources.

However, if "OPEN_INFO" is only called once by a program that runs to completion, the O/S will clean up any resources that you forgot about.

How did you get your code to compile without returning a "void *" from "OPEN_INFO"?

Last edited by David1357; 11-26-2009 at 12:48 PM. Reason: Completely changed my reply
 
Old 11-26-2009, 12:47 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
In other words:

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.

That's it!

'Hope that helps .. PSM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a sender socket blocks a listening socket murahman Programming 2 09-22-2008 06:55 PM
fedora - syslog not creating socket file shinepuppy Linux - Software 4 08-02-2007 12:45 PM
FC4: Error creating netlink socket jakubi Fedora 3 07-10-2006 12:29 PM
creating server socket program to handle multiple client at same time cranium2004 Programming 2 03-14-2005 10:58 AM
Creating socket programs as daemon cranium2004 Programming 2 03-04-2005 04:42 AM


All times are GMT -5. The time now is 01:11 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration