Looping problems socket programming in C


 
Thread Tools Search this Thread
Top Forums Programming Looping problems socket programming in C
# 1  
Old 08-07-2011
Looping problems socket programming in C

Sorry if I posted 2 separate questions.
I'm currently doing socket programming on my current task. As you can see below in the client side. I've tried to do a loop so I will be able to get prompt for input over and over again.
This is the code.

Code:
do{ 
 
    
   printf("Please your name > "); 
   scanf("%s",name); 
    
   write(clientFd,name,strlen(name)); 
 
    
   }while(strcmp("exit",name)!=0);

It worked for the first time, but the second time it asked me for input but it did not print anything on the server side. After entering the second input it exited from the terminal.

I've check up the loop and realize it was the write problem. Any way to solve this?
# 2  
Old 08-07-2011
From the code snipit you posted it's difficult to say what is happening. My only guess what you've provided is that the space allocated for name isn't large enough to handle the string that you entered.

It would help if you could post the whole routine which would include the declaration of name, as well as the two strings that you entered from the tty.
# 3  
Old 08-07-2011
Add:
Code:
ret = write(clientFd,name,strlen(name));
printf("Write ret: %d\n", ret);

Also with this loop you will write 'exit' to the fd. Maybe use a while(1) { } and place
Code:
while (1) {
    int ret;

    /* input stuff */

    if (strcmp(name, "exit") == 0)
        break;
    ret = write(clientFd,name,strlen(name));
    if (ret == -1) perror("write");
    printf("Write returned: %d\n", ret);
}

It also may help to issue a read() to ensure the socket is available... My guess is that you're getting a SIGPIPE by writing to a broken socket. you can set SO_NOSIGPIPE with setsockopt
# 4  
Old 08-07-2011
Hahaha its still not working..

The client side
Code:
while(1){

   int ret;
   char country[300];


   printf("Please enter country name > ");

   scanf("%s",country);

   if (strcmp(country, "exit") == 0)
   break;
   else
   ret = write(clientFd,country,strlen(country));
   //f (ret == -1) perror("write");
   //printf("Write returned: %d\n", ret);    

   

   //write(clientFd,country,strlen(country));

   }

Server Side

Code:
readData();

            bzero(country,300);

            c=read(clientFd,country,300);

	    printf("You have entered: %s\n",country);

            printf ("%s Capital       : %s\n", country,getCapital(country));

            printf ("%s Currency Code : %s\n", country,getCurrencyCode(country));

            char* capital = getCapital (country);

	    char* currency = getCurrencyCode(country);



            close (clientFd); /* Close the socket */

            exit (/* EXIT_SUCCESS */ 0); /* Terminate */

So like what you all said it might be the reading part that is the the problem.
From what I think is just the read problem?
Reason is I comment the write link it just run the loop normally.
# 5  
Old 08-07-2011
The server reads once and exits... So the client can't loop if the server doesn't...
# 6  
Old 08-07-2011
haha sry i did post the whole thing i did try to loop thou.

Code:
   

char country[300];

char capital[300];

char currency[300];

 while (1) /* Loop forever */

      {

        /* Accept a client connection */

        clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);



        if (fork () == 0) /* Create child to send recipe */

          {

            readData();

            bzero(country,300);

            c=read(clientFd,country,300);

	    printf("You have entered: %s\n",country);

            printf ("%s Capital       : %s\n", country,getCapital(country));

            printf ("%s Currency Code : %s\n", country,getCurrencyCode(country));

            char* capital = getCapital (country);

	    char* currency = getCurrencyCode(country);



            close (clientFd); /* Close the socket */

            exit (/* EXIT_SUCCESS */ 0); /* Terminate */

          }

        else

          close (clientFd); /* Close the client descriptor */

      }

   }

# 7  
Old 08-07-2011
it still looks to me that your loop only is for accepting new connections and still only reads a connection ONCE then exits...

I generally don't have a need for a fork() and use select() in the main loop. You can use select() in your child too. It'll sleep until something is available to read.

Code:
man 2 select

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping connect call for a non blocking socket

will there be any unexpected results on looping connect call for a non blocking socket to determine the connection based on error code. I am getting connection unsuccessful intermittently and so wondering whether is the timeout 500 millisec not sufficient or looping connect cause any unexpected. ... (7 Replies)
Discussion started by: satish@123
7 Replies

2. Programming

socket programming

how to include socket.h in visual studio 2005.. (2 Replies)
Discussion started by: asd123
2 Replies

3. Programming

socket accept() keeps looping

I'm using C/ C++ with gcc on Linux. I've a server socket where accept() is called on the socket inside a while() loop. The problem I am facing is that the first call to accept is blocking (i.e., the program waits for the first connection) but as soon as I fork afterwards (so that the child process... (2 Replies)
Discussion started by: jaywalker
2 Replies

4. Programming

Please help! accept function problems in Socket programming

Hi, I have a client-server socket program. It has been working fine for over a year, but recently it started to show strange behavior.:confused: After the server program runs for a while, it will show in the top command saying it is using lots of CPU, MEM. I assume it means the server code is... (1 Reply)
Discussion started by: natxie
1 Replies

5. Programming

help regarding socket programming

i m using sockets for setting up a connection between a server and a client. When the clients gets connected to the server, its ip is conveyed to the server through one of the predefined structures in c library... i save this ip address in an array....1st client's ip address goes to the zeroth... (1 Reply)
Discussion started by: abmxla007
1 Replies

6. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

7. Programming

Socket programming

Hello!:) I'm trying to do some socket programming based on the following situation: I have a directory service named Casino that will hold all the information regarding the gamers that will try to connect to it in order to play a game(for example (Blackjack).Once they make the login they are... (4 Replies)
Discussion started by: maracumbigo
4 Replies

8. Programming

Socket Programming

Hi , I'm facing the following problem in socket programming. My structure is struct { int i; char *str; }samp; I'm creating the pointer to this structure and assigning the value to the structure members and send via the socket to the another process. The receiver process when... (7 Replies)
Discussion started by: arunviswanath
7 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

Socket programming

Suppose i am writing a C program which is going to use Socket calls. I want to use a Unix port for my Socket. How can i determine a port which is not already in use? (1 Reply)
Discussion started by: Nadeem Mistry
1 Replies
Login or Register to Ask a Question