[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reg send() and receive() in c;

Posted on 2011-05-05
11
Medium Priority
?
318 Views
Last Modified: 2012-05-11
I wanted to know in the following function,

send(int sock, void *mesg, size_t len, int flags)

char mesg[] = "Hi !";
send(cli, mesg, strlen(mesg), 0)

why do we pass the actual message instead of the address to the void pointer with some
typecasting. BTW this is socket programming.

Thanks
0
Comment
Question by:tpat
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35703836
> why do we pass the actual message instead of the address to the void pointer with some typecasting.
So you can change the content of message in one place and affect all the referenced pointers.  
0
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 35703872
>> why do we pass the actual message ...
Did you mean
    why don't we pass the actual message ...
===

    ssize_t send(int s, const void *buf, size_t len, int flags);

A message for sending is just a memory region that contains the message. From the send point of view, it is just a memory region defined by a starting address and the length of the memory region to transmit.

In your case, your message happens to be a c-style string. Your array name mesg, when passed as an argument, is translated into an address, which is the start of array. You chose in your example, to pass the number, 'H', 'i', ' ', and '!'. You chose not to send the terminating null byte of the string "Hi !", as is your perogative.

But you don't just have to send arrays. You could send a long variable if you wanted:
    long mesgLong = 12345633;
    send(cli, &mesgLong, sizeof(long), 0);

The send prototype requires a void* for the 2nd argument (i.e., the address of the memory region you want to transmit). In this example, the value &mesgLong is of the type long*, But, in C, no typecasting is required when passing a pointer of some type to a function requiring a void*.

Now, there could have been a sendLong function which accepts for the 2nd argument the value of that which you wish to send. In that case, the function might look like:
     sendLong (cli, mesgLong, 0);
where the 2nd argument is a long rather than a void*. And since sendLong is now defined to have the 2nd argument as a long, and the passing mechanism is pass-by-value rather than pass-by-pointer, then this function is rather limited - it can only send a single long variable.

You would need a different send for sending a short or a char, or an array of long, for examples.

To generalize the usage of send, the 2nd argument is a void* which allows any object to be sent - just pass the object's starting address, and tell send how many bytes to transmit.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35703970
> send(int sock, void *mesg, size_t len, int flags)
The 2nd argument is of type "void *".
That means, you can send any kind of data.
Suppose, you have a struct variable.
You can send this struct also using (void *).
So, you can send any type of data using this function.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:sarabande
ID: 35704677
in c and c++ a char array like

    char mesg[] = "Hi !";

is not passed by value but by pointer to the first array element.

your send call takes that pointer as void*.

the following would have been absolutely equivalent for the send call:

   char * msg = "Hi!";
   send(cli, msg, strlen(mesg), 0);

Sara



0
 

Author Comment

by:tpat
ID: 35704700
Thank you wesly_chen, sarabande, ssnkuma, phoffric. You guys were helpful.

Appreciate it.
Thanks
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35704705
Repeating my previous remark for emphasis (in case others didn't see it):
  In your case, your message happens to be a c-style string. Your array name mesg, when passed as an argument, is translated into an address, which is the start of array.

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35704713
>> Suppose, you have a struct variable.
>> You can send this struct also using (void *).

In this case, you would have to use the address of the struct. You couldn't say
    send(cli, (void*)mystruct, strlen(mystruct), 0);
Instead, you would have to take the address as follows:
    send(cli, (void*)&mystruct, strlen(mystruct), 0);

In this case, the typecast of (void*) is optional, and generally not used; so normally you would see:
    send(cli, &mystruct, strlen(mystruct), 0);

0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35704738
> In this case, you would have to use the address of the struct
The meaning was implicit. Only when it is very much necessary, we can be explicit.
If we have to start explaining everything, then we might have to start from "What is a pointer"!
0
 

Author Comment

by:tpat
ID: 35704769
Thank you guys, I understand both of you phoffric, ssnkumar. Thanks for your time and effort.

0
 
LVL 40

Expert Comment

by:evilrix
ID: 36518475
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question