You are on page 1of 40

Lab 1 Fundamentals of Socket Programming

Dr. Tony K. C. Chan

1

Acknowledgements
Thanks for Prof. Y. W. Leung providing valuable materials by which this teaching materials is prepared.

2

References
[1] D. E. Comer and D. L. Stevens, Internetworking with TCP/IP Volume III: Client-Server Programming and Applications, Prentice-Hall, Inc, 1997. [2] Beej’s Guide to Network Programming Using Internet Sockets [Online] Available: http://beej.us/guide/bgnet/ [3] Winsock Functions [Online] Available: http://msdn2.microsoft.com/en-us/library/ms741394.aspx.

3

1. Review of C Programming
1.1 Some data types

 

u_char unsigned 8-bit character u_short unsigned 16-bit integer u_long unsigned 32-bit integer

4

1. … struct info student[40]. student[0]. cont. int id. }. Review of C Programming 1.name = “Tony”.id = 1234. 5 .1 Some data types.  structure: a group of different types of data Example struct info { char name[20]. student[0].

6 . the data type of the following expression is double (double)(x+1).2 Casting of Data Types  Casting: to change the data type of a variable or expression to the designated one. Example If x is an int.1. Review of C Programming 1.

A pointer is a variable that stores the memory address (not the value) of a data item.1. 7 .3 Pointers   Every memory location has an address  we can use this address to access the content in this memory location. Review of C Programming 1.

Review of C Programming 1. Address operator &: The address storing the first memory byte of the variable x in &x.3 Pointers     Declare a pointer: data-type *pointer-variable. /* declare p as a pointer of float /* assign the address of x to p /* assign the content of p to y 8 . Dereference operator *: *p gives the content stored in the memory location with address p. y. y = *p. p = &x. Example: float x. *p.1.

1. If x is a one-dimensional array. … 9 .4 Pointers and array   The name of an array is actually a pointer to the first element in the array.  The address of the 2nd array element is &x[1] or (x+1).  The address of the 1st array element is &x[0] or x. Review of C Programming 1.

0.  memset() specified number of bytes to a value. int len). void *memcpy(void *dest. Why?  Three byte-manipulation functions    void *memset(void *dest. const void *secnd. int len).5 Byte-manipulation functions  We may not use string functions in network programming. sizeof(x)). Example Stores zeros in a field called x: memset(&x. int chr. Review of C Programming 1. const void *src.1. 10 . int len). int memcmp(const void *first.

 memcmp() compares two fields Example Compares the first 10 bytes of fields x and y: if (memcpy(&x. &y.10) == 0) printf(“x and y are the same”). Review of C Programming 1. 11 . &y. sizeof(x)).1.5 Byte-manipulation functions  memcpy() copies the value of one field to another Example Copy the value of y field to the x field: memcpy(&x.

2.g. used by Motorola 6800): 12 . Byte Order  Different computers may have different internal representation of 16/32-bit integer (called host byte order)  Big-endian byte order (e..

used by Intel 80x86) 13 . Byte Order  Little-endian byte order (e..2.g.

u_long htonl(u_long host_long). u_short ntohs(u_short network_short). u_long ntohl(u_long network_long). Functions for converting between host byte order and network byte order:     u_short htons(u_short host_short). Byte Order   TCP/IP specifies a network byte order which is the big-endian byte order. 14 .2.

3. /* value of address */ } 15 . Endpoint Address  structure holds socket address information for many types of sockets struct sockaddr { u_short sa_family. /* type of address family */ char sa_data[14].

Endpoint Address  For TCP/IP.3. 16 . an endpoint address is composed of the following items:   Address family is AF_INET (Address Family for InterNET). Endpoint address in that family is composed of an IP address and a port number.

49152-65535: for client programs. Endpoint Address  Port number   A port number identifies an application running on a computer. and its possible values are used in the following manner:    0-1023: for well-known server applications.3. 17 . The port number is composed of 16 bits. 1024-49151: for user-defined server applications.

Endpoint Address  To deal with struct sockaddr. u_short sin_port. struct sockaddr_in { u_short sin_family.3. } /* address family */ /* port number */ /* IP address */ /* unused (set to zero) */ 18 . struct in_addr sin_addr. char sin_zero[8]. a compatible structure for TCP/IP endpoint address was created as follows.

struct in_addr { u_long s_addr. Endpoint Address  The structure of in_addr is as follows. or 4 bytes */ } 19 . /* that's a 32-bit long.3.

Endpoint Address  sockaddr and sockaddr_in are compatible: If you only use TCP/IP. 20 .3. you can use sockaddr_in without using sockaddr.

21 .s_addr = htonl(3232240696). Endpoint Address  Example 1  The IP address of a server is 192.sin_port = htons(2000). We can specify the endpoint address for this server as follows. Its decimal value is 3232240696.sin_family = AF_INET.56.20. serverAddr. serverAddr.3. … serverAddr. struct sockaddr_in serverAddr.sin_addr.168.

20. cont. 22 . char * serverIP = "192.168.s_addr = inet_addr(serverIP).sin_port = htons(2000). serverAddr.168.g.3.56". 192.56) to an integer value suitable for use as an Internet address. serverAddr. Endpoint Address  Example 1.sin_addr..  Uses inet_addr() converts a string in dotted decimal (e.20. … serverAddr. struct sockaddr_in serverAddr.sin_family = AF_INET.

Endpoint Address  Example 2  We specify the endpoint address for a server as follows: struct sockaddr_in serverAddr. serverAddr.3.sin_port = htons(2000). 23 .  Where the symbolic constant INADDR_ANY represents a wildcard address that matches any of the computer's IP address. … serverAddr. serverAddr.sin_addr.sin_family = AF_INET.s_addr = htonl(INADDR_ANY).

/* Initialize socket software */ /* Use socket functions here */ … WSACleanup().h> #define WSVERS MAKEWORD(2. char *argv[]) { WSADATA wsaData. 0) int main(int argc.0): macro creates WORD value */ /* that specifies version 2.lib" ) /* Specify the linker to link with ws2_32. } /* Deallocate all data structures and socket bindings */ 24 .lib */ #include <stdio.0 /* Declare variables */ WSAStartup(WSVERS. /* MAKEWORD(2. return 0. "/defaultlib:ws2_32.Exercises #pragma comment( linker.h> #include <winsock2. &wsaData).

Write a program to print the IP address in 32-bit integer value. 2.Exercises 1. If you do not convert the byte order. what happen? 25 .

4. Details of Each Function Call  General Sequence of Function Calls CLIENT SIDE WSAStartup socket SERVER SIDE WSAStartup socket TCP Communications connect bind listen accept send recv closesocket WSACleanup recv send closesocket WSACleanup 26 .

… WSAStartup(WSVERS. Details of Each Function Call   Must call WSAStartup() before using socket functions. &wsaData) 27 . Example #define WSVERS MAKEWORD(2. 0) … WSADATA wsaData.4.

0) 28 .4. Details of Each Function Call   Creates a socket Example SOCKET s. … s = socket(PF_INET. SOCK_STREAM.

4. sizeof(serverAddr)). Details of Each Function Call   Specifies the endpoint address for a socket. … /* Specify the server's endpoint address in serverAddr here */ … bind(s. 29 . Example struct sockaddr_in serverAddr. (struct sockaddr * )&serverAddr.

1). 30 . Details of Each Function Call   Makes socket ready to accept incoming request.4. Example listen(s.

(struct sockaddr * )&serverAddr. Details of Each Function Call   Establishes connection to a specified remote server Example struct sockaddr_in serverAddr. 31 . … /* Specify the server's endpoint address in serverAddr here */ … connect(s.4. sizeof(serverAddr)).

32 .4. & return a new socket for the connection Example struct sockaddr_in clientAddr. &len). Details of Each Function Call   Accepts the next incoming connection. SOCKET ss. Int len. (struct sockaddr * )&clientAddr. … ss=accept(s.

33 . … send(s. strlen(message).4. Details of Each Function Call   Sends message Example char *message = "Hello". message. 0).

buf. 34 . … recv(s. BUFMAX.4. Details of Each Function Call   Receives message Example #define BUFMAX 5 … char buf[BUFMAX]. 0).

Details of Each Function Call   Terminates connection Example closesocket(s). 35 .4.

Example WSACleanup().4. Details of Each Function Call   When an application finishes using sockets. it call WSACleanup() to deallocate all data structures and socket bindings. 36 .

4. Details of Each Function Call  General Sequence of Function Calls CLIENT SIDE WSAStartup socket SERVER SIDE WSAStartup socket TCP Communications connect bind listen accept send recv closesocket WSACleanup recv send closesocket WSACleanup 37 .

5.  TIME    ECHO  38 . Three simple services of TCP/IP  Three simple services of TCP/IP  DAYTIME   Triggered by TCP connection or UDP datagram Return the date and time in a format for human Triggered by TCP connection or UDP datagram Return the time in a 32-bit integer Return all the data it receives from a client.

6 with different sockets.Exercises 1. Write a TCP server to provide DAYTIME service. 39 . Write a TCP server to provide ECHO service. the time that elapses between when it sends the request and when it receives a response. Add one-half of E to the time the server sends. 8. 4. 7. Write a UDP server to provide TIME service. Modify the TIME client so it computes E. 2. 6. 3. Write a TCP client for the DAYTIME service. 5. Repeat 1 . Write a TCP client for the ECHO service. Write a UDP client for the TIME service.

THE END 40 .