110 likes | 252 Views
CS 471/571. Sockets 5. Create a UDP Socket ( C ). int sockfd = socket(AF_INET, SOCK_DGRAM, 0); sockfd is called a socket descriptor There is no address associated with the descriptor at this point The 0 parameter represents a protocol option. Rarely used. Create a sockaddr_in struct ( C ).
E N D
CS 471/571 Sockets 5
Create a UDP Socket ( C ) • int sockfd = socket(AF_INET, SOCK_DGRAM, 0); • sockfd is called a socket descriptor • There is no address associated with the descriptor at this point • The 0 parameter represents a protocol option. Rarely used
Create a sockaddr_in struct ( C ) • memset(&server, 0, sizeof(structsockaddr_in)); • Sets all bytes to 0 in server • server.sin_family = AF_INET; • Use IPv4 addressing • server.sin_addr.s_addr = inet_addr( ip); • ip should be a string in dotted quad format • If you only know the name of the machine use gethostbyname • server.sin_port = htons(port); • port is a 2 byte int that represents the port number • htons puts the int into network (big endian) ordering
gethostbyname • struct hostent *host = gethostbyname(“cs.uwlax.edu”) • server.sin_addr = * ((struct in_addr *) host->h_addr);
Program to get IP address given a host name #include <stdlib.h> #include <stdio.h> #include <netdb.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { struct hostent *host = gethostbyname(argv[1]); printf("IP Address: %s\n", inet_ntoa(* ((struct in_addr *) host->h_addr_list[0]))); printf("IP Address: %s\n", inet_ntoa(* ((struct in_addr *) host->h_addr))); }
struct hostent struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses from name server */ }; #define h_addr h_addr_list[0] /* address, for backward compatibility */
struct sockaddr_in struct sockaddr_in { __uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; };
struct in_addr struct in_addr { in_addr_t s_addr; };
Create a TCP Socket and connect to a server sockfd = socket(AF_INET, SOCK_STREAM, 0); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(ServerIP); server.sin_port = htons(port); connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in));
Create a TCP Socket and connect to a server using gethostbyname sockfd = socket(AF_INET, SOCK_STREAM, 0); struct hostent *host = gethostbyname(argv[1]); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr = * ((struct in_addr *) host->h_addr)); server.sin_port = htons(argv[2]); connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in));
Create a TCP Server Socket sockfd1 = socket(AF_INET, SOCK_STREAM, 0); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(port); bind(sockfd1, (struct sockaddr *) &server, sizeof(struct sockaddr_in));