Link to home
Start Free TrialLog in
Avatar of adam_kan
adam_kan

asked on

C & Socket Communication

Hi I'm working on a college project developing a Network Analyser using Libpcap. The Capture engine is built in C and GUI in Java and i'm using sockets ipc for communication. I have developed a prtocol, which both sides understand. The structure of the protocol is:

Message ID:
Message Type:
Session ID:
NO oF Parameters:
Parameter Length:
Parameter:

The problem is i have a basic TCP echo server in C which just echos the word hello to the client.

I want to know how would I decode the incomming message which has the above fields and how would I encode using the same above field to send the same message back to the client.

thanks
adam


The socket code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <sys/time.h>

#define MAXPENDING 5

void DieWithError(char *charMessage);
void HandleTCPClient(int clntSocket);

int main(int argc, char *argv[])

{
        int servSock;
        int clntSock;
        struct sockaddr_in echoServAddr;
        struct sockaddr_in echoClntAddr;

        unsigned short echoServPort;
        unsigned short echoServPort;
        unsigned int clntLen;

        int i = 24;

        if (argc !=2)
        {
                fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
                exit(1);
        }

        echoServPort = atoi(argv[1]);

        if((servSock= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
                DieWithError("socket()failed");
        memset(&echoServAddr,0,sizeof(echoServAddr));
        echoServAddr.sin_family = AF_INET;
        echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        echoServAddr.sin_port = htons(echoServPort);

        if(bind(servSock, (struct sockaddr *)&echoServAddr,sizeof(echoServAddr))<0) {
                DieWithError("bind() failed");
        }

        if(listen(servSock, MAXPENDING) < 0)

         DieWithError("listen() failed");
        /*for(i = 0; i <= 4; i++)
        {*/
                clntLen =sizeof(echoClntAddr);

                if((clntSock = accept(servSock,(struct sockaddr*)&echoClntAddr,&clntLen))<0)
                        DieWithError("accept()failed");

                printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
                HandleTCPClient(clntSock);
        /*}*/
        close(servSock);
        return(0);
}

void DieWithError(char *errorMessage)
{
        perror(errorMessage);
        exit(1);
}

void HandleTCPClient(int clntSocket)
{
        int sendrc;
        char greeting[44];
         strcpy(greeting, "This is a message from the C socket server.");

        if((sendrc = send(clntSocket, greeting, strlen(greeting),0))<0)
        {
                printf("Send failed\n");
                perror(NULL);
                exit(-1);
        }
        close(clntSocket);
}

Avatar of Dikran
Dikran

Hello dear adam_kan.
Below is the little draft which you can improve dipending of your requirement.

1. Lets assume following structure:
typedef struct _MY_MESSAGE
{
int Message_ID;
int Message_Type;
int Session_ID;
int Parameter_Length;
int Parameter;
} MY_MESSAGE;

2. You have to receive the message from the client:
unsigned char buffer[1024];
recv(clntSocket,buffer,1024);

3.Then you have to make the casting of your buffer to message:
MY_MESSAGE* pMY_MESSAGE = (MY_MESSAGE*)buffer;
4.Then you can performe any checking or even the modifications of the message fields; For instance:
pMY_MESSAGE->Parameter_Length = x;
5. Then you can send it back to the client:
sendrc = send(clntSocket, buffer, 1024,0);


 Hope this will help you to solve your problems
Regards
Dikran.

Avatar of adam_kan

ASKER

dikran  thanks for your answer it has been of great help.
By the way do you know much about Libpcap & Tcpdump?

regards
adam
Avatar of Narendra Kumar S S
=>Tcpdump  prints  out the headers of packets on a network interface.
=>libpcap is a system-independent interface for user-level packet capture. libpcap provides a portable framework for low-level network monitoring.
ASKER CERTIFIED SOLUTION
Avatar of Dikran
Dikran

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys !!!

I have got the program which prints the Src & Des IP, Src & Des Ports and Transport Layer protocol. What i trying to do or need help is in capturing the total bytes exchanged between a session.

Any help would be dearly appreciated.

regards
adam
Are you trying to find out the total bytes exchanged at transport layer or session layer or application layer!?

-Narendra
Hi Narendra

I'm trying to find total bytes/Packets exchanged at the tranport layer, i.e. i'm interested in finding out the total bytes/Packets exchanged in a TCP or UDP connection.

Thanks
How can you do this at the application layer then!?

-Narendra
Hi Narendra

Sorry, i havent understood your comment ?

regards
adam
>I'm trying to find total bytes/Packets exchanged at the tranport layer
Your statement itself says that, you want to work at transport layer.
But, you are using socket APIs, which is an interface at the application layer. With socket you can get only the data sent by the application layer at the destination host. Here you will not be able to differentiate between different TCP/UDP packets. And you will get data which are stripped of its transport layer headers.
So, to get to know about the transport layer packets, you have to tap the data at the transport layer interface to network layer.
I suggest you to read some book which explains the transport layer protocol/headers and check if you can get an API for transport layer data.

-Narendra
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answwer by Dikran (acknowledging good responses from ssnkumar).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
[Posting again. If I don't spell things right, my tools break down.]

Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Dikran (acknowledging good responses from ssnkumar).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
>(acknowledging good responses from ssnkumar).

Then I can also expect some points!!?:-))
I don't want to be greedy....and the points are too less to be split.....go ahead and award points to DiKran.

-ssnkumar