M T S KHÁI NI M C Ơ B N v winsock :
WinSock :
WinSock API (Windows Sockets Application Programming Interface) là th vi n các hàm giaoư
di n l p trình m ng cho Microsoft Windows. WinSock t ng thích v i h nghi th c m ng ươ
TCP/IP.
Socket :
Socket là đ u (endpoint) trao đ i d li u gi a các quá trình, qua đó các ng d ng WinSock g i /
nh n d li u qua m ng.
Hai lo i Socket :
Stream Socket :
D a trên nghi th c TCP ( Tranmission Control Protocol ), vi c truy n d li u ch th c hi n gi a
hai quá trình đã thi t l p k t n i. D li u đ c truy n tin c y, đúng trình t và không l p l i.ế ế ượ
D li u đ c truy n nh dòng liên t c các byte không phân bi t ranh gi i các record ượ ư
Datagram Socket :
D a trên nghi th c UDP ( User Datagram Protocol ), vi c truy n d li u không yêu c u có s
thi t l p k t n i gi a hai quá trình. D li u truy n không tin c y, có th không đúng trình t ho cế ế
l p l i.
D li u đ c truy n nh dòng các record. ượ ư
Port :
Port xác đ nh duy nh t m t quá trình trên m t máy trong m ng.
M t TCP/IP Socket g m m t đ a ch IP k t h p v i m t port ? xác đ nh duy nh t m t quá trình ế
trên m ng.
M t s port dành riêng :
UDP Ports :
0 Reversed
7 Echo
37 Time
42 Name Server
53 Domain Name Server
69 Trivial File Transfer Program ( TFTP )
514 System Log
. . . . . . . . . .
TCP Ports :
0 Reversed
1 TCP Multiplexor
20 FTP_ Data Connection
21 FTP_ Command Connection
23 TELNET
25 SMTP
42 Name Server
53 Domain Name Server
79 Finger_ find a active user
. . . . . . . . . . . . . . . . . . . . .
L p trình winsock
Các file c n thi t : ế
WINSOCK.DLL hay WSOCK32.DLL ( tuỳ version 16 hay 32 bit c a Windows ).
WINSOCK.LIB hay WSOCK32.LIB.
WINSOCK.H.
Gi i thi u m t s hàm c a WinSock:
D i đây ch gi i thi u ch c năng c a m t s hàm WinSock, không trình bày prototype c a cácướ
hàm
Các hàm kh i đ ng WinSock :
Dùng đ kh i đ u và k t thúc quá trình s d ng WinSock. ế
WSAStartup() : Kh i đ ng WinSock.
WSACleanup() : K t htúc WinSock.ế
WSAGetLastError() : L y mã sai v a x y ra.
Các hàm Socket :
Dùng đ thi t l p quá trình trao đ i d li u trên m ng. ế
socket() : t o socket truy n d li u.
bind() : đ nh danh cho socket v a t o (gán port cho socket).
listen() : l ng nghe k t n i. ế
accept() : ch p nh n k t n i t client. ế
connect() : k t n i v i server.ế
send(), recv() : g i, nh n d li u v i stream socket.
sendto(), recvfrom() : g i, nh n d li u v i datagram socket.
. . . . . . . .
Các hàm khác :
inet_addr() : chuy n t đ a ch IP d ng s th p phân có ch m sang đ a ch internet.
inet_ntoa() : ng c l i.ượ
gethostname() : cho tên máy hi n hành.
gethostbyname(),WSAAsyncGetHostByName()
: cho đ a ch IP ng v i tên máy.
gethostbyaddr(),WSAAsyncGetHostByAddr()
: cho tên máy ng v i đ a ch IP.
. . . . . . . .
WinSock error:
L i khi send data
WSANOTINITIALISED A successful AfxSocketInit must occur before using this API.
WSAENETDOWN The Windows Sockets implementation detected that the network subsystem
failed.
WSAEACCES The requested address is a broadcast address, but the appropriate flag was not
set.
WSAEINPROGRESS A blocking Windows Sockets operation is in progress.
WSAEFAULT The lpBuf or lpSockAddr parameters are not part of the user address space, or the
lpSockAddr argument is too small (less than the size of a SOCKADDR structure).
WSAENETRESET The connection must be reset because the Windows Sockets implementation
dropped it.
WSAENOBUFS The Windows Sockets implementation reports a buffer deadlock.
WSAENOTCONN The socket is not connected (SOCK_STREAM only).
WSAENOTSOCK The descriptor is not a socket.
WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not of type SOCK_STREAM.
WSAESHUTDOWN The socket has been shut down; it is not possible to call SendTo on a socket
after ShutDown has been invoked with nHow set to 1 or 2.
WSAEWOULDBLOCK The socket is marked as nonblocking and the requested operation would
block.
WSAEMSGSIZE The socket is of type SOCK_DGRAM, and the datagram is larger than the
maximum supported by the Windows Sockets implementation.
WSAECONNABORTED The virtual circuit was aborted due to timeout or other failure.
WSAECONNRESET The virtual circuit was reset by the remote side.
WSAEADDRNOTAVAIL The specified address is not available from the local machine.
WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket.
WSAEDESTADDRREQ A destination address is required.
WSAENETUNREACH The network cannot be reached from this host at this time.
L i khi receive data
WSANOTINITIALISED A successful AfxSocketInit must occur before using this API.
WSAENETDOWN The Windows Sockets implementation detected that the network subsystem
failed.
WSAENOTCONN The socket is not connected.
WSAEINPROGRESS A blocking Windows Sockets operation is in progress.
WSAENOTSOCK The descriptor is not a socket.
WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not of type SOCK_STREAM.
WSAESHUTDOWN The socket has been shut down; it is not possible to call Receive on a
socket after ShutDown has been invoked with nHow set to 0 or 2.
WSAEWOULDBLOCK The socket is marked as nonblocking and the Receive operation would
block.
WSAEMSGSIZE The datagram was too large to fit into the specified buffer and was truncated.
WSAEINVAL The socket has not been bound with Bind.
WSAECONNABORTED The virtual circuit was aborted due to timeout or other failure.
WSAECONNRESET The virtual circuit was reset by the remote side.
Trình t thi t l p quá trình trao đ i d li u : ế
Dùng Stream Socket :