質問

I have my server program binding fine to 0.0.0.0 (INADDR_ANY) or 127.0.0.1 (INADDR_LOOPBACK), however I want the program to listen on my local network IP (192.168.1.24) and I keep getting this error:

Error opening the listening port 8888 (Raw TCP output): Cannot assign requested address

Here is the relevant code:

#define LOCAL_IP ((unsigned long int) 0x1801A8C8) //192.168.1.24
#define PORT 8888

struct sockaddr_in sa;

sa.sin_family = AF_INET;
sa.sin_port = htons(PORT);
sa.sin_addr.s_addr = LOCAL_IP; /* Bind servers to local net*/
//sa.sin_addr.s_addr = htonl(INADDR_ANY);

I've also tried htonl(LOCAL_IP) and inet_addr("192.168.1.24") with no luck.

役に立ちましたか?

解決

The IP you use is: 200.168.1.24

Use:

sa.sin_addr.s_addr = inet_addr("192.168.1.24");

instead.
Make sure the port isn't already in use. (use the program netstat) Have you closed the socket correctly, at tests before?

他のヒント

you can not assign any string or integer to the sa.sin_addr.s_addr.

So you need to cast it into suitable format. The statement you have written sa.sin_addr.s_addr = LOCAL_IP will be replaced as

sa.sin_addr.s_addr =192.168.1.24

Which is causing the problem.

Use inet_addr(LOCAL_IP) instead, it should work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top