Eternal Learner
Eternal Learner

Reputation: 3870

How to send and receive an array of character pointers char *argv[] from server to client in linux socket programming

I have a char * array like

 char *options[n] ; // n = 2 just for example . 

 options[0] = "How to";

 options[1] = "Send";

How do I send "options" from server to client , using just one function call like send. Since the prototype of send is like this int send(int sockfd, const void *msg, int len, int flags); and receive is int recv(int sockfd, void *buf, int len, int flags); I am not sure how to cast "options" such that send and receive can take place in one function call.

Upvotes: 0

Views: 1010

Answers (2)

cHao
cHao

Reputation: 86525

Sending the pointer doesn't send the data. In order for you to transmit strings like that, you'll need to either come up with a protocol to send the data, or guarantee that the sender and receiver are the same process (or at least share the same memory at the same spot).

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

You have to send the actual character strings. The pointers are memory addresses on you machine and are only accesable/meanaingful inside your program.

Even if the client and server were on the same machine the OS would block any attempt to access the client programs memory.

Upvotes: 2

Related Questions