LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   recv command in sock in c (https://www.linuxquestions.org/questions/programming-9/recv-command-in-sock-in-c-527709/)

mohtasham1983 02-12-2007 12:10 AM

recv command in sock in c
 
i have a http client and server which must interact with each other using GET command. My client is working fine. But I am having problem with the server one.

Code:

#include <sys/socket.h>
#include <sys/types.h>
#include <iostream>
#include <sys/time.h>       
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
using namespace std;
#include <arpa/inet.h>
int main()
{
        register int bytes;
        int    sockfd,re,se,listen_connection,accept_connection,fromlen;
        struct sockaddr_in myaddr,from;
        int bufsize=1024;        /* a 1K buffer */
        char str[INET_ADDRSTRLEN];
        char* buffer=(char*)malloc(bufsize);
        FILE *fp;
        char *command="GET /index.jsp HTTP/1.0\n\n";
        sockfd=socket(AF_INET,SOCK_STREAM,0);
        bzero(&myaddr, sizeof(myaddr));
        myaddr.sin_family = AF_INET;
        myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        myaddr.sin_port = htons(8070);
        inet_ntop(AF_INET, &myaddr.sin_addr, str, sizeof(str));
        se=bind(sockfd, (struct sockaddr *) &myaddr,sizeof(myaddr));
        if (se!=0)
        {
                cout<<"Server cannot bind a port"<< endl;
                exit(0);
        }
        listen_connection=listen(sockfd,5);
        if (listen_connection!=0)
        {
                cout<<"Connection cannot be listened"<< endl;
                exit(0);
        }
//        addrlen = sizeof(struct sockaddr_in);
        for ( ; ; )
        {
                accept_connection=accept(sockfd,NULL,NULL);
                if (accept_connection<0)
                {
                        cout<<"Connection was refused"<< accept_connection <<endl;
                        exit(0);
                }
                cout << "value of accept command ="<<accept_connection<< endl;
                re=recv(accept_connection,command,strlen(command),0);
                cout <<"The value of receive command = "<< re<<endl;
                cout <<command<<endl;
                if (strcmp(command,"GET /index.jsp HTTP/1.0\n\n")==0)
                {
                        cout << "You have chosen the right command"<< endl;
                        if ((fp=fopen("/home/mohi/temp/index.html","r")) == NULL)
                            {
                                    cout << "Cannot open the file"<< endl;
                                exit(1);
                            }
                            while ((bytes = send(accept_connection, buffer, bufsize,0)) > 0)
                            {
                                  write(1,buffer,bytes);
                                fputs(buffer, fp);
                            }
                            cout << bytes;
                }
        }
            close(accept_connection);
        exit(0);
}

I can receive command variable from client one but value of re is -1.

Any idea what s wrong with
Code:

re=recv(accept_connection,command,strlen(command),0);
?

bigearsbilly 02-12-2007 03:13 AM

i put a perror in there and got:
Code:

value of accept command =4
Hmm!: Software caused connection abort
The value of receive command = -1
GET /index.jsp HTTP/1.0

Code:

                cout << "value of accept command ="<<accept_connection<< endl;
                re=recv(accept_connection,command,strlen(command),0);
                perror("Hmm!");


bigearsbilly 02-12-2007 03:26 AM

easy,
Code:

        char *command="GET /index.jsp HTTP/1.0\n\n";
is NOT a buffer. It's a pointer to somewhere.
make it a proper buffer
Code:

        #define SZ 512
        char command[SZ];
        strncat(command,"GET /index.jsp HTTP/1.0\r\n\r\n", SZ);

result:

Code:

value of accept command =4
Hmm!: No error
The value of receive command = 15
this is a test
HTTP/1.0

FYI this is my client:
Code:

$ nc -vv  localhost 8070
DNS fwd/rev mismatch: BTG142169.iuser.iroot.adidom.com != localhost
BTG142169.iuser.iroot.adidom.com [127.0.0.1] 8070 (?) open : Operation now in progress
this is a test


mohtasham1983 02-12-2007 06:08 PM

thanks for help.
I am having another problem now. Now recv function returns 1 which means it can receive message from client, but it receives only one character of the client message. I added MSG_WAITALL as the last parameter in recv function. Then I used strcpy to compy the message from the client, but it returned only the first letter. Then I tried to define a single counter and used a loop to receive the message from client and used strcat to store the whole message in a char variable. This worked for me but it returned some extra characters; however, I don't want to define a fixed variable for this loop, since different client may send messages in different lenghts.

Here is the code:
Code:

cout << "value of accept command ="<<accept_connection<< endl;
while (j<26)
{                       
        re=recv(accept_connection,command,strlen(command),0);
        j++;
        strcat(command1,command);
        cout <<re<<endl;
}
cout <<"The value of receive command = "<<command1<<endl;


bigearsbilly 02-13-2007 04:41 AM

funny, cos as i posted I was getting everything.
maybe it's a client problem?

did you try it using my netcat example as the client.

you should learn to use netcat, it's indispensable for this work.
it can serve as an adhoc client or server, great for testing.

mohtasham1983 02-14-2007 03:09 AM

I forgot about that part already, because it is less important from my new problem.

Now I suppose that client has sent the correct command, so my server is ready to send content of a text file to my client.

Code:

cout << line1 << endl;
cout << line1.c_str()<< endl;
bytes = send(accept_connection,line1.c_str(), sizeof(line1.c_str()),0);
cout <<line1.c_str();

As you see, first two lines output the same thing, but after send the value becomes different and client can receive a small part of string. For example if line1 is "Hello World", my client receives only Hell part of it.

bigearsbilly 02-14-2007 03:40 AM

what exactly are you doing?
why http?

do you know how http works? the construction of the header etc.?


try this:
Code:

printf  "HTTP/1.1\nContent-Length:6\nContent Type: text/html\n\nhELLO\n" | nc -lp 50125
and then point your browser at http://localhost:50125/


All times are GMT -5. The time now is 11:31 PM.