Question

I implemented a TCP client server architecture both in C++ on Ubuntu and the server should process the received data according to the type of information received. So I need a sort of header.

Do I need to do this manually by writing a string like "THIS IS THE HEADER" to the server at first?

What is the common way to do this?

Client:

if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
       COMP_ERR("ERROR connecting");

int bytes_written = write(sockfd, buffer, bytes_read);

Server:

while(1)
    {
        if((size_recv =  recv(sock , chunk , 256 , 0) ) <= 0)
        {
            break;
        }
        else
        {
            //use received data
     }
}

This seems like a standard problem but I couldnt find an answer online, sorry. Thanks.

Was it helpful?

Solution

TCP is just sending bytes. It is up to the client and server to agree what the bytes mean.

You can use some well-known protocol, like HTTP or FTP, if you can find one that meets your needs. e.g. maybe use HTTP and use MIME types to identify the data.

Otherwise you can "roll your own", with each packet containing a header which tells the recipient how to handle the data.

It is all up to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top