Thread: tcp server problems

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    tcp server problems

    This is my code, I want to be able to keep receiving data while I can send data, so I used fork(). But for some reason it is not working, it will only recv once. I am new to network programming, please help.

    Code:
                    char buf[10000];
                    pid_t pid;
    		pid = fork();
    		if(pid == 0)
    		{
    			while(1)
    			{
    				sleep(1);
    				memset(buf, '\0', sizeof(buf));
    				recv(clntSock, buf, sizeof(buf), 0);
    				printf("%s", buf);
    			}
    		}
    		else
    		{
    			while(1)
    			{
    				sleep(1);
    				memset(buf, '\0', sizeof(buf));
    				fgets(buf, sizeof(buf), stdin);
    				buf[strlen(buf) - 1] = '\0';
    				send(clntSock, buf, strlen(buf), 0);
    			}
    		}
    Foxyy

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How do you know what is working and what isn't, when you ignore the status results for
    - fgets
    - send
    - recv

    Plus, if you want us to actually test the code say, you need to post a complete program.
    Not the random snippet where you think the problem is.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows TCP Server Can't Handle >100 Clients
    By Xterria in forum Networking/Device Communication
    Replies: 13
    Last Post: 11-02-2009, 07:21 PM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  3. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  4. TCP TIME_WAIT state
    By Engineer in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-27-2001, 08:50 AM
  5. Socket Problems
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 08-18-2001, 06:59 AM