Thread: recv data with sockets

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    4

    recv data with sockets

    Hi

    Im not sure if this post should be in this board or the network programming board, anyway here is my question.

    I am coding a server program with sockets on linux and when I got to the recv part I noticed a unexpected behaviour.

    Here is the code for the recv data part:
    Code:
    //read some data
    char recvBuf;
    do
    {
    	error = recv(new_fd, &recvBuf, 1, 0);
    	switch (error)
    	{
    	case -1:
    		cout << "[ERROR] recieving data" << endl;
    		recvBuf = '.';
    		break;
    	case 0:
    		cout << "Connection to client was lost" << endl;
    		recvBuf = '.';
    		break;
    	default:
    		cout << recvBuf;
    	}
    } while (recvBuf != '.');
    Now when I run this and connect to the server with telnet, the server waits until I press ENTER until it writes recvBuf to the screen. But if I press '.' the program ends without me pressing ENTER.

    How can the text I type in telnet be saved and then printed, shouldn't the recvBuf be overwritten for every new data the server reads ?

    Thanks for helping me understand this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The output is probably buffered until it sees a newline.
    Try cout.flush()
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    4

    Thumbs up

    Thanks that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Sockets: send/receive continuous stream of data?
    By diddy02 in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-09-2008, 12:52 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM