473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Networking: getaddrinfo using different ports

158 100+
My program sends commands to a different computer running a udp server.

My problem is that each different node is running on a different udp port and the code needs to take that into account by allowing the port to change depending on a int variable.

The to code snippets ive provided show what i got working and what i think should work but doesn't.

This Works.
Expand|Select|Wrap|Line Numbers
  1.  
  2.    const char* po = "49240";
  3.  
  4.     if ((rv = getaddrinfo(lightIp, po, &hints, &servinfo)) != 0) {
  5.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  6.  
  7.     }
  8.  
  9.  
This does not work.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.      udpPort = 49240;
  4.      const char* po = (char*)udpPort;
  5.  
  6.  
  7.     if ((rv = getaddrinfo(lightIp, po, &hints, &servinfo)) != 0) {
  8.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  9.  
  10.     }
  11.  
  12.  
Mar 16 '10 #1

✓ answered by kardon33

I was finally able to get it working with the fallowing code:

Expand|Select|Wrap|Line Numbers
  1.    int udpPort = 12312;
  2.    char buf[5];
  3.    sprintf(buf,"%d",udpPort);
  4.    po = (char*) &buf;
  5.  
  6.     if ((rv = getaddrinfo(lightIp, po, &hints, &servinfo)) != 0) {
  7.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  8.     }
  9.  
  10.  

4 6742
jkmyoung
2,057 Expert 2GB
It looks like you're casting 49240 as a memory address. udpPort appears to be an integer, not a char*.

You might want:
udpPort = "49240";
const char* po = (char*) &udpPort;
Mar 16 '10 #2
kardon33
158 100+
OK but how would you take an integer (udpPort) containing the number 49240 and set it to something that getaddrinfo could use? I got close with (const char* po = "49240"; ) but i haven't been able to get that to be dynamic using a variable.

Thanks for the help.
Mar 17 '10 #3
jkmyoung
2,057 Expert 2GB
Can you use the itoa function? http://www.cplusplus.com/reference/c.../cstdlib/itoa/
Mar 17 '10 #4
kardon33
158 100+
I was finally able to get it working with the fallowing code:

Expand|Select|Wrap|Line Numbers
  1.    int udpPort = 12312;
  2.    char buf[5];
  3.    sprintf(buf,"%d",udpPort);
  4.    po = (char*) &buf;
  5.  
  6.     if ((rv = getaddrinfo(lightIp, po, &hints, &servinfo)) != 0) {
  7.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  8.     }
  9.  
  10.  
Mar 23 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Hans Kamp | last post by:
I found a useful client/server combination for newbies at http://www.msdnaa.net/Resources/Display.aspx?ResID=1375 I typed both of the programs and I ran them on one of my 2 computers (I have a...
1
by: Rob R. Ainscough | last post by:
Does .NET framework 2.0 provide any TCP/IP support -- specifically looking to write a Service that will monitor TCP/IP packets across various ports. I was wondering if this can be accomplished via...
4
by: edcdave | last post by:
I'm trying to get MoinMoin 1.5.4 running with Python 2.3.4 (installed from an SCO Skunkworks binary). Python 2.3.4 (#1, Aug 27 2004, 18:22:39) on sco_sv3 One of the MoinMoin modules attempts to...
3
by: jeremy.todter | last post by:
I have a question...obviously I am developing an application, or two applications using c++ and openGL. They involve a basic structure where approximately 70 percent of the framework applies to...
1
by: mirandacascade | last post by:
I noticed the following lines from the connect() method of the HTTPConnection class within httplib: for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): af, socktype,...
6
by: Maxim Veksler | last post by:
Hello, I wish to do some low level network stuff using python. I've googled somewhat and came up with pylibpcap, trouble is I can't compile it on my Ubuntu 6.10 workstation. Can someone please...
2
by: John Nagle | last post by:
Here's a strange little bug. "socket.getaddrinfo" blows up if given a bad domain name containing ".." in Unicode. The same string in ASCII produces the correct "gaierror" exception. Actually,...
3
by: Nathan | last post by:
Hi, I'm trying to do some network programming, but when I use 'struct addrinfo' or getaddrinfo, I see errors: gcc --std=c99 -Wall ipv6_test.c -o it ipv6_test.c: In function `main':...
1
by: Karl Chen | last post by:
I've discovered that since glibc 2.3.2, getaddrinfo(3) supports a useful flag called AI_ADDRCONFIG. It turns off AAAA lookups if the machine isn't configured for IPv6 (and similarly for IPv4,...
7
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.