c++ upd brodcast client
-
hi I'm programming a c++ client to receive packets from a server (UDP broadcast). This is a hardware switch (One direction only) system so I have no information regarding the senders address or any other info. It all compiles and binds OK but no packets received.
WSAStartup(0x0101, &_wsaData);
memset((char *)&_sockaddr, 0, sizeof(_sockaddr));
_sockaddr.sin_addr.s_addr = _nAdapterIp;
_sockaddr.sin_family = _nadressFam;
_sockaddr.sin_port = _usPort; //Gave it a try with (0) - also not working
_socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
char broadcast='1';
setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, &broadcast ,sizeof(broadcast));
bind(_socket, (sockaddr*) & _sockaddr, sizeof (_sockaddr));
char* recvbuf = new char[_nPacketSize + 1];
int nSize = recv(_socket, recvbuf, _nPacketSize + 1, 0);Any ideas? Thanks,
-
hi I'm programming a c++ client to receive packets from a server (UDP broadcast). This is a hardware switch (One direction only) system so I have no information regarding the senders address or any other info. It all compiles and binds OK but no packets received.
WSAStartup(0x0101, &_wsaData);
memset((char *)&_sockaddr, 0, sizeof(_sockaddr));
_sockaddr.sin_addr.s_addr = _nAdapterIp;
_sockaddr.sin_family = _nadressFam;
_sockaddr.sin_port = _usPort; //Gave it a try with (0) - also not working
_socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
char broadcast='1';
setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, &broadcast ,sizeof(broadcast));
bind(_socket, (sockaddr*) & _sockaddr, sizeof (_sockaddr));
char* recvbuf = new char[_nPacketSize + 1];
int nSize = recv(_socket, recvbuf, _nPacketSize + 1, 0);Any ideas? Thanks,
One important thing to do is look at the return codes for the methods that you are calling and your socket constructor, most times they will give you a clue about what is wrong. Also, what are values _nAdapterIp, _nadressFam, and _usPort? Could you be suffering from endian type issues?
-
One important thing to do is look at the return codes for the methods that you are calling and your socket constructor, most times they will give you a clue about what is wrong. Also, what are values _nAdapterIp, _nadressFam, and _usPort? Could you be suffering from endian type issues?
sorry abut that, I shorten it so the code will be clear.... The actual code has the tests:
if (WSAStartup(0x0101, &_wsaData) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}char broadcast= (char)1;
if (setsockopt(\_socket, SOL\_SOCKET, SO\_BROADCAST, &broadcast ,sizeof(broadcast)) != 0) { fprintf(stderr, "Could not set socket options.\\n"); WSACleanup(); exit(0); }
etc....
-
sorry abut that, I shorten it so the code will be clear.... The actual code has the tests:
if (WSAStartup(0x0101, &_wsaData) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}char broadcast= (char)1;
if (setsockopt(\_socket, SOL\_SOCKET, SO\_BROADCAST, &broadcast ,sizeof(broadcast)) != 0) { fprintf(stderr, "Could not set socket options.\\n"); WSACleanup(); exit(0); }
etc....
arishri wrote:
_socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
How could you tell it wasn't working? I assume IPPROTO_UDP equates to '17'. When your application is running, and you go to DOS prompt and do a "netstat -a" do you see your socket with the proper port listed?
-
arishri wrote:
_socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
How could you tell it wasn't working? I assume IPPROTO_UDP equates to '17'. When your application is running, and you go to DOS prompt and do a "netstat -a" do you see your socket with the proper port listed?
-
arishri wrote:
_socket = socket(_nadressFam, SOCK_DGRAM, 0); //Gave it a try with (17) - UDP for the 3 rd parameter - not working
How could you tell it wasn't working? I assume IPPROTO_UDP equates to '17'. When your application is running, and you go to DOS prompt and do a "netstat -a" do you see your socket with the proper port listed?
-
_socket = socket(_nadressFam, SOCK_DGRAM, 17);
_socket1 = socket(_nadressFam, SOCK_DGRAM, 17);if ((\_socket == INVALID\_SOCKET) || (\_socket1 == INVALID\_SOCKET)) { fprintf(stderr, "Could not create socket.\\n"); WSACleanup(); exit(0); }
-
One important thing to do is look at the return codes for the methods that you are calling and your socket constructor, most times they will give you a clue about what is wrong. Also, what are values _nAdapterIp, _nadressFam, and _usPort? Could you be suffering from endian type issues?
#include
main()
{
char m;
int p;
float n;
scanf ("%s%d%f",&m,&p,&n);
}