COMP2330 Data Communications and Networking Dr. Chu Xiaowen (Second semester, 2009-2010 academic year) Laboratory 10 Socket Programming Last updated: Mar 19 2010 Objectives: 1. 2. 3. To use Wireshark to analyze UDP packet format To learn UDP socket programming To practice UDP socket programming Structure of the UDP client-server Fig. 1: The structure of client-server UDP programming Socket Programming A. Source Code: UDP Server: http://www.comp.hkbu.edu.hk/~comp2330/example/timeudpsrv.c UDP Client: http://www.comp.hkbu.edu.hk/~comp2330/example/timeudpcli.c B. Compiling the Source Code: The “cl” command is used for the compilation of the c program. However, in default condition “cl” command is not set as environmental variable. Therefore, we need to use the command prompt of the visual studio, as shown in following picture. In order to write a network program, we need to include the winsock2.h header file. During the compilation, we need to link the “ws2_32.lib”. Fig. 2: The example of compiling c program with “cl” command Capturing packets with Wireshark 1. 2. Open Wireshark, and click the “capture options…” Change the “capture filter” to “udp and host Your Neighbour’s IP Address” e.g., 3. 4. udp and host 158.182.6.115 set the buffer size to 20M click to choose ‘Limit packets to 100 bytes’. Fig. 3: The example of Wireshark Capture Options Executing the programs You must have a friend to cooperate with you. Each of you two should execute the server program and client program respectively. 0. Start Wireshark at both of your computers. Capture all UDP packets sending or receiving from your partner IP. 1. At the server computer: execute: timeudpsrv.exe Fig. 4: Execution of the server 2. Ask your friend to perform as the client and execute: [timeudpcli] + [IP of the server computer] timeudpcli 158.182.6.71 Fig. 5: Execution of the client 3. Check if you got the packets in your Wireshark, these packets should be sent from your friend’s PC. 4. Ask your friend to check his or her captured packets. 5. Check the C source programs and compare with the packets captured by Wireshark. This can help you understand the UDP socket programming. 6. Exchange the Server/Client role and repeat step 0-4. A typical Wireshark captured result is shown below: Fig. 6: The result captured by Wireshark References: [1] Wikipedia. UDP. http://en.wikipedia.org/wiki/User_Datagram_Protocol Mar 2009 [2] Wikipedia. Time server. http://en.wikipedia.org/wiki/Time_server Mar2009