?
Solved

C & Socket Communication

Posted on 2003-03-17
14
Medium Priority
?
232 Views
Last Modified: 2010-04-15
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);
}

0
Comment
Question by:adam_kan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
14 Comments
 
LVL 1

Expert Comment

by:Dikran
ID: 8151745
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.

0
 

Author Comment

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

regards
adam
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8157153
=>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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
Dikran earned 80 total points
ID: 8157747
I know about the WinPcap library. The alpha version of it you can find on the following site:
 http://wincap.polito.it/ for information and updates.

The TcpDump You can find on the site:
http://windump.polito.it/
Goog Luck! ;)

0
 

Author Comment

by:adam_kan
ID: 8159007
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
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8164181
Are you trying to find out the total bytes exchanged at transport layer or session layer or application layer!?

-Narendra
0
 

Author Comment

by:adam_kan
ID: 8165280
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
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8172318
How can you do this at the application layer then!?

-Narendra
0
 

Author Comment

by:adam_kan
ID: 8172625
Hi Narendra

Sorry, i havent understood your comment ?

regards
adam
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8178893
>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
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10144447
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
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10144463
[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
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10161695
>(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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question