I am begginer in C so please be easy on me.....:-)

Anyways....my query is I have a server and a Client program written in C(attached below) for test in UNIX/LINUX in which the client sends a request to the server and the server then sends back the request to the client.

What I am looking for is bascially Punch in a request to the server for

GET/test.html /HTTP1.0

and in return get the contents of the test.html file which would be something along the lines on motor status(on or off) and the value of 4 temperature sensors......

So any ideas then please let me know

Thanks

Recommended Answers

All 12 Replies

i haven't tested it, but it looks right.

so, what's your question? didnt the site you downloaded the programs from give you some instructions?

i haven't tested it, but it looks right.

so, what's your question? didnt the site you downloaded the programs from give you some instructions?

Basically I send a request by entering the following to the server

GET/DATA.HTML /HTTP1.0

and get back the contents of DATA.HTML in the client side.....

have you even tried running it?

you run the server on one computer. run the client on another.

specify the port that the server will use by passing the port number as an argument to the executable. see code for example

if (argc != 2)     /* Test for correct number of arguments */
    {
        fprintf(stderr, "Usage:  %s <Server Port>\n", argv[0]);
        exit(1);
    }

    echoServPort = atoi(argv[1]);  /* First arg:  local port */

don't use a reserved port number. pick something greater than 1000 and you should be safe

for the client, send the Server IP, Command, and Port # as arguments to the executable. see the code for example.

if ((argc < 3) || (argc > 4))    /* Test for correct number of arguments */
    {
       fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",
               argv[0]);
       exit(1);
    }

Oh yes I've tested the scripts on Ubuntu..........

Basically opened two terminal windows.......

One acting as a Server and one as a Client........

After I setup the Server like

./S 1500

and then the server basically waits for the client to send the request.

then on the other window....

./C 127.0.0.1 Hello 1500

On the server side it says handling 127.0.0.1

and on the client side it says MOTOR=0,TEMPS are 3A,42,FA,99

whereas I want to use the same concept to get back the values on the HTML files something like this

<html>
<body>
Hello
</body>
</html>

sounds like the program is working correctly.

review the file that you think is being sent, versus the file that is actually being sent.

i believe there you will find the source of your confusion.

Bump! Anybody with any suggestions please!:(

oh, i just re-read your last post.

the problem is not with TCP/IP.

the problem is that you need to take the values that you get from the server, and rewrite them to a file with HTML tags.

maaann....

if you can write a server and a client, what you're asking is CHILDS PLAY... ;)

i mean, really...

"fopen"
a bunch of "fprintf"
"fclose"

and you're done.

oh, i just re-read your last post.

the problem is not with TCP/IP.

the problem is that you need to take the values that you get from the server, and rewrite them to a file with HTML tags.

maaann....

if you can write a server and a client, what you're asking is CHILDS PLAY... ;)

i mean, really...

"fopen"
a bunch of "fprintf"
"fclose"

and you're done.

Cheers man for the advice! well I didn't write the server and client....Got it from a reference book as I am trying to understand Unix networking programming.....

the best way to understand is to spend a lot of time making mistakes.

just start hacking away at a solution. understanding will come later. meanwhile, figure out how to use those three commands i mentioned (any online reference) and that's about all you need.

Right time for an update!:)

I have been digging about with code and have been able to do most of the stuff but few couple of issues.....

This is the code I implemented into my S.c

if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) > 0)
    {
        puts(echoBuffer);
        if ( strncmp("GET /DATA.HTML /HTTP1.0",echoBuffer,18)==0){
            if( !(inputFilePtr = fopen("DATA.HTML", "r"))){
                puts("ERROR: file open error");close(clntSocket);exit(1);
            }
            i=0;
            do{
                echoBuffer[i++]=fgetc(inputFilePtr);
            }while (!feof(inputFilePtr));
            echoBuffer[i]='\0'; // i holds length
            recvMsgSize=i;
            printf("\nGot %d chars from file\n",i);

        }
        else puts("bad input received\n");
    }   
    else{
         close(clntSocket);
         DieWithError("recv() failed");
     }

So once I have the Server running.......

and if i type in this from my client side

./C 127.0.0.1 "GET /DATA.HTML /HTTP1.0" 1500

I am able to get the contents of the file back from the server.

Now, I have four DATA.HTML files with this as its content:
DATA2.HTML

temp=1

and then on the next DATA3.HTML with this as its content :

temp=2

...etc......

So therefore I am not able to figure it out as how I can further inplement the code so that if I put this as my request

./C 127.0.0.1 "GET /DATA.HTML?temp=1 /HTTP1.0" 1500

I should then be able to get back the contents of DATA2.HTML
file which has temp=2 as its content......?????

Please advice

a few things here, that i think are confusing:

the string you are sending, "GET /DATA.HTML /HTTP1.0", is kind of meaningless. by that i mean there is nothing in this string that affects the behavior of the server. the whole concept of "GET" is that it's a command that tells the server what to do. other commands would be "PUT" or "DEL(ETE)" or "CD" or "DIR" or "QUIT". likewise your "DATA.HTML" means this should also be parsed so that you choose one file from one or more possible files in the working directory.

another thing thats bothering me somewhat is the ubiquitous use of the variable "echoBuffer" for both sending and receiving. i suggest that you change your Server code for the "HandleTCPclient" function to use the concept of send and receive, and forget about this 'echo':

void HandleTCPClient(int clntSocket)
{
    char recvBuffer[RCVBUFSIZE];        /* Buffer for recv string */
    char sendBuffer[SNDBUFSIZE];        /* Buffer for send string */
    int recvMsgSize;                    /* Size of receive message */
    int sendMsgSize;                    /* Size of send message */

    /* Receive message from client */
    if ((recvMsgSize = recv(clntSocket, recvBuffer, RCVBUFSIZE, 0)) < 0)
        DieWithError("recv() failed");
        
    /* incoming request from client is in the recvBuffer */
    /* message can be parsed as necessary */
    printf("  received client message: %s\n",recvBuffer);
    
    /* put outgoing message from server in the sendBuffer */   
    sprintf(sendBuffer,"MOTOR=0,TEMPS are 3A,42,FA,99");   //reply! 
    sendMsgSize = strlen(sendBuffer);

    /* Send message until transmission completed*/
    while (recvMsgSize > 0)      /* zero indicates end of transmission */
    {
        if (send(clntSocket, sendBuffer, sendMsgSize, 0) != sendMsgSize)
            DieWithError("send() failed");
  
        /* See if there is more data to receive */
        if ((recvMsgSize = recv(clntSocket, recvBuffer, RCVBUFSIZE, 0)) < 0)
            DieWithError("recv() failed");
    }
    close(clntSocket);    /* Close client socket */
}

look at this code now, and understand whats going on here: the server gets a request from the client in the "recv" buffer. at which point the server would normally parse the request and decide what to send via the "send" buffer.

so far, the only thing you are doing is sending a line of text about the MOTOR TEMPS.... it's doing exactly what you are telling it to do.

but since you want the server to read a file and send the contents of the file back to the client, you'll have to change the HandleTCPClient function in the server to read contents of the file, and send those contents back to the client instead of just the hardcoded line.

furthermore, if you're reading out an entire file, you would not have the client print all of the file's information to the terminal... you would have the client take the information received from the server and write it to another file on the client's end.

now, implement this into the new "HandleTCPClient" function that i provided, above. you can easily read and write one line at a time using fgets() and fprintf(). the preferred method would be to read and write large blocks of data using fread() and fwrite().

the best way to test this is to open two separate terminal windows, each one in a different directory.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.