You dont have to store your data in char * format to send it or receive it over network. If you have seen the prototypes for send() and recv() functions, you will see that the data or msg or buf is a void pointer. You can cast it to whatever you want
Code:
int send(int s, const void *msg, size_t len, int flags);
int recv(int s, void *buf, size_t len, int flags);
For. e.g you may use a data structure to compose your data at client end as below,
Client:
Code:
/* message data structure */
struct msg {
int version;
char command[10];
};
struct msg nmsg;
/* Fill the data in nmsg */
nmsg.version = 2;
strcpy(nmsg.command,"HELO");
/*Send the command to server */
/*assuming sockfd is the socket connecting to the server */
send (sockfd,(struct msg *)&nmsg,sizeof(struct msg),0);
Similarly at server end you will have similar data structure defined
Server:
Code:
/* message data structure */
struct msg {
int version;
char command[10];
};
struct msg rmsg;
int rc;
/* Receive message from client */
rc = recv (sockfd,(struct msg *)&rmsg,sizeof(struct msg),0);
if (rc > 0)
{
printf(" Data Received: %d bytes\n Version: %d \t
Command: %s\n",rc,msg.version,rmsg.command);
} else if (rc==0)
{
printf("Connection closed\n');
} else {
printf("Error occured in recv()\n");
exit(0);
}