Lập trình mạng P2
lượt xem 111
download
Giới thiệu thư viện winsock Giao tiếp lập trình mạng cho phép phát triển ứng dụng giao tiếp trên cùng một máy hoặc nhiều máy khác nhau thông qua môi trường mạng Winsock được hỗ trợ sẵn trong windows cho phép lập trình mạng với giao thức TCP/IP hoặc IPX Lập trình Winsock trong windows ta sử dụng thư viện WINSOCK2.H, WS2_32.LIB Phiên bản winsock hỗ trợ cho các hệ điều hành Windows như sau: .Chương 2 1. Giới thiệu thư viện winsock Khởi động Winsock Trước khi chạy ứng dụng winsock cần khởi động thư viện winsock, winsock DLL bằng hàm WSAStartup...
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Lập trình mạng P2
- Chương 2: Lập trình mạng trong windows
- Chương 2 1. Giới thiệu thư viện winsock - Giao tiếp lập trình mạng cho phép phát triển ứng dụng giao tiếp trên cùng một máy hoặc nhiều máy khác nhau thông qua môi trường mạng - Winsock được hỗ trợ sẵn trong windows cho phép lập trình mạng với giao thức TCP/IP hoặc IPX - Lập trình Winsock trong windows ta sử dụng thư viện WINSOCK2.H, WS2_32.LIB - Phiên bản winsock hỗ trợ cho các hệ điều hành Windows như sau:
- Chương 2 1. Giới thiệu thư viện winsock Khởi động Winsock - Trước khi chạy ứng dụng winsock cần khởi động thư viện winsock, winsock DLL bằng hàm WSAStartup int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); wVersionRequested : version của winsock lpWSAData : trỏ tới struct LPWSADATA
- Chương 2 1. Giới thiệu thư viện winsock Khởi độ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. Giới thiệu thư viện winsock Kết thúc Winsock Gọi hàm int WSACleanup(void);
- Chương 2 2. Tạo socket trong windows - Cú pháp SOCKET socket ( int af, int type, int protocol ); af: họ địa chỉ giao thức, thiết lập là AF_INET nếu ta sử dụng IPv4 type: kiểu giao thức của socket, thiết lập là SOCK_STREAM cho TCP/IP, SOCK_DGRAM cho UDP/IP Protocol: thiết lập là IPPROTO_TCP đối với TCP, IPPROTO_UDP đối với UDP
- Chương 2 2. Tạo socket trong windows - Địa chỉ winsock quản lý địa chỉ thông qua SOCKADDR_IN structure SOCKADDR_IN structure có dạng 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 chỉ IP sin_port : port sin_zero : make the SOCKADDR_IN structure the same size as the SOCKADDR structure.
- Chương 2 3. Xây dựng chương trình giao tiếp có kết nối dùng winsock Server client socket socket bind Address resolution listen connect accept
- Chương 2 3. Xây dựng chương trình giao tiếp có kết nối dùng winsock 3.1 Server binding: int bind( SOCKET s, const struct sockaddr FAR* name, int namelen ); Khi socket được tạo ra cần dùng hàm bind để bind tới địa chỉ s: socket name: kiểu địa chỉ socket struct sockaddr namelen: kích thước của name
- Chương 2 3. Xây dựng chương trình giao tiếp có kết nối dùng winsock Đoạn lệnh tạo 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 dựng chương trình giao tiếp có kết nối dùng winsock Listenning: lắng nghe kết nối từ client int listen( SOCKET s, int backlog ); backlog : chiều dài tối đa của hàng đợi kết nối
- Chương 2 3. Xây dựng chương trình giao tiếp có kết nối dùng winsock accepting: chấp nhận kết nối SOCKET accept( SOCKET s, struct sockaddr FAR* addr, int FAR* addrlen ); addrlen: tham chiếu tới kích thước của SOCKADDR_IN structure
- Chương 2 3. Xây dựng chương trình giao tiếp có kết nối dùng winsock Chương trình phía server: #include #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 dựng chương trình giao tiếp có kết nối 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 dựng chương trình giao tiếp có kết nối 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 dựng chương trình giao tiếp có kết nối dùng winsock 3.2 Client #include #pragma comment(lib, "wsock32.lib") void main(void) { WSADATA wsaData; SOCKET s; SOCKADDR_IN ServerAddr; int Port = 5150; // Initialize Winsock version 2.2 WSAStartup(MAKEWORD(2,2), &wsaData); // Create a new socket to make a client connection.
- Chương 2 3. Xây dựng chương trình giao tiếp có kết nối dùng winsock 3.2 Client #include #pragma comment(lib, "wsock32.lib") void main(void) { WSADATA wsaData; SOCKET s; SOCKADDR_IN ServerAddr; int Port = 5150; // Initialize Winsock version 2.2 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 dựng chương trình giao tiếp có kết nối 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_addr = inet_addr("136.149.3.29"); // 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. Truyền/nhận dữ liệu 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 truyền dữ liệu flags: trạng thái send MSG_DONTROUTE, or MSG_OOB
- Chương 2 4. Truyền/nhận dữ liệu Hàm WSASend : int WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesSent, DWORD dwFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine ); lpBuffers : buffer kiếu LPWSABUF dwBufferCount : số lượng buffer lpNumberOfBytesSent : số lượng bytes đã truyền dwFlags: số bản sao lpOverlapped and lpCompletionRoutine : thông số về đường truyền I/O
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Công nghệ sản xuất xi măng P2
15 p | 254 | 136
-
Tài liệu ASM - PIC (P2)
17 p | 265 | 131
-
HỆ MỜ & NƠRON TRONG KỸ THUẬT ĐIỀU KHIỂN - TS. NGUYỄN NHƯ HIỀN & TS. LẠI KHẮC LÃI - 6
19 p | 102 | 25
-
Giáo trình phân tích phương pháp cấu tạo của hệ thống S7200 ứng dụng vào hệ thống cung cấp điện và bảo vệ các thiết bị điện p2
10 p | 143 | 24
-
Giáo trình phân tích khả năng định vị công trình dẫn tim cốt trong lắp đặt ván khuôn p2
7 p | 81 | 21
-
Giáo trình phân tích công tác định vị công trình dẫn tim cốt công trình trong lắp đặt ván khuôn p2
7 p | 218 | 21
-
LẬP QUI TRÌNH HẠ THỦY TÀU TRỌNG TẢI LỚN TRÊN ĐÀ TRƯỢT NGHIÊNG, chương 7
6 p | 103 | 17
-
Giáo trình phân tích nguyên lý ứng dụng định vị công trình dẫn tim cốt trong lắp đặt ván khuôn p2
7 p | 70 | 9
-
Giáo trình phân tích khả năng vận hành các trạm lặp kế hoạch hai tần số cho kênh RF song công p2
12 p | 68 | 8
-
Giáo trình hình thành quy trình điều khiển định vị công trình dẫn tim cốt trong lắp đặt ván khuôn p2
7 p | 63 | 4
-
Giáo trình phân tích ứng dụng nguyên lý thuật toán hiệu chỉnh trong đường chạy lập trình p2
5 p | 61 | 4
-
Giáo trình hình thành quy trình nghiên cứu phương pháp định tuyến các giao thức trong cấu hình ACP p2
15 p | 64 | 4
-
Giáo trình hướng dẫn ứng dụng lập trình dòng nhiệt riêng do làm lạnh nước châm p2
5 p | 82 | 4
-
Giáo trình phân tích các trạm lặp kế hoạch hai tần số cho kênh RF song công p2
12 p | 59 | 4
-
Giáo trình hướng dẫn phương pháp thiết lập kết nối vào cổng console của mainboard p2
6 p | 71 | 4
-
Giáo trình phân tích khả năng ứng dụng phương thức gán đối tượng cho một giao diện đối lập p2
5 p | 81 | 3
-
Giáo trình phân tích ứng dụng nguyên lý dãy số lập trình vòng quay với các tỷ số tài chính p2
5 p | 59 | 2
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn