Network Programming C
Network Programming C
NETWORKING LABORATORY
Introduction to Network
Programming using C/C++
Address Structures
struct sockaddr_in {
uint8_t sin_len; /* length of structure (16) */
sa_family_t sin_family; /* AF_INET */
in_port_t sin_port; /* 16-bit TCP or UDP port number */
struct in_addr sin_addr; /* 32-bit IPv4 address */
char sin_zero[8];
};
struct in_addr {
in_addr_t s_addr; /* 32-bit IPv4 address */
};
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family; /* address family: AF_xxx value */
char sa_data[14]; /* protocol-specific address */
};
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
char *h_addr;
};
const char *inet_ntop(int af, const void *src, char *dst, size_t)
src: in_addr bzw. in6_addr
char dst[INET_ADDRSTRLEN] bzw. char dst[INET6_ADDRSTRLEN]
Socket Creation
int socket(domain,type,proto)
int bind(sd,addr,addrlen)
int createSocket(const sockaddr_in &addr)
{ Socke t domain
AF_INET, PF_INET6
int sd=socket(AF_INET,SOCK_DGRAM,0); Socke t type
if (sd<0) return -1; SOCK_STREAM, SOCK_DGRAM, …
Protocol
0 (a ny), 6 (tcp), 17 (udp)
int yes = 1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof yes);
fcntl(sd,F_SETFL,O_NONBLOCK);
if (bind(sd,reinterpret_cast<const sockaddr *>(&addr),sizeof addr)<0) {
std::cerr << strerror(errno) << std::endl;
return -1;
}
return sd;
}
UDP:
Create a socket with SOCK_DGRAM
Bind the socket to a address (particular IP and port number)
Ex- bind (int sd, struct sockaddr *, socklen_t len);
Now the socket can be used for send and receive operations
TCP:
Create a socket with SOCK_STREAM
Bind the socket to a address (particular IP and port number
If program need to accept any connection request, then
listen on the socket
Listen() - allows to specify the number of backlogs of
connection requests that can be buffered
Closing a connection
shutdown (int sd, int mode)
0: no further sending, 1: no further reception, 2: neither sending nor receiving
close(sd) to clean up – beware of data loss!
Sending Data
Connection-oriented (TCP)
write (int sd, char *buffer, size_t length);
writev (int sd, struct iovec *vector, int count);
List of buffers, each with pointer to memory and length
send (int sd, char *buffer, size_t length, int flags)
May be used for out-of-band data
Connectionless (UDP)
sendto (int sd, char *buffer, size_t length, int flags,
struct sockaddr *target, socklen_t addrlen)
sendmsg (int sd, struct msghdr *msg, int flags)
Target address
Pointer to the memory containing the data
Control information
Receiving Data
Connection-oriented (TCP)
read (int sd, char *buffer, size_t length);
readv (int sd, struct iovec *vector, int count);
List of buffers, each with pointer to memory and length
recv (int sd, char *buffer, size_t length, int flags)
May be used for out-of-band data
Connectionless (UDP)
recvfrom (int sd, char *buffer, size_t length, int flags,
struct sockaddr *target, socklen_t addrlen)
recvmsg (int sd, struct msghdr *msg, int flags)
Sender address
Pointer to the data
Control information
Further Functions
getpeername (int sd, struct sockaddr *peer, size_t *len)
Obtain the address of the communicating peer
getsockname (int sd, struct sockaddr *local, size_t *len)
Obtain the address of the local socket (e.g., if dynamically assigned)
Multicast reception
Multicast JOIN
setsockopt (sd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
struct ip_mreq *mreq, sizeof (ip_mreq));
struct ip_mreq {
struct in_addr imr_multiaddr; /* IP multicast address of
group */
struct in_addr imr_interface; /* local IP address of
interface */
};
Multicast-LEAVE
setsockopt (sd, IPPROTO_IP, IP_DROP_MEMBERSHIP, struct
ip_mreq *mreq, sizeof (ip_mreq));
Select() example
•
struct pollfd {
int fd; // file descriptor
int events; // events to watch for
int revents; // occurred events
};
Poll events:
POLLIN input pending
POLLOUT socket writable (only needed with non-blocking i/o)
POLLHUP, POLLERR
Timeout is specified in milliseconds
-1 == no timeout, 0 == return immediately (perform real polling)
Handling otherwise identical to select()