Socket programming doubt


 
Thread Tools Search this Thread
Top Forums Programming Socket programming doubt
# 1  
Old 07-26-2010
Socket programming doubt

I am aware that TCP sockets are stream based and a single write may not send all the data. Is this the case with recv as well ?

I am in process of deciding a protocol to handle communication. I wanted some tips as to handle transactions. The data sent / received would be fixed length. Hence i am toying with the idea of discarding messages on receive do not conform to a length. The data sent is less then 100 bytes , so i am not sure how many times will i encounter a scenario where the send fails to fire it in one go or receive fails to read in one go.

Any other approaches to send /receive data ?

Since multiple receives are possible (via select call) I am not sure how to handle incomplete data from two more different clients. ( perhaps via some sort of buffer created per connection)

More importantly , if i have about 2-3 sockets from which i am receiving data , is the conventional approach to block till the complete packet(i.e. message) from one is received ?

PS : These are unix domain sockets for IPC , so no flow over n/w

Cheers!
# 2  
Old 07-26-2010
How much data of the request gets sent is under the control of the kernel. Usually for anything under ~1000 bytes you can send( ) and recv( ) everything in one go.

One way -
set socket to NON-BLOCKING, sample the input with MSG_PEEK.
Code:
int sample(int s, char *msg)
{

       int len=100;
       int nbytes=0;
       
       memset(msg, 0x0, 100);
       nbytes= recv(s, msg, len, MSG_PEEK);
       if(nbytes<0 && errno== EWOULDBLOCK)
          return EWOULDBLOCK * -1;
       else
          return nbytes;
}

This does not have error handling - note. msg now has what recv probably thinks is a message. Go back and recv() nbytes.

Good practice in something like this is to have your own protocol "header"
like
Code:
00098DT001234

00098 is length of the packet including your header
DT might mean it is data. HS might mean a heartbeat. Whatever.
The last number is a sequence that goes from 000000 -> 999999, and rolls over.
This way you can keep track of separate packets. So all you would do is sample
13 bytes, and then you know how many bytes to do a full recv() on. And you would know what to expect in the data part of the packet if any.
# 3  
Old 07-27-2010
Thanks jim!

I am unable to figure out why is this failing with bad file descriptor.

Code:
        while (1)
        {
                stat =0;
                execReturn=0;
                s2 = accept(s, (struct sockaddr *)&remote, &t);

                if(s2== -1)
                {
                         perror("accept");
                         outfile<<strerror(errno)<<endl;
                         exit(1);
                }

                pidc = fork();
                if(pidc == 0)
                {
                        close(s);
                        int retVal;
                        string xs;
                        retVal = RecieveAllData(s2,outfile,xs);
                        if(retVal !=SUCCESS)
                        {
                                close(s2);
                        }

                        ipMsg parsedValue;
                        TokenizeString(xs,parsedValue);
                        outfile<<"User----"<<parsedValue.user<<endl;
                        outfile<<"Pwd ----"<<parsedValue.pwd<<endl;
                        outfile<<"origin ----"<<parsedValue.origin<<endl;
                        outfile<<"cmd ----"<<parsedValue.command<<endl;


                        pidf = fork();
                        if (pidf == 0)
                        {
                                close(s2);
                                close(s);
                                sleep(5);
                                execReturn =execl("/root/test.sh","test.sh",NULL);
                                outfile<<"Errror"<<endl;
                                _exit(-1);
                        }
                        else if (pidf > 0)
                        {
                                int id = waitpid(pidf,&stat,0);
                                int sat = WEXITSTATUS(stat);
                                outfile<<"status is "<<sat<<"pid is "<<pidf<<endl;
                                char stg[32];
                                sprintf(stg,"%d",sat);
                                send(s2,stg,strlen(stg), 0);
                                close(s2);
                        }
                }
                sleep(5); /* wait 30 seconds */
        }


Last edited by pludi; 07-27-2010 at 08:21 AM..
# 4  
Old 07-27-2010
Do you know where it's failing?
# 5  
Old 07-27-2010
Code:
int main(void)
{

        /* Our process ID and Session ID */
        pid_t pid, sid;

        /* Fork off the parent process */
        pid = fork();
        if (pid < 0)
        {
                exit(EXIT_FAILURE);
        }

        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);

        /* Open any logs here */
        ofstream outfile("/root/log.txt");


        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0)
        {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }

        /* Change the current working directory */
        if ((chdir("/")) < 0)
        {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }

        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        /* Daemon-specific initialization goes here */

        int s, s2,  len;
        socklen_t t;
        struct sockaddr_un local, remote;

        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
        {
                perror("socket");
                exit(1);
        }

        local.sun_family = AF_UNIX;
        strcpy(local.sun_path, SOCK_PATH);

        unlink(local.sun_path);
        len = strlen(local.sun_path) + sizeof(local.sun_family);

        if (bind(s, (struct sockaddr *)&local, len) == -1)
        {
                perror("bind");
                exit(1);
        }


        if (listen(s, 5) == -1)
        {
                perror("listen");
                exit(1);
        }


        pid_t  pidf,pidc;
        int stat=0;
        int execReturn =0;
        int n;
        char str[100];
        vector<int> pidV;

        while (1)
        {
                stat =0;
                execReturn=0;
                s2 = accept(s, (struct sockaddr *)&remote, &t);

                if(s2== -1)
                {
                         perror("accept");
                         outfile<<strerror(errno)<<endl;
                         exit(1);
                }

                pidc = fork();
                if(pidc!=0)
                        pidV.push_back(pidc);

                if(pidc == 0)
                {
                        close(s);
                        n = recv(s2, str, 100, 0);
                        str[n]='\0';
                        outfile<<"Message is "<<str;
                        pidf = fork();
                        if (pidf == 0)
                        {
                                close(s2);
                                execReturn =execl("/root/cpp/test.sh","test.sh",NULL);
                                outfile<<"Errror"<<endl;
                                _exit(-1);
                        }
                        else if (pidf > 0)
                        {
                                int id = waitpid(pidf,&stat,0);
                                int sat = WEXITSTATUS(stat);
                                outfile<<"status is "<<sat<<"pid is "<<pidf<<endl;
                                char stg[32];
                                sprintf(stg,"%d",sat);
                                send(s2,stg,strlen(stg), 0);
                                close(s2);
                                _exit(0);
                        }
                }
                else if(pidc > 0)
                {
                        close(s2);
                        //REAP ALL CHILDREN
                        /*
                        vector<int>::iterator beg = pidV.begin();
                        while(beg!=pidV.end())
                        {
                                int id = waitpid(*beg,&stat,WNOHANG);
                                if(id!=0)
                                {
                                        beg = pidV.erase(beg);
                                }
                                else
                                {
                                        ++beg;
                                }

                        }
                       */

                sleep(5); /* wait 30 seconds */
        }
        exit(EXIT_SUCCESS);
}

This is the complete code. perhaps exec is closing my listen socket descriptor. The failure is on accept call with bad file descriptor.... I am not touching/closing the listen socket any where... this is driving me crazy! Smilie
If i do not reap the child processes, it is fine... with zombies though..... but if they are reaped... the listen socket goes boom!
# 6  
Old 07-30-2010
Maybe it really is invalid. Print s occasionally to see if its value changes. there could be stack mangling or something overwriting its value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Socket Programming

HI Can anyone provide me with codes for file transfer server to client or vice versa? Also please explain how to compile those programs in ubuntu terminal as i am totally new to socket programming. Thanks (1 Reply)
Discussion started by: mayhemtrigger
1 Replies

2. UNIX for Advanced & Expert Users

Socket programming

my socket program is not working with larger port numbers like more than 60000 , any reason why ? (4 Replies)
Discussion started by: Gopi Krishna P
4 Replies

3. Programming

help with socket programming in c

i'm doing a simple program in socket programming on c i have server that can handle 2clients in a single machine i'm running ubuntu linux so i got it work but the probelm when clients send a message the server will echo it but i cant distinguish which client send the message client 1 or client... (7 Replies)
Discussion started by: kedah160
7 Replies

4. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

5. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

6. UNIX for Advanced & Expert Users

socket programming doubt

can we create multiple sockets in a machine with different ip addresses and all port listen to one single port ???? i hav one tool which creates virtual interface and i m using that to create multiple ip addresses in a linux machine and i use thos eip addresses to create multiple sockets and... (1 Reply)
Discussion started by: kic
1 Replies

7. 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

8. IP Networking

socket programming

my system is a stand alone system... i want to try doing socket porgramming..ihave heard that this is usually done during testing... how can i do that....? (6 Replies)
Discussion started by: damn_bkb
6 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

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies
Login or Register to Ask a Question