Hi all,
I'm learning socket programming in Unix. My jobs is sending some frames over TCP/IP Client-Server model, ex Hello, Ack, Bye packets. Each frames have a ID field, which helps we know which packet the server/client received. For example, 1 for Hello, 2 for Ack and 3 for Bye. I use struct in C to create frames, one struct per frame, so I have 3 structs: Hello, Ack and Byte for both Client and Server. But I got a problem. How the receiver know which packet it receive ?

Have the server check the packet ID field and it will know which packet its getting. Hopefully the ID field is the first item in the packet to make it easier for the programs to reconstruct the appropriate structure.

For example the client would read the first byte from the tcp/ip stream, check whether its 1, 2, 3 or something else. Then read the rest of the data in the stream into the appropriate structure. BTW it might be easier if you made a union out of those three structures so that you only have to pass around only one object instead of three.

I send whole struct over TCP/IP. Here's my code for client when I define and send hello packet:

struct {
    char Preamble[11];
    int32_t msgLen;
    int32_t msgId; // ID of packet
    char senderId[50]; // email of sender
} helloMsg;

send(sockfd, &helloMsg, sizeof(helloMsg), 0);

and Client also difine a Hello struct and receive the frame.

struct {
    char Preamble[11];
    int32_t msgLen;
    int32_t msgId; // ID of packet
    char senderId[50]; // email of sender
} helloMsg;

recv(newsockfd, &helloMsg, sizeof(helloMsg), 0);

How can I check first byte of TCP stream to identify the kind of frames that server receive ?

Move the ID field to the first field in the packet.

struct {
    int32_t msgId; // ID of packet
    int32_t msgLen;
    char Preamble[11];
    char senderId[50]; // email of sender
} helloMsg;

>>How can I check first byte of TCP stream to identify the kind of frames that server receive ?
Normal socket receive stuff

char buffer[256];		// On the stack

nret = recv(theSocket,

	    buffer,

	    256,		// Complete size of buffer

	    0);

int32_t x = *(int32_t *)buffer;
switch(x)
{
   case 1: /* do soething */ break;
   case 2: /* do soething */ break;
   case 3: /* do soething */ break;
   default: /* errir */ break;
}

Move the ID field to the first field in the packet.

struct {
    int32_t msgId; // ID of packet
    int32_t msgLen;
    char Preamble[11];
    char senderId[50]; // email of sender
} helloMsg;

So how to receive, check first byte of TCP stream and assign it to proper struct in Client ? If I use the codes above for Client, I already know that the packet just come is Hello packet. That's what I'm wondering ?

I assumed it was the client that did the receiving. But it doesn't really matter because the techique is the same whether its received by client or server.

What's happen if we have multiple clients and one server ? Server doesn't know packet just come is hello, bye or Ack. As you wrote, server must check first byte. But I dont know how to do it in C code. Could you give me some sample codes ?
Thanks,

Didn't you bother to read the post I wrote? :@ Why am I wasting my time and effort if you don't want to read it.

Didn't you bother to read the post I wrote? :@ Why am I wasting my time and effort if you don't want to read it.

Sorry man, I did not read it carefully. Dont be mad !
Thanks for reply my question,

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.