Problem creating socket with C++ in winsock2
- by Ash85
Hi,
I'm having the weirdest problem causing me headaches. Consider the following code:
// Create and bind socket
std::map<Connection, bool> clients;
unsigned short port=6222;
struct sockaddr_in local_address, from_address;
int result;
char buffer[10000];
SOCKET receive_socket;
local_address.sin_family = AF_INET;
local_address.sin_addr.s_addr = INADDR_ANY;
local_address.sin_port = htons(port);
receive_socket = socket(AF_INET,SOCK_DGRAM,0);
What's happening is receive_socket is not binding, I get SOCKET_ERROR. When I debug the program and check receive_socket, it appears to just be garbled crap. I put a breakpoint on the 'std::map' line. When I step into each line of the above code, the debug cursor jumps straight from the 'unsigned short port' line to the first 'local_address.sin' line, even though I am using step into (F11), it does not stop at struct, int, char or SOCKET lines, it jumps straight over them.
At this point I hover my mouse over local_address, from_address, result, buffer and receive_socket. They are all full of garbled crap. Is this because I have not defined these variables yet? I've also noticed that when I reach the bottom of the above code, local_address.sin_port is set to 19992, but it should be 6222?