[C – UNIX] Network – Send/Receive Data through Sockets

If you have sockets, you are ready to send or recieve data through them. Let’s find out how you can do it.

1. Sending and Receiving Data Using TCP

send() / recv()

These two functions are for communicating over stream sockets or connected datagram sockets. They are defined in <sys/socket.h>.

int send(int sockfd, const void *msg, int len, int flags);
  • sockfd : the socket descriptor you want to send data to
  • msg : a pointer to the data you want to send
  • len : the length of that data in bytes
  • flags : just set to 0
  • returns the number of bytes actually sent out, or -1 is returned on error
char *msg = "Hello World!";
int len, bytes_sent;
...
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
...

To receive data, recv() function is used.

int recv(int sockfd, void *buf, int len, unsigned int flags);
  • sockfd : the socket descriptor to read from
  • buf : the buffer to read the information into
  • len : the maximum length of the buffer
  • flags : set to 0
  • returns the number of bytes actually read into the buffer, or -1 on error
#define MAXDATASIZE 256
...
char buffer[MAXDATASIZE];
bzero(buffer, MAXDATASIZE);
...
int bytes_recv;
...
bytes_recv = recv(sockfd, buffer, MAXDATASIZE, 0);
...

 

2.  Sending and Receiving Data Using UDP

sendto() / recvfrom()

These two functions are for communicating over unconnected datagram sockets. They are defined in <sys/socket.h>.

int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, socklen_t tolen);
  • sockfd : the socket descriptor you want to send data to
  • msg : a pointer to the data you want to send
  • len : the length of that data in bytes
  • flags : just set to 0
  • to : a pointer to a struct sockaddr
  • tolen : sizeof(struct sockaddr)
  • returns the number of bytes actually sent out, or -1 on error
struct sockaddr_in des_addr;
// set destination address
...
char *msg = "Hello World!";
int bytes_sent;
...
bytes_sent = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *)&des_addr, sizeof(struct sockaddr) );
...

The counterpart function is “recvfrom()

int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);
  • sockfd : the socket descriptor to read from
  • buf : the buffer to read the information into
  • len : the maximum length of the buffer
  • flags : set to 0
  • from : a pointer to a struct sockaddr of the source
  • fromlen : sizeof(struct sockaddr)
  • returns the number of bytes received, or -1 on error
#define MAXDATASIZE 256
...
struct sockaddr_in client_addr;
int a_size; // client address size

char buffer[MAXDATASIZE];
bzero(buffer, MAXDATASIZE);
...
int bytes_recv;
...
bytes_recv = recvfrom(sockfd, buffer, MAXDATASIZE, 0, (struct sockaddr *)&client_addr, &a_size);
...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s