Introduction to Socket Programming Jeong Eun Young notav








![TCP Client Example /* get the server host entry */ hp = gethostbyname(argv[1]); if TCP Client Example /* get the server host entry */ hp = gethostbyname(argv[1]); if](https://slidetodoc.com/presentation_image_h2/d66074cf4fcde9bc8728ce18fd9d5e7f/image-9.jpg)




![UDP Talker Example /* get the server host entry */ hp = gethostbyname(argv[1]); if UDP Talker Example /* get the server host entry */ hp = gethostbyname(argv[1]); if](https://slidetodoc.com/presentation_image_h2/d66074cf4fcde9bc8728ce18fd9d5e7f/image-14.jpg)















- Slides: 29

Introduction to Socket Programming Jeong, Eun. Young (notav at ndsl. kaist. edu) EE 324 Programming Tutorial 1

Outline q Socket programming q Editor (emacs) q Compiling (Makefile) q Debugging (gdb) 2

Sockets q Sockets Abstraction for data communication A process can send/receive data through sockets 3

Socket API q TCP socket API Stream sockets Connection-oriented 4 tuples (source IP, port, destination IP, port) Reliable q UDP socket API Datagram sockets 2 tuples (source IP, port) Unreliable 4

TCP Socket Programming q TCP socket programming Server first listens for incoming connections Client initiates the connection to the server Server accepts the connection Exchange data Server sends “Hello, world!” to the client Close connection q Reference: Beej’s guide to network programming http: //beej. us/guide/bgnet/output/html/multipage/clientserver. html 5

TCP Server Example /* create a socket */ sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd < 0) { perror("socket"); return -1; } /* set the address family (IPv 4), address (any address), port */ saddr. sin_family = AF_INET; saddr. sin_addr. s_addr = INADDR_ANY; saddr. sin_port = htons(PORT); /* bind the address to the socket */ if (bind(sockfd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in))) { perror("bind"); close(sockfd); return -1; } 6

TCP Server Example /* listen connections */ if (listen(sockfd, BACKLOG) < 0) { perror("listen"); exit(1); } /* reap all dead processes */ sa. sa_handler = sigchld_handler; sigemptyset(&sa. sa_mask); sa. sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } printf("server: waiting for connections. . . n"); 7

TCP Server Example while (1) { // main accept() loop /* accept new connections from clients */ sin_size = sizeof(their_addr); new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("accept"); continue; } inet_ntop(AF_INET, &their_addr. sin_addr, s, sizeof(s)); printf("server: got connection from %sn", s); if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener if (send(new_fd, "Hello, world!", 13, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); // parent doesn't need this } 8
![TCP Client Example get the server host entry hp gethostbynameargv1 if TCP Client Example /* get the server host entry */ hp = gethostbyname(argv[1]); if](https://slidetodoc.com/presentation_image_h2/d66074cf4fcde9bc8728ce18fd9d5e7f/image-9.jpg)
TCP Client Example /* get the server host entry */ hp = gethostbyname(argv[1]); if (hp == NULL) { perror("gethostbyname"); return -1; } /* create stream socket */ sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd < 0) { perror("socket"); return -1; } /* set the destination address */ daddr. sin_family = AF_INET; memcpy(&daddr. sin_addr. s_addr, hp->h_length); daddr. sin_port = htons(PORT); 9

TCP Client Example /* connect to the destination */ if (connect(sockfd, (struct sockaddr *)&daddr, sizeof(struct sockaddr_in))) { close(sockfd); perror("connect"); return -1; } inet_ntop(AF_INET, &daddr. sin_addr, s, sizeof(s)); printf("client: connecting to %sn", s); /* receive message sent from the server */ if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } /* print the received message */ buf[numbytes] = '