CS3212 ******* Winsock Tutorial

Download Report

Transcript CS3212 ******* Winsock Tutorial

CS3212 計算機網路概論
Winsock Tutorial
Dev C++ environment setting
• Download Dev-C++ from
http://www.bloodshed.net/devcpp.html
• Scheme 1
• Scheme 2
Scheme 1: No project
• Tools  Compiler Options 
Add “-lws2_32” to the linker
command line
• File  New  Source File
• #include <winsock2.h>
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>
Socket programming
• TCP flow chart
• UDP flow chart
• Data structure of address
• Functions
Server
Client
socket():
Create a socket
bind():
Assign the socket an address
listen():
Prepare for incoming connections
socket():
Create a socket
connect():
Connect the socket to a host
accept():
Create a socket for the incoming
connection
recv() and send():
Communicate through the socket
recv() and send():
Communicate through the socket
closesocket():
Close a socket
closesocket():
Close a socket
Server
Client
socket():
Create a socket
bind():
Assign the socket an address
socket():
Create a socket
recvfrom() and sendto():
Communicate through the socket
recvfrom() and sendto():
Communicate through the socket
closesocket():
Close a socket
closesocket():
Close a socket
Data structure of address
Structure
Usage
struct sockaddr_in {
sin_family = AF_INET;
short sin_family;
sin_port = htons(80);
unsigned short sin_port;
sin_addr=inet_addr(“127.0.0.1”); (for client)
struct in_addr sin_addr;
sin_addr.s_addr = INADDR_ANY; (for server)
char sin_zero[8];
};
The htons function converts a u_short from host to TCP/IP network byte order
(which is big-endian).
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)
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)
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)
Examples
• TCP echo server/client
• UDP echo server/client
CS3212 計算機網路概論
Lab 1
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.
Requirements
• Input: The users enter the URL of the desired web page
without “http://”.
• Output: Print all hyperlinks and the number of hyperlinks.
Note that only <a href=”xxx”> should be counted while <link
href=”xxx”> shouldn’t.
Example
• 9 hyperlinks in http://can.cs.nthu.edu.tw/index.html
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
• www.cs.nthu.edu.tw
• /index.html
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