Tải bản đầy đủ (.pdf) (37 trang)

Tài liệu Lập trình mạng P2 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (223.6 KB, 37 trang )

Chương 2: Lậptrìnhmạng trong windows
Chương 2
1. Giớithiệuthư viện winsock
-Giaotiếplậptrìnhmạ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 đượchỗ trợ sẵn trong windows cho phép lậptrìnhmạng vớigiao
thức TCP/IP hoặcIPX
-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ớithiệuthư 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ớithiệuthư 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ớithiệuthư 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ọđịachỉ giao thức, thiếtlậplàAF_INET nếuta 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
- Địachỉ
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ươngtrìnhgiaotiếpcókếtnối
dùng winsock
socket
bind
listen
accept
socket
Address
resolution
connect
Server client
Chương 2
3. Xây dựng chươngtrìnhgiaotiếpcókếtnối
dùng winsock
3.1 Server
binding:
int bind(
SOCKET s,
const struct sockaddr FAR* name,
int namelen
);
Khi socket đượctạoracần dùng hàm bind để bind tới địachỉ
s: socket

name: kiểu địachỉ socket struct sockaddr
namelen: kích thướccủaname
Chương 2
3. Xây dựng chươngtrìnhgiaotiếpcókếtnối
dùng winsock
Đoạnlệ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ươngtrìnhgiaotiếpcókếtnối
dùng winsock
Listenning: lắng nghe kếtnốitừ client
int listen(
SOCKET s,
int backlog
);
backlog : chiều dài tối đacủa hàng đợikếtnối
Chương 2
3. Xây dựng chươngtrìnhgiaotiếpcókếtnối
dùng winsock
accepting: chấpnhậnkếtnối
SOCKET accept(
SOCKET s,
struct sockaddr FAR* addr,

int FAR* addrlen
);
addrlen: tham chiếutớikíchthướccủa SOCKADDR_IN structure
Chương 2
3. Xây dựng chươngtrìnhgiaotiếpcókếtnối
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 dựng chươngtrìnhgiaotiếpcókếtnố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

×