문제

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