Introduction to Socket Programming Jeong Eun Young notav

  • Slides: 29
Download presentation
Introduction to Socket Programming Jeong, Eun. Young (notav at ndsl. kaist. edu) EE 324

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

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

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,

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

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

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");

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

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 = gethostbyname(argv[1]); if

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,

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] = ''; printf("client: received '%s'n", buf); close(sockfd); 10

UDP Socket Programming q UDP socket programming No connection establishment! Server binds to a

UDP Socket Programming q UDP socket programming No connection establishment! Server binds to a port (bind()) Exchange data Server waits for incoming messages (recvfrom()) Client sends messages (sendto()) q Simple! But there can be loss of data 11

UDP Listener Example /* create a socket */ sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if

UDP Listener Example /* create a socket */ sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 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; } 12

UDP Listener Example printf("listener: waiting to recvfrom. . . n"); addr_len = sizeof their_addr;

UDP Listener Example printf("listener: waiting to recvfrom. . . n"); addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN - 1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("listener: got packet from %sn", inet_ntop(AF_INET, &their_addr. sin_addr, s, sizeof(s))); printf("listener: packet is %d bytes longn", numbytes); buf[numbytes] = ''; printf("listener: packet contains "%s"n", buf); close(sockfd); 13

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 (hp == NULL) { perror("gethostbyname"); return -1; } /* create a socket */ sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sockfd < 0) { perror("socket"); return -1; } 14

UDP Talker Example /* set the destination address */ daddr. sin_family = AF_INET; memcpy(&daddr.

UDP Talker Example /* set the destination address */ daddr. sin_family = AF_INET; memcpy(&daddr. sin_addr. s_addr, hp->h_length); daddr. sin_port = htons(PORT); if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&daddr, sizeof(struct sockaddr))) == -1) { perror("talker: sendto"); exit(1); } printf("talker: sent %d bytes to %sn", numbytes, argv[1]); close(sockfd); 15

Emacs q Launching emacs $ emacs server. c q Notations C-x: Ctrl + x,

Emacs q Launching emacs $ emacs server. c q Notations C-x: Ctrl + x, M-x: ESC followed by x (or Alt + x) q Moving around in buffers Forward: →, C-f Backward: ←, C-b Next line: ↓, C-n Previous line: ↑, C-p Searching C-s: forward search, C-r: backward search Jumping M-g g: go to the line 16

Emacs q Region Marking: C-SPC C-x h: Select all M-h: Select paragraph q Killing

Emacs q Region Marking: C-SPC C-x h: Select all M-h: Select paragraph q Killing C-k: kill line, C-u 10 C-k: kill 10 lines q Yanking (Paste) C-y: Yanks last killed text q Undo C-/, C-_, C-x u 17

Emacs q Finally exit! C-x C-c q For more detail http: //www. gnu. org/software/emacs/tour/

Emacs q Finally exit! C-x C-c q For more detail http: //www. gnu. org/software/emacs/tour/ http: //cmgm. stanford. edu/classes/unix/emacs. html 18

Compiling q Simple compiling g++ (source codes) –o (object name) Ex) $ g++ server.

Compiling q Simple compiling g++ (source codes) –o (object name) Ex) $ g++ server. c –o server Ex) $ g++ a. c b. c d. c –o abd q Compile options -g: for debugging -Wall: show as many warnings as possible -DNDEBUG: remove asserts when compiling Ex) $ g++ -g –Wall server. c –o server 19

Makefile q Script for easier compilation g++ main. cpp hello. cpp factorial. cpp -o

Makefile q Script for easier compilation g++ main. cpp hello. cpp factorial. cpp -o hello -> make Basic Makefile Target: dependencies [tab] system command q Example q all: g++ main. cpp hello. cpp factorial. cpp -o hello Reference: http: //mrbook. org/tutorials/make/ 20

Using Dependencies q Build process q Compiler generates object files from source codes Linker

Using Dependencies q Build process q Compiler generates object files from source codes Linker creates executable binary from the object files Example all: hello: main. o factorial. o hello. o g++ main. o factorial. o hello. o main. o: main. cpp g++ -c main. cpp … clean: rm -rf *. o hello 21

Using Variables q Example CC=g++ CFLAGS=-g –Wall all: hello: main. o factorial. o hello.

Using Variables q Example CC=g++ CFLAGS=-g –Wall all: hello: main. o factorial. o hello. o $(CC) main. o factorial. o hello. o main. o: main. cpp $(CC) $(CFLAGS) -c main. cpp … clean: rm -rf *. o hello 22

Complete Example CC=g++ CFLAGS=-g –Wall LDFLAGS= SOURCES=main. cpp hello. cpp factorial. cpp OBJECTS=$(SOURCES: .

Complete Example CC=g++ CFLAGS=-g –Wall LDFLAGS= SOURCES=main. cpp hello. cpp factorial. cpp OBJECTS=$(SOURCES: . cpp=. o) EXECUTABLE=hello all: $(SOURCES) $(EXECUTABLE): $(OBJECTS) –o $@ $(CC) $(LDFLAGS) $(OBJECTS) –o $@. cpp. o: $(CC) $(CFLAGS) –c $< -o $@ clean: rm -rf *. o $(EXECUTABLE) 23

gdb q Debugger Helps pointing problem of your program Reference: http: //www. cs. cmu.

gdb q Debugger Helps pointing problem of your program Reference: http: //www. cs. cmu. edu/~gilpin/tutorial/ 24

Running with gdb q Running program gdb main (gdb) run q Exiting (gdb) quit

Running with gdb q Running program gdb main (gdb) run q Exiting (gdb) quit 25

Inspecting Crashes q Exploring stack frames backtrace (or bt): print backtrace of all stack

Inspecting Crashes q Exploring stack frames backtrace (or bt): print backtrace of all stack frames up: go to the stack frame called by this one down: go to the stack frame called this one help stack for more features q print: print value of expression Ex) print item_to_remove q x: examine memory Ex) x 0 xffbef 014 26

Conditional Breakpoints q Breakpoints Make the program break at a certain point break Linked.

Conditional Breakpoints q Breakpoints Make the program break at a certain point break Linked. List<int>: : remove Or break main. cc: 53 q Giving condition Make the breakpoint only works on a certain condition 1 item_to_remove==1 q Stepping step: forward a line 27

Finding the Bug q Line 77: marker is set to 0 q Line 79:

Finding the Bug q Line 77: marker is set to 0 q Line 79: marker is accessed q Removing line 77 will make it work q For more information http: //www. cs. cmu. edu/~gilpin/tutorial/ http: //www. unknownroad. com/rtfm/gdbtut/ http: //www. cs. umd. edu/~srhuang/teaching/cms c 212/gdb-tutorial-handout. pdf 28

Question? 29

Question? 29