Winsock problems

This is a discussion on Winsock problems within the Windows Programming forums, part of the Platform Specific Boards category; Hey guys, having a bit of a problem with Winsock in C++. I have a suspicion what the problem is ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    25

    Winsock problems

    Hey guys, having a bit of a problem with Winsock in C++. I have a suspicion what the problem is but I thought I'd get a second opinion. I'm creating a simple chat program just to practice my windows skills. I've created a simple base class called CSocket from which the two child classes, Server and Client are derived (obviously for the server program and the client program.) I am using asynchronous sockets and they seem to be working fine but for a few problems. First, a bit of code. When either the socket recieves an FD_READ message, the CSocket::Read() member function is called:

    I have used cout for debugging purposes.

    Code:
    void CSocket::Read(WPARAM wParam) {
    	char buffer[250];
    	
    	memset(buffer, 0, sizeof(buffer));
    	
    	recv(wParam, buffer, sizeof(buffer)-1, 0);
    	
    	cout << "Recieved: " << buffer << endl; 
    	
    	Write(buffer);
    
    }
    Both the server and the client classes have their own implementations of Write.

    Code:
    void Server::Write(const char* buffer) {
    	for(unsigned i = 0; i < clients.size(); i++) {
    
    		cout << "Sending to " << clients.at(i) << endl;
    
    		int j = send(clients.at(i), buffer, sizeof(buffer) - 1, 0);
    
    		if (j == INVALID_SOCKET) {
    			cout << "FAILED" << endl;
    		}
    	}
    }
    In this case, the server loops through clients (which is a standard deque indexing the client sockets) and sends them the message it just recieved.

    Below, the client writes the received message to a read-only text box called IDC_MESSAGES.

    Code:
    void Client::Write(const char* buffer) {
    	
    	SetDlgItemText(hwnd, IDC_MESSAGES, buffer);
    
    }


    And here is the Send function for the client, pretty self-explanatory.

    Code:
    void Client::Send(const char* message) {
    	cout << "SENDING: " << message << endl;
    	send(_socket, message, sizeof(message) - 1, 0);
    }
    If you need any more code, please tell me. I don't want to overdo it, that's all...


    Now, to my problem. The server and client start up fine. The server reports that it has accepted a connection from the client. The client then attempts to send some text... the server receives it and the debugging window confirms that it received the correct data, so far so good. Then, according to the above code, the server is supposed to send the data to all connected clients, and it confirms that it is sending the data to the client that just sent the text. But the message never gets back to the client. The debugging window on the client shows nothing. The message is being sent from the server but not received by the client. Anybody think they can tell me why?

    Secondly, when the server disconnects and leaves a client hanging, therefore the client receives an FD_CLOSE message, the client is supposed to display a messagebox with an error message in it then close. Yet, it does nothing. It just continues on as if nothing happened.

    Also, if I send a message from a client, then close the client, the server shuts down (I am guessing this is because it has received the message but errors occur when it tries to close the client in the middle of sending it data.)

    The only thing I can think of at the moment to explain these issues (besides a profound misunderstanding of how sockets work) is that I am running the client and server from the same IP. When the server is running and I have opened up a few clients, I run netstat -an from the console and I get this:
    Code:
    TCP    10.1.1.200:4469        0.0.0.0:0              LISTENING
    TCP    10.1.1.200:4469        10.1.1.200:1390        ESTABLISHED
    TCP    10.1.1.200:4469        10.1.1.200:1391        ESTABLISHED
    (Obviously that isn't all, but that's the relevant part.)

    So, anyone think they can help me?
    Last edited by addle_brains; 11-25-2007 at 08:00 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > int j = send(clients.at(i), buffer, sizeof(buffer) - 1, 0);
    sizeof() doesn't work on pointers (at least not in the way you want it to here).
    Use strlen() instead.

    Also, you need to be aware that just because you asked to send say 10 bytes that 10 bytes will always be sent. send() may return with any number of bytes sent (up to the limit you state), and it's up to you to call send() again with the remainder.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    Right, fixed that. Do you know why the message never gets sent to the client?

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    I tested the program running the server and clients from different computers, the problem still persists. I've followed everything properly (in terms of initialising winsock and everything) as far as I know. Any ideas as to what's going on?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Dunno without seeing the latest code.

    You could try running wireshark (was ethereal) to monitor the traffic at the network level to verify what you think is happening is really happening.
    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
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    I figured it out. When the server accepted a connection from a client, I hadn't WSAAsyncSelect() ed this new socket yet. I added this line and it works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. winsock linking problems
    By Calef13 in forum Windows Programming
    Replies: 2
    Last Post: 08-29-2007, 08:54 AM
  2. winsock problems
    By axr0284 in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2006, 08:40 AM
  3. Huge problems in Winsock recv!
    By sirSolarius in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-11-2004, 09:34 AM
  4. Winsock - Asynchrnonous Server problems
    By neandrake in forum Windows Programming
    Replies: 6
    Last Post: 06-24-2003, 06:19 PM
  5. Complete Port I/O and Heap Problems :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 11-04-2002, 11:45 PM

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