Jump to content
  • Advertisement
Sign in to follow this  
Gullanian

Winsock 2 not sending

This topic is 4346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I'm writing a simple application using Winsock. I send the data to the server with the send() function. This works every now and then, but most of the time it does not send the data until I call the closeSocket() function. Once this is called, the data seems to send. Anyone know what's going on here? Is there some sort of flush command? Thanks, Tom

Share this post


Link to post
Share on other sites
Advertisement

// Build up the message to send to the server
string message = "LOGIN|";
message += username + "|" + password;

// Create socket
theSocket = connectToServer();

// Send the message
sendLine(theSocket, message);

//destroyServerConnection(theSocket);




The data only sends to the server side application when the destroy server connection function is not commented out.

Also, when running in debug all appears to run fine, every line in sendLine() works fine, but it just doesn't actually send the data.

Below are the functions called above:


// Send a message to the server
void sendLine(SOCKET sock, string str)
{
str += "\n";
int err = send(sock, str.c_str(), str.length(), 0);
}





// Create connection to server
SOCKET connectToServer()
{
WORD sockVersion;
WSADATA wsaData;

// Set version of winsock to use
sockVersion = MAKEWORD(2, 0);

// Initialize winsock
WSAStartup(sockVersion, &wsaData);

// Store information about the server
LPHOSTENT hostEntry;

/*
// USE THIS IF USING SERVER IP
in_addr iaHost;
iaHost.s_addr = inet_addr(SERVER_ADDRESS.c_str());
hostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
*/


// Get host my name (or domain name)
hostEntry = gethostbyname("localhost");

// Create the socket
SOCKET theSocket;
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

//int value = 0;
//setsockopt(theSocket, SOL_SOCKET, SO_SNDBUF, (const char*)&value, sizeof(int));

// Fill a SOCKADDR_IN struct with address information
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

// Set port to use
serverInfo.sin_port = htons(SERVER_PORT);

// Connect to the server
connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

// Return socket for closing later
return theSocket;
}






// Destroy connection to server
void destroyServerConnection(SOCKET& serverSocket)
{
// Close the socket and clean up
closesocket(serverSocket);
WSACleanup();
}


Share this post


Link to post
Share on other sites
I'm assuming the problem is with the client side program as everything works when the closesocket line is included.

Is there some sort of buffer or something? I'm only sending a tiny amount of data, only 30bytes or so.

Share this post


Link to post
Share on other sites
Are you sure that you're getting the correct packet? Have you checked to make sure you arn't just getting the -1 message letting you know the socket was closed?

The code you have should send the message fine. Unless Brocketino and I are missing something, the problem is with your server's code.

If you post the server's code maybe we could offer a little more assistance.

Share this post


Link to post
Share on other sites
This code is the second thread that listens for new connections:

DWORD cThread::ThreadProc()
{

// Status message
pMutex->MutexOn();
cout<<"Listen thread active..."<<endl<<endl<<"STARTING"<<endl;
pMutex->MutexOff();

// Loop forever listening for clients
while(1) {

// Wait for a client
SOCKET theClient;
theClient = accept(listenSocket, NULL, NULL);

// Turn on mutext to protect
pMutex->MutexOn();

// Create new record of connection
playerCon p;
p.theSocket = theClient;

// Add player to vertex
pPlayers->push_back(p);

// Status report and turn off mutex
cout<<"Accepted client #"<<pPlayers->size()<<endl;
pMutex->MutexOff();
}

return 0;
}



This code processes received data

// Loop to process game data
while(1) {

// Start mutext to protect from data collisions
mutex.MutexOn();

vector<string> tokens;
vector<playerCon>::iterator p;

// Loop through the connected players
for (p = connectedPlayers.begin(); p != connectedPlayers.end(); p++)
{
// Clear message
theMessage = "";

// Receive data from the socket
while(recv(p->theSocket, &ch, 1, 0))
theMessage += ch;

// If something arrived
if (theMessage.length() > 0)
{
// Clear tokens vector
tokens.clear();

// Split data into tokens
Tokenize(theMessage, tokens, "|");

// Status message
cout<<"RCV MSG. TYPE: "<<tokens.at(0).c_str()<<endl;

// Switch to process the data
if (tokens.at(0) == "LOGIN")
{
// Build query to check username and password are correct
strSQL = string("SELECT * FROM tblUsers WHERE username = '") + tokens.at(1) + string("'");

// Query DB
mysql_query(conn, strSQL.c_str());
res_set = mysql_store_result(conn);
unsigned int numrows = mysql_num_rows(res_set);

// If username password is invalid
if (numrows == 0)
{
// Send login failed msg
cout<<"ILLEGAL LOGIN ATTEMPTED"<<endl;
sendLine(p->theSocket, "LOGIN|false");
} else {

// Login ok msg to client
cout<<"Valid login attempted"<<endl;
sendLine(p->theSocket, "LOGIN|true");
}
}
}
}

// Close mutex
mutex.MutexOff();

}


Share this post


Link to post
Share on other sites
Thanks for everyones time and help, I've found the problem, the server needs to break out the receive loop once it discovers a \n character. All works fine now!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!