Sockets program

This is a discussion on Sockets program within the Networking/Device Communication forums, part of the General Programming Boards category; I have to send data over the internet and I have a working windows sockets program that does it but ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    13

    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.

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    please post what you think is the offending code.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    13
    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");
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > #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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21