winsock2 connection works localy but not "global"
Hi
I have gotten my app to connect to my other program using winsock2, the connection is very stable when trying it on my computer(127.0.0.1 ip) but as soon as I trie to connect to another computer loacted on the net I recive a windows error message 5-10sec after it has been initialized and it shuts down.
so i wounder if there is something special I should think about when connecting via the net??
please help!
and to where are you connecting? are you sure it''s a valid IP address (does it respond to pings for instance)? what are you trying to do with it, and how do you know the server supports whatever it is you are trying to do?
-me
-me
im using a client on one computer that connects to a sever that''s on another computer... im only sending a small package but after 5 sec into the game (that im trying to make it for )one of the sides recives the error from windows....using XP so it dosen''t say so much in the error...just if I want''t to report it to them...
I just got thinking that it could be somthing with the threading too...im quite new to it so..but this is how I have set it up now:
The thread func:
loop start..
WaitForSingleObject(mutexHandle, INFINITE);
recivedData = netHandle.reciveData();
ReleaseMutex(mutexHandle);
Sleep(10);
loop until done
//in the game loop i''ve got the following
loop
//render / update game...
sendData = CreatePackage(...);
netHandle.SendData(sendData);
//Lock the mutex until we read the data recived
WaitForSingleObject(mutexHandle, INFINITE);
//When accesed the mutex use the data for update;
Player.update(recivedData);
ReleaseMutex(mutexHandle);
loop
witch i think should work...
I just got thinking that it could be somthing with the threading too...im quite new to it so..but this is how I have set it up now:
The thread func:
loop start..
WaitForSingleObject(mutexHandle, INFINITE);
recivedData = netHandle.reciveData();
ReleaseMutex(mutexHandle);
Sleep(10);
loop until done
//in the game loop i''ve got the following
loop
//render / update game...
sendData = CreatePackage(...);
netHandle.SendData(sendData);
//Lock the mutex until we read the data recived
WaitForSingleObject(mutexHandle, INFINITE);
//When accesed the mutex use the data for update;
Player.update(recivedData);
ReleaseMutex(mutexHandle);
loop
witch i think should work...
i found that when moving from loopback/LAN to a real connection, i got problems caused by not waiting for the data transfer to complete. specifically, on the loopback/LAN every call to send() had successfully sent all the bytes i asked it to, but over the internet sometimes it didn''t. it was made worse by the fact that i had a cable connection and the friend i was sending to was on 56k modem. my end would send the data and quit before his end was even halfway through receiving it.
it''s a bit hard to help without knowing what netHandle does. if it is your own code, i suggest you check the transfer correctness of it (over internet) by making a simple file transfer test utility. see if you can correctly transfer say a 10Mb zip file with it (zip is good because if any bytes are lost or incorrect you will know because the received file will be unusable). also try the fast->slow connection combination if you can
it''s a bit hard to help without knowing what netHandle does. if it is your own code, i suggest you check the transfer correctness of it (over internet) by making a simple file transfer test utility. see if you can correctly transfer say a 10Mb zip file with it (zip is good because if any bytes are lost or incorrect you will know because the received file will be unusable). also try the fast->slow connection combination if you can
are you using gethostbyname or getaddrinfo anywhere to convert a hostname to an ip address
Well this is the code I''m using for the connection for
the client and the server...picked it up from a tutorial
im not using any of those functions in the code...I just specifie the adress I want to connect to..
//-------------------------
// Shutdown the connection
//-------------------------
void NetworkHandler::ShutdownConnection()
{
// Close the socket
closesocket(mySocket);
WSACleanup();
}
//----------------------------------------------------------------
// Server Functions
//----------------------------------------------------------------
int NetworkHandler::startupServer(unsigned short port) {
// startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
return 1;
}
// create my socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
// Error checking
if (serverSocket == SOCKET_ERROR) {
return 1;
}
// fill the address structure with appropriate data
server.sin_family = AF_INET; //Use the TCP protocoll for transfer
server.sin_port = htons(port); //setting the port for listening
server.sin_addr.s_addr = INADDR_ANY; //Handle all adresses
// Bind the socket
if (bind(serverSocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
closesocket(serverSocket);
return 1;
}
// Start the socket for listening
if (listen(serverSocket, 5) == SOCKET_ERROR) {
closesocket(serverSocket);
return 1;
}
if (serverSocket == -1) {
return 1;
}
return 0;
}
//-------------------------------------------------
// Start accepting connections from clients
//-------------------------------------------------
void NetworkHandler::acceptConnections()
{
//Accept connections from clients
mySocket = accept(serverSocket, 0, 0);
// Error checking
if (mySocket == SOCKET_ERROR)
{
// printf("Accept Failed!\n");
}
}
//-------------------------------------------------
// Recive data from the clients
//-------------------------------------------------
NetPackage NetworkHandler::reciveData()
{
// Get the size of the incoming data
nBytes = recv(mySocket, (char*)&messageSize, sizeof(messageSize), 0);
// Error checking
if (nBytes == SOCKET_ERROR) {
// printf("Recv Failed!\n");
}
messageSize = ntohl(messageSize);
NetPackage tempPlayer;
nBytes = recv(mySocket, (char*)&tempPlayer, messageSize, 0);
// check for errors
if (nBytes == SOCKET_ERROR) {
// printf("Recv Failed!\n");
}
return tempPlayer;
}
//----------------------------------------------------------------
// CLIENT FUNCTIONS
//----------------------------------------------------------------
//Sends the size of the package first and after thant, the entire package
void NetworkHandler::SendData(NetPackage newPackage)
{
// calculate the size of that data
messageSize = sizeof(newPackage); //strlen(buffer);
// Converts the byte ordering for the network
messageSize = htonl(messageSize);
// Send message about the size of the object we are going to send
nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0);
// Error checking
if (nBytes == SOCKET_ERROR) {
// printf("Send Failed!\n");
}
// Convert back to hosts byte ordering
messageSize = ntohl(messageSize);
// Send the object
nBytes = send(mySocket, (char*)&newPackage, messageSize, 0);
// Error checking
if (nBytes == SOCKET_ERROR) {
// printf("Send Failed!\n");
}
}
//-------------------------------------------------
// Start the client by connecting to specified server
//-------------------------------------------------
int NetworkHandler::StartupClient(unsigned short port, const char* serverName) {
// Startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
// printf("Could Not Start Up Winsock!\n");
return 1;
}
// Create my socket
mySocket = socket(AF_INET, SOCK_STREAM, 0);
// Error checking
if (mySocket == SOCKET_ERROR) {
// printf("Error Opening Socket!\n");
return 1;
}
// Set the proberties for the server socket
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(serverName);
const char *ip = inet_ntoa(server.sin_addr);
// Connect to the server
if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
//cout << "Error connecting to server!" << endl;
}
// cout << "Client Started" << endl;
return 0;
}
the client and the server...picked it up from a tutorial
im not using any of those functions in the code...I just specifie the adress I want to connect to..
//-------------------------
// Shutdown the connection
//-------------------------
void NetworkHandler::ShutdownConnection()
{
// Close the socket
closesocket(mySocket);
WSACleanup();
}
//----------------------------------------------------------------
// Server Functions
//----------------------------------------------------------------
int NetworkHandler::startupServer(unsigned short port) {
// startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
return 1;
}
// create my socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
// Error checking
if (serverSocket == SOCKET_ERROR) {
return 1;
}
// fill the address structure with appropriate data
server.sin_family = AF_INET; //Use the TCP protocoll for transfer
server.sin_port = htons(port); //setting the port for listening
server.sin_addr.s_addr = INADDR_ANY; //Handle all adresses
// Bind the socket
if (bind(serverSocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
closesocket(serverSocket);
return 1;
}
// Start the socket for listening
if (listen(serverSocket, 5) == SOCKET_ERROR) {
closesocket(serverSocket);
return 1;
}
if (serverSocket == -1) {
return 1;
}
return 0;
}
//-------------------------------------------------
// Start accepting connections from clients
//-------------------------------------------------
void NetworkHandler::acceptConnections()
{
//Accept connections from clients
mySocket = accept(serverSocket, 0, 0);
// Error checking
if (mySocket == SOCKET_ERROR)
{
// printf("Accept Failed!\n");
}
}
//-------------------------------------------------
// Recive data from the clients
//-------------------------------------------------
NetPackage NetworkHandler::reciveData()
{
// Get the size of the incoming data
nBytes = recv(mySocket, (char*)&messageSize, sizeof(messageSize), 0);
// Error checking
if (nBytes == SOCKET_ERROR) {
// printf("Recv Failed!\n");
}
messageSize = ntohl(messageSize);
NetPackage tempPlayer;
nBytes = recv(mySocket, (char*)&tempPlayer, messageSize, 0);
// check for errors
if (nBytes == SOCKET_ERROR) {
// printf("Recv Failed!\n");
}
return tempPlayer;
}
//----------------------------------------------------------------
// CLIENT FUNCTIONS
//----------------------------------------------------------------
//Sends the size of the package first and after thant, the entire package
void NetworkHandler::SendData(NetPackage newPackage)
{
// calculate the size of that data
messageSize = sizeof(newPackage); //strlen(buffer);
// Converts the byte ordering for the network
messageSize = htonl(messageSize);
// Send message about the size of the object we are going to send
nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0);
// Error checking
if (nBytes == SOCKET_ERROR) {
// printf("Send Failed!\n");
}
// Convert back to hosts byte ordering
messageSize = ntohl(messageSize);
// Send the object
nBytes = send(mySocket, (char*)&newPackage, messageSize, 0);
// Error checking
if (nBytes == SOCKET_ERROR) {
// printf("Send Failed!\n");
}
}
//-------------------------------------------------
// Start the client by connecting to specified server
//-------------------------------------------------
int NetworkHandler::StartupClient(unsigned short port, const char* serverName) {
// Startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
// printf("Could Not Start Up Winsock!\n");
return 1;
}
// Create my socket
mySocket = socket(AF_INET, SOCK_STREAM, 0);
// Error checking
if (mySocket == SOCKET_ERROR) {
// printf("Error Opening Socket!\n");
return 1;
}
// Set the proberties for the server socket
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(serverName);
const char *ip = inet_ntoa(server.sin_addr);
// Connect to the server
if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
//cout << "Error connecting to server!" << endl;
}
// cout << "Client Started" << endl;
return 0;
}
Do you call shutdown() on your socket before you call closesocket()? I just made that mistake myself today. I learned my lesson, after a lot of work. The server was closing the connection before the client received all the data that the server had sent before it closed the connection. shutdown() makes sure that everything gets sent first, apparently. Fixed my problem, at least.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement