Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 04. TCP Server/Client.

Similar presentations


Presentation on theme: "Chapter 04. TCP Server/Client."— Presentation transcript:

1 Chapter 04. TCP Server/Client

2 Basic structure and principle of TCP server/client
Goal Basic structure and principle of TCP server/client Socket system call for TCP application Understanding application protocol

3 TCP server/client operation (1/6)
TCP server/client example GET / HTTP/1.1 Accept: image/gif, ... <HTML> <HEAD>...</HEAD>... Web server Web client Web client

4 TCP server/client operation (2/6)
TCP client listen accept recv send connect network

5 TCP server/client operation (3/6)
TCP server/client socket calls (cont’d) - bind : to assign a name to an unnamed socket. - listen: to indicate that it is willing to receive connections. - connect: to establish a connection with a server - send: to send data to peer side - accept: to accept the connection request from client - recv: to receive the data that client sent

6 TCP server/client operation(4/6)
TCP server/client operation principle TCP server waiting TCP server TCP clients #1 Client connecting

7 TCP server/client operation(5/6)
TCP server/client operation principle (cont’d) TCP server TCP client #1 Comm. waiting TCP server TCP client #1 client #2 Comm. waiting

8 TCP server/client operation(6/6)
TCP server/client operation principle (cont’d) TCP server TCP client #1 waiting client #n . . .

9 TCP server/client example
Code Example(TCPServer.cpp, TCPClient.cpp) TCP client TCP server fgets() send() printf() recv()

10 TCP server/client analysis (1/2)
Socket requires three components ① protocol Defined by Socket() system call ② local IP address and port number Server or client side ③ remote IP address and port number

11 TCP server/client analysis (2/2)
Socket data structure server local IP addr. Local port num. Remote IP addr. Remote port num. client application OS network • • •

12 Socket call for TCP server (1/8)
TCP server side socket() bind() recv() send() closesocket() TCP server TCP client connect() listen() accept() network

13 Socket call for TCP server (2/8)
bind() Assign a name to an unnamed socket Define IP address and port number of server side int bind ( SOCKET s, const struct sockaddr* name, int namelen ) ; success: 0, fail: SOCKET_ERROR

14 Socket call for TCP server (3/8)
bind() example SOCKADDR_IN serveraddr; ZeroMemory(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(9000); serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); retval = bind(listen_sock, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); if(retval == SOCKET_ERROR) err_quit("bind()");

15 Socket call for TCP server (4/8)
listen() transit TCP port state to LISTENING state - ready to receive connections. int listen ( SOCKET s, int backlog ) ; success: 0, fail: SOCKET_ERROR

16 Socket call for TCP server (5/8)
listen() example retval = listen(listen_sock, SOMAXCONN); if(retval == SOCKET_ERROR) err_quit("listen()");

17 Socket call for TCP server (6/8)
accept() accept the connection request from client tell the IP address and port number of client SOCKET accept ( SOCKET s, struct sockaddr* addr, int* addrlen ) ; success: new socket, fail: INVALID_SOCKET

18 Socket call for TCP server (7/8)
accept() example // variables for data communiction SOCKET client_sock; SOCKADDR_IN clientaddr; int addrlen; ... while(1){ // accept() addrlen = sizeof(clientaddr); client_sock = accept(listen_sock, (SOCKADDR *)&clientaddr, &addrlen); if(client_sock == INVALID_SOCKET){ err_display("accept()"); continue; }

19 Socket call for TCP server (8/8)
accept() example (cont’d) printf("\n[TCP server] client connection: IP addr=%s, port num=%d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); 078 // data comm with client while(1){ ... } 102 // closesocket() closesocket(client_sock); printf("[TCP server] client exit: IP addr=%s, port num=%d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); }

20 Socket call for TCP client (1/3)
TCP client side TCP server TCP client socket() socket() bind() listen() network accept() connect() recv() send() send() recv() closesocket() closesocket()

21 Socket call for TCP client (2/3)
connect() to establish a connection with a server int connect ( SOCKET s, const struct sockaddr* name, int namelen ) ; success: 0, fail: SOCKET_ERROR

22 Socket call for TCP client (3/3)
connect() example SOCKADDR_IN serveraddr; serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(9000); serveraddr.sin_addr.s_addr = inet_addr(" "); retval = connect(sock, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); if(retval == SOCKET_ERROR) err_quit("connect()");

23 Data transfer socket call (1/10)
Socket data structure server Local IP addr Local port num Remote IP addr Remote port num client application OS network • • • Recv buffer Send buffer

24 Data transfer socket call (2/10)
send() 함수 Data transfer to peer side int send ( SOCKET s, const char* buf, int len, int flags ); success: num of sent bytes, fail: SOCKET_ERROR

25 Data transfer socket call (3/10)
recv() Data receving from peer side int recv ( SOCKET s, char* buf, int len, int flags ); success: num of received bytes or 0(the remote side has closed the connection ), fail: SOCKET_ERROR

26 Data transfer socket call (4/10)
recvn() function 037 int recvn(SOCKET s, char *buf, int len, int flags) 038 { int received; char *ptr = buf; int left = len; 042 while(left > 0){ received = recv(s, ptr, left, flags); if(received == SOCKET_ERROR) return SOCKET_ERROR; else if(received == 0) break; left -= received; ptr += received; } 052 return (len - left); 054 }

27 Data transfer socket call (5/10)
recvn() function principle buf ptr left len 읽은 데이터

28 Data transfer socket call (6/10)
Data send/recv example – TCP client char buf[BUFSIZE+1]; int len; ... while(1){ // data input ZeroMemory(buf, sizeof(buf)); printf("\n[sending data] "); if(fgets(buf, BUFSIZE+1, stdin) == NULL) break; 088 // '\n' char delete len = strlen(buf); if(buf[len-1] == '\n') buf[len-1] = '\0'; if(strlen(buf) == 0) break;

29 Data transfer socket call (7/10)
Data send/recv example – TCP client (cont’d) // data sending retval = send(sock, buf, strlen(buf), 0); if(retval == SOCKET_ERROR){ err_display("send()"); break; } printf("[TCP client] %d bytes sent...\n", retval); 103 // data receiving retval = recvn(sock, buf, retval, 0); if(retval == SOCKET_ERROR){ err_display("recv()"); break; } else if(retval == 0) break;

30 Data transfer socket call (8/10)
Data send/recv example – TCP client(cont’d) // print received data buf[retval] = '\0'; printf("[TCP client] %d bytes received...\n", retval); printf("[received data] %s\n", buf); }

31 Data transfer socket call (9/10)
Data send/recv example– TCP server char buf[BUFSIZE+1]; ... while(1){ // data receiving retval = recv(client_sock, buf, BUFSIZE, 0); if(retval == SOCKET_ERROR){ err_display("recv()"); break; } else if(retval == 0) break; 089 // print received data buf[retval] = '\0'; printf("[TCP/%s:%d] %s\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), buf);

32 Data transfer socket call (10/10)
Data send/recv example– TCP server(cont’d) // data sending retval = send(client_sock, buf, retval, 0); if(retval == SOCKET_ERROR){ err_display("send()"); break; } }

33 Application protocol and message design (1/3)
Define data format and semantic which is exchanged in application level Application protocol example network

34 Application protocol and message design (2/3)
Message definition ① Message definition ② struct DrawMessage1 { int x1, y1; // line starting point int x2, y2; // line ending point int width; // line width int color; // line color }; struct DrawMessage2 { int x1, y1; // circle center int r; // circle radius int fillcolor; // internal color int width; // line width int color; // line color };

35 Application protocol and message design (3/3)
Message definition ③ struct DrawMessage1 { int type; // = LINE int x1, y1; // line starting point int x2, y2; // line starting point int width; // line width int color; // line color }; struct DrawMessage2 int type; // = CIRCLE int x1, y1; // circle center int r; // circle radius int fillcolor; // internal color int width; // line width int color; // line color

36 Application protocol and message design (3/3)
FileSender.cpp, FileReceiver.cpp


Download ppt "Chapter 04. TCP Server/Client."

Similar presentations


Ads by Google