1 / 11

CS 471/571

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 ).

quinta
Download Presentation

CS 471/571

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 471/571 Sockets 5

  2. 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

  3. 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

  4. gethostbyname • struct hostent *host = gethostbyname(“cs.uwlax.edu”) • server.sin_addr = * ((struct in_addr *) host->h_addr);

  5. 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))); }

  6. 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 */

  7. 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]; };

  8. struct in_addr struct in_addr { in_addr_t s_addr; };

  9. 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));

  10. 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));

  11. 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));

More Related