Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS3212 計算機網路概論 Winsock Tutorial

Similar presentations


Presentation on theme: "CS3212 計算機網路概論 Winsock Tutorial"— Presentation transcript:

1 CS3212 計算機網路概論 Winsock Tutorial

2 Dev C++ environment setting
Download Dev-C++ from Scheme 1 Scheme 2

3 Scheme 1: No project Tools  Compiler Options  Add “-lws2_32” to the linker command line File  New  Source File #include <winsock2.h>

4 Scheme 2: Use project File  New  Project
Choose “Console Application” Project  Project Options  Parameters Add Library or Object Choose C:\Dev-Cpp\lib\libws2_32.a #include <winsock2.h>

5 Socket programming TCP flow chart UDP flow chart
Data structure of address Functions

6

7

8 Data structure of address
The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian).

9 Domain name to IP address
Use domain name instead of IP as the address struct sockaddr_in serverAddress struct hostent *h = gethostbyname(hostName) memcpy(&serverAddress.sin_addr, h->h_addr_list[0], h->h_length)

10 Functions WSAStartup(MAKEWORD(2,2),(LPWSADATA)&wsadata)
Load winsock of version 2.2 WSACleanUp() Release winsock socket(AF_INET, SOCK_STREAM, 0) Create a TCP socket (SOCK_STREAM) or UDP socket (SOCK_DGRAM) bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress) Assign serverSocket serverAddress listen(serverSocket, 3) Prepare for incoming connections (maximum 3 connections)

11 accept(serverSocket, (struct sockaddr
accept(serverSocket, (struct sockaddr *)&clientAddress, &clientAddressLen) Create a socket for the incoming connection, and the address of the target host is stored in clientAddress closesocket(serverSocket) Close serverSocket send(serverSocket, buf, len, 0) Send buf of size len (TCP socket) recv(serverSocket, buf, MAX_SIZE, 0) Receive data of maximum size MAX_SIZE, and store the data in buf (TCP socket) sendto(serverSocket, buf, len, 0, (struct sockaddr *)&clientAddress, &clientAddressLen) Send buf of size len to client (UDP socket) recvfrom(socket, buf, MAX_SIZE, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) Receive data of maximum size MAX_SIZE, and store the data in buf (UDP socket)

12 Examples TCP echo server/client UDP echo server/client

13 CS3212 計算機網路概論 Lab 1

14 Description Write a program (using Dev-C++) which can count the number of hyperlinks of a given web page. The main objective is to practice socket programming and try to use HTTP protocol.

15 Requirements Input: The users enter the URL of the desired web page without “ Output: Print all hyperlinks and the number of hyperlinks. Note that only <a href=”xxx”> should be counted while <link href=”xxx”> shouldn’t.

16 Example 9 hyperlinks in

17 Hint Refer to the “winsock tutorial” document for information about Dev-C++ and socket programming. Divide the URL into a host name and a path /index.html

18 Hint Get the source code of a web page
Send a request to the server (GET xxx …\r\n\r\n) Receive responses from the server (you may need to receive more than one responses) Count the number of hyperlinks <a href="xxxx"> Use strtok() to tokenize the source code by space characters (" \t\n") If the string starts with "href" and the previous token ends with "a", then you find a hyperlink


Download ppt "CS3212 計算機網路概論 Winsock Tutorial"

Similar presentations


Ads by Google