-
Sockets program
I have to send data over the internet and I have a working windows sockets program that does it but after like sending a command about 50 times or so, the link goes bad and I cant send any more data. I think a buffer of some sort is filling up but I dont know how to empty it.
Thank you.
-
please post what you think is the offending code.
-
Is the other end reading the data off OK?
As Draco said, posting code is always useful when asking questions about it, preferably along with the error messages you're receiving.
-
Here is the server side of my sockets program.
Code:
// winsock for our network communication
#include <winsock2.h>
// stdio for I/O
#include <stdio.h>
#include <iostream.h>
#include <time.h>
// -----------------------------------------------------------------------------------
// startupServerForListening() - will return us a socket that is bound to the
// port we specify, and is listening for connections if all operations succeeded,
// and a -1 if startup failed.
int startupServerForListening(unsigned short port);
// -----------------------------------------------------------------------------------
// shutdownServer() - will take the socket we specify and shutdown the
// network utilities we started with startupServerForListening()
// Note: In order to function properly, the socket passed in MUST be the
// socket created by the startupServerForListening() function
void shutdownServer(int socket);
void main()
{
// the socket my server will use for listening
int serverSocket;
// startup our server utilities with my handy function
serverSocket = startupServerForListening(7654);
// check for errors
if (serverSocket == -1)
{
printf("Network Startup Failed!\nProgram Terminating\n");
return;
}
// accept a client
int clientSocket;
clientSocket = accept(serverSocket, 0, 0);
// check for errors
if (clientSocket == SOCKET_ERROR)
{
printf("Accept Failed!\n");
}
int nBytes;
// message size so we will know how much data we will send and receive
#define MESSAGE_SIZE 2
char inMessage[MESSAGE_SIZE];
// receive at MAX sizeof(inMessage) bytes into inMessage from the client using clientSocket
nBytes = recv(clientSocket, inMessage, sizeof(inMessage), 0);
// check for errors
if (nBytes == SOCKET_ERROR)
{
printf("Recv Failed!\n");
}
else
{
cout<<"Command received"<<endl;
cout<<"Command is: "<<inMessage<<endl;
}
//and shutting down our network.
if (inMessage[MESSAGE_SIZE] = 0)
{
// close our client socket
closesocket(clientSocket);
// and shutdown our network
shutdownServer(serverSocket);
printf("Press any key to continue ...\n");
getchar();
}
}
// -----------------------------------------------------------------------------------
// startupServerForListening() - a function to startup winsock, and open a socket for listening
int startupServerForListening(unsigned short port) {
// the winsock data structure
WSAData wsaData;
// startup winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR) {
printf("Could Not Start Up Winsock!\n");
return -1;
}
// create socket
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
// make sure nothing bad happened
if (mySocket == SOCKET_ERROR) {
printf("Error Opening Socket!\n");
return -1;
}
// the address structure
struct sockaddr_in server;
// fill the address structure with appropriate data
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
// and now bind my socket
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind Failed!\n");
closesocket(mySocket);
return -1;
}
// mark socket for listening
if (listen(mySocket, 5) == SOCKET_ERROR) {
printf("Listen Failed!\n");
closesocket(mySocket);
return -1;
}
printf("Server Started\n");
return mySocket;
}
// -----------------------------------------------------------------------------------
// shutdownServer() - a function to shutdown a socket and clean up winsock
void shutdownServer(int socket) {
// close our socket
closesocket(socket);
// shut down winsock
WSACleanup();
printf("Server Shutdown\n");
}
-
> #include <stdio.h>
> #include <iostream.h>
Choose C or C++, not some horrible mix
Like why call printf() and cout ?
> void main()
See avatar - void main bad, int main good
> nBytes = recv(clientSocket, inMessage, sizeof(inMessage), 0);
This doesn't append a \0 for you to make it a printable string
Code:
nBytes = recv(clientSocket, inMessage, sizeof(inMessage)-1, 0);
// check for errors
if (nBytes == SOCKET_ERROR)
{
printf("Recv Failed!\n");
}
else
{
inMessage[nBytes] = '\0'; // make it a string
cout<<"Command received"<<endl;
cout<<"Command is: "<<inMessage<<endl;
}
> if (inMessage[MESSAGE_SIZE] = 0)
1. this is an assignment, you meant comparison, which is ==
2. it is also an out of bounds access - the indices of an array run from 0 to N-1
-
>>This doesn't append a \0 for you to make it a printable string
.. and to highlight to the OP, one call to recv() doesn't guarantee you'll get one "string" of data, you might only get a partial string. Or, if the other end has sent two strings in succession, one call to recv() might get you:
- part of the first string only
- all of the first string
- all of the first string, plus part of the second
- all of the first string and all of the second
- an error
Your app should be able to cater for all of the above, calling recv() multiple times where appropriate, and manipulating the data buffer correctly.
(maybe your code does this, I haven't read it through)