Bài giảng Lập trình mạng - Chương 2: Lập trình mạng trong Windows

Chương 2: Lp trình mng trong windows  
Chương 2  
1. Gii thiu thư vin winsock  
- Giao tiếp lp trình mng cho phép phát trin ng dng giao tiếp trên cùng  
mt máy hoc nhiu máy khác nhau thông qua môi trường mng  
- Winsock được htrsn trong windows cho phép lp trình mng vi giao  
thc TCP/IP hoc IPX  
- Lp trình Winsock trong windows ta sdng thư vin WINSOCK2.H,  
WS2_32.LIB  
- Phiên bn winsock htrcho các hệ điu hành Windows như sau:  
Chương 2  
1. Gii thiu thư vin winsock  
Khi động Winsock  
- Trước khi chy ng dng winsock cn khi động thư vin winsock, winsock  
DLL bng hàm WSAStartup  
int WSAStartup(  
WORD wVersionRequested,  
LPWSADATA lpWSAData  
);  
wVersionRequested : version ca winsock  
lpWSAData : trti struct LPWSADATA  
Chương 2  
1. Gii thiu thư vin winsock  
Khi động Winsock  
- typedef struct WSAData  
{
WORD wVersion;  
WORD wHighVersion;  
char szDescription[WSADESCRIPTION_LEN + 1];  
char szSystemStatus[WSASYS_STATUS_LEN + 1];  
unsigned short iMaxSockets;  
unsigned short iMaxUdpDg;  
char FAR * lpVendorInfo;  
} WSADATA, * LPWSADATA;  
Chương 2  
1. Gii thiu thư vin winsock  
Kết thúc Winsock  
Gi hàm int WSACleanup(void);  
Chương 2  
2. To socket trong windows  
- Cú pháp  
SOCKET socket (  
int af,  
int type,  
int protocol  
);  
af: họ địa chgiao thc, thiết lp là AF_INET nếu ta sdng IPv4  
type: kiu giao thc ca socket, thiết lp là SOCK_STREAM cho TCP/IP,  
SOCK_DGRAM cho UDP/IP  
Protocol: thiết lp là IPPROTO_TCP đối vi TCP, IPPROTO_UDP đối vi  
UDP  
Chương 2  
2. To socket trong windows  
-
Địa chỉ  
winsock qun lý địa chthông qua SOCKADDR_IN structure  
SOCKADDR_IN structure có dng sau  
struct sockaddr_in  
{
short sin_family;  
u_short sin_port;  
struct in_addr sin_addr;  
char sin_zero[8];  
};  
sin_family : AF_INET  
sin_addr : lưu trữ địa chIP  
sin_port : port  
sin_zero : make the SOCKADDR_IN structure the same size as the SOCKADDR structure.  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
Server  
client  
socket  
socket  
bind  
Address  
resolution  
listen  
accept  
connect  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
3.1 Server  
binding:  
int bind(  
SOCKET s,  
const struct sockaddr FAR* name,  
int namelen  
);  
Khi socket được to ra cn dùng hàm bind để bind ti địa chỉ  
s: socket  
name: kiu địa chsocket struct sockaddr  
namelen: kích thước ca name  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
Đon lnh to socket và bind  
SOCKET s;  
SOCKADDR_IN tcpaddr;  
int port = 5150;  
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  
tcpaddr.sin_family = AF_INET;  
tcpaddr.sin_port = htons(port);  
tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY);  
bind(s, (SOCKADDR *)&tcpaddr, sizeof(tcpaddr));  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
Listenning: lng nghe kết ni tclient  
int listen(  
SOCKET s,  
int backlog  
);  
backlog : chiu dài ti đa ca hàng đợi kết ni  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
accepting: chp nhn kết ni  
SOCKET accept(  
SOCKET s,  
struct sockaddr FAR* addr,  
int FAR* addrlen  
);  
addrlen: tham chiếu ti kích thước ca SOCKADDR_IN structure  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
Chương trình phía server:  
#include <winsock2.h>  
#pragma comment(lib, "wsock32.lib")  
void main(void)  
{
WSADATA wsaData;  
SOCKET ListeningSocket;  
SOCKET NewConnection;  
SOCKADDR_IN ServerAddr;  
SOCKADDR_IN ClientAddr;  
int Port = 5150;  
// Initialize Winsock version 2.2  
WSAStartup(MAKEWORD(2,2), &wsaData);  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
Chương trình phía server:  
// Create a new socket to listen for client connections.  
ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  
// Set up a SOCKADDR_IN structure that will tell bind that we  
ServerAddr.sin_family = AF_INET;  
ServerAddr.sin_port = htons(Port);  
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);  
// Associate the address information with the socket using bind.  
bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr));  
// Accept a new connection when one arrives.  
NewConnection = accept(ListeningSocket, (SOCKADDR *)  
&ClientAddr,&ClientAddrLen));  
// At this point you can do two things with these sockets. Wait  
// for more connections by calling accept again on ListeningSocket  
// and start sending or receiving data on NewConnection  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
Chương trình phía server:  
closesocket(NewConnection);  
closesocket(ListeningSocket);  
// When your application is finished handling the connections,  
// call WSACleanup.  
WSACleanup();  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
3.2 Client  
#include <winsock2.h>  
#pragma comment(lib, "wsock32.lib")  
void main(void)  
{
WSADATA wsaData;  
SOCKET s;  
SOCKADDR_IN ServerAddr;  
int Port = 5150;  
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);  
// Create a new socket to make a client connection.  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
3.2 Client  
#include <winsock2.h>  
#pragma comment(lib, "wsock32.lib")  
void main(void)  
{
WSADATA wsaData;  
SOCKET s;  
SOCKADDR_IN ServerAddr;  
int Port = 5150;  
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);  
// Create a new socket to make a client connection.  
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  
Chương 2  
3. Xây dng chương trình giao tiếp có kết ni  
dùng winsock  
3.2 Client  
// server IP: 136.149.3.29  
ServerAddr.sin_family = AF_INET;  
ServerAddr.sin_port = htons(Port);  
ServerAddr.sin_addr.s_a
// Make a connection to the server with socket s.  
connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));  
// At this point you can start sending or receiving data on  
// the socket s. We will describe sending and receiving data  
closesocket(s);  
// When your application is finished handling the connection, call  
// WSACleanup.  
WSACleanup(); }  
Chương 2  
4. Truyn/nhn dliu  
Hàm send và WSASend (dùng cho socket version 2)  
Hàm send:  
int send(  
SOCKET s,  
const char FAR * buf,  
int len,  
int flags  
);  
buf : bộ đệm truyn dliu  
flags: trng thái send MSG_DONTROUTE, or MSG_OOB  
Chương 2  
4. Truyn/nhn dliu  
Hàm WSASend :  
int WSASend(  
SOCKET s,  
LPWSABUF lpBuffers,  
DWORD dwBufferCount,  
LPDWORD lpNum
DWORD dwFlags,  
LPWSAOVERLAPPED lpOverlapped,  
);  
lpBuffers : buffer kiếu LPWSABUF  
dwBufferCount : slượng buffer  
lpNumberOfBytesSent : slượng bytes đã truyn  
dwFlags: sbn sao  
lpOverlapped and lpCompletionRoutine : thông svề đường truyn I/O  
Tải về để xem bản đầy đủ
pdf 37 trang Thùy Anh 12/05/2022 1580
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình mạng - Chương 2: Lập trình mạng trong Windows", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_lap_trinh_mang_chuong_2_lap_trinh_mang_trong_windo.pdf