You could do a typedef and declare it as just 'samp s1'.
And about transferring the struct across sockets, there shouldn't be any problems there. Isn't it just a bunch of data? I thought you could transfer it just like you could transfer a string buffer.
Correct, the struct is just a bunch of data. Unfortunately the "char *str" element is just the memory address returned by malloc. It is not the data in that memory address.
So in the original code, what is being sent across the socket will be "meaningful" in that the receiver will understand it as a local memory address. Unfortunately, the data at that address in the receiver will not be the same as the data in that address in the originator.
Using code tags for code makes it far more readable, as it presents it in a fixed-size font, allows indenting, and other such things. Use it like {code} int variable; {/code} except with instead of { }. I'll do this before I make a real reply so I can see your code better.
typedef struct
{
int i;
char *str; // things working fine when it is an array say char str[10]; But I want the pointer to void here.}samp;
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = INADDR_ANY;
serv.sin_port = htons(PORTNUM);
mysocket = socket(AF_INET, SOCK_STREAM, 0);
/* bind serv information to mysocket */
if(bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr)) < 0 )
{
printf("Cannot bind to port ");
}
/* start listening, allowing a queue of up to 1 pending connection */
listen(mysocket, 1);
while(1)
{
int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
len = recv(consocket, (char *)s1, sizeof(samp), 0);
printf("\n Rec data len : %d" , len);
printf("\n Received Data : %d %s\n", s1->i, s1->str);
close(consocket);
}
close(mysocket);
I'm sorry you need that feature, but pointers just don't work that way. That pointer is getting sent raw -- just a raw memory address, pointing to memory in your own process that the reciever simply doesn't have. The data you want remains squarely in your own process, untransmitted.
You'll have to either translate the structure instead of sending it raw, or use an array. Perhaps you could use a different method to denote NULL data? A negative size, maybye?