WSAStartup() ?? - PowerPoint PPT Presentation

About This Presentation
Title:

WSAStartup() ??

Description:

Chapter 02. * – PowerPoint PPT presentation

Number of Views:8
Avg rating:3.0/5.0
Slides: 14
Provided by: tis134
Category:

less

Transcript and Presenter's Notes

Title: WSAStartup() ??


1
Chapter 02. ??? ?? ????
????
  • ?? ??? ?? ?? ??? ??
  • ?? ???? ?? ??? ??
  • ??? ???? ?? ??? ??
  • ?? 2? ?? 1? 3? ?? 1?

2
?? ?? ?? ??
  • ?? ??
  • ? ??? ??? ??? ?? ??
  • ???? ??? ?? ? ?? ???? ?? ?? ??
  • ? ?????? ??? ???? ??
  • WSAStartup() ??
  • ? ????? ?? ??? ????, ???? ??? ?? ??? ???? ???? ??
  • ???? ?? ??
  • ?? ?? ??
  • ?? ?

int WSAGetLastError (void)
if (????(...) ??) int errcode
WSAGetLastError() printf (errcode? ???? ??
???)
3
?? ??? ???? ??? (1)
  • FormatMessage() ?? ?? ?? -gt ?? ??? (??!)

DWORD FormatMessage ( DWORD dwFlags,
// ? ?? LPCVOID lpSource, //
NULL DWORD dwMessageId, // ? ?? ??
DWORD dwLanguageId, // ? ?? LPTSTR lpBuffer,
// ? ?? ??? ?? ?? DWORD nSize,
// 0 va_list Arguments
// NULL )
?? ?? ???? ??, ?? 0
? FORMAT_MESSAGE_ALLOCATE_BUFFER
FORMAT_MESSAGE_FROM_SYSTEM (?????? ??? ???
?? ?? ??????? ?? ???? ???) gt
lpSourceNULL, nSize0, ArgumentsNULL ? ?????
??? -gt WSAGetLastError() ?? ? ?? ???? ?? ???
???? (???? ???? ??? ??) gt
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ? ??
?????? ??? ??? ?? ??? ?? ?
4
?? ??? ???? ??? (2)
  • err_quit() ?? ??
  • err_quit() ?? ?? ?

include ltwinsock2.hgt include ltstdlib.hgt void
err_quit(char msg) LPVOID lpMsgBuf
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BU
FFER FORMAT_MESSAGE_FROM_SYSTEM,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)lpMsgBuf, 0, NULL)
MessageBox(NULL, (LPCTSTR)lpMsgBuf, msg,
MB_ICONERROR) LocalFree(lpMsgBuf)
exit(-1)
if (socket(...) SOCKET_ERROR)
err_quit("socket()") if (bind(...)
SOCKET_ERROR) err_quit("bind()")
5
?? ??? ???? ??? - (3)
  • ?? ?? ??

6
?? ??? ???? ??? - (4)
  • err_display() ?? ?? ??? ?? -gt printf??? ??

include ltwinsock2.hgt include ltstdlib.hgt void
err_display(char msg) LPVOID lpMsgBuf
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BU
FFER FORMAT_MESSAGE_FROM_SYSTEM,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)lpMsgBuf, 0, NULL) printf("s
s", msg, (LPCTSTR)lpMsgBuf)
LocalFree(lpMsgBuf)
7
?? ???? ?? - (1)
  • ?? ??? ??
  • wVersionRequested(2Byte)
  • ????? ???? ??? ?? ??.
  • ?? 8??? ?(major) ???, ?? 8??? ?(minor) ??? ???
    ??.
  • Ex) ?? 3.2 -gt 0x0203, MAKEWORD(3,2)
  • lpWSAData
  • WSADATA ?? ??? ??.
  • ????? ???? ?? ??? ?? ?? ??? ?? ? ??.

int WSAStartup ( WORD wVersionRequested,
LPWSADATA lpWSAData )

?? 0, ?? ?? ??
  • ?? ?? ??

int WSACleanup (void)
?? 0, ??
SOCKET_ERROR
8
?? ???? ?? - (2)
  • ?? ??

include ltwinsock2.hgt int main(int argc, char
argv) // ?? ??? WSADATA wsa
if(WSAStartup(MAKEWORD(2,2), wsa) ! 0)
return -1 MessageBox(NULL, "?? ??? ??",
"??", MB_OK) // ?? ?? WSACleanup()
return 0
9
?? ??? ?? - (1)
  • ?? ?? ??
  • ???? ??? ????? ???? ??? ? ? ??? ????? ???? ??
  • ?? ??? ? ?? ??? ???(SOCKET ??, 32?? ??)? ??
    ?????(socket descriptor)? ??

SOCKET socket ( int af, // ?? ??
int type, // ?? ?? int protocol //
???? )
?? ??? ??, ?? INVALID_SOCKET
10
?? ??? ?? - (2)
  • ?? ?? (winsock2.h ?? ??)
  • C\Program Files\Microsoft Visual
    Studio\VC98\Include

define AF_UNIX 1 / local to host
(pipes, portals) / define AF_INET 2 /
internetwork UDP, TCP, etc. / define
AF_IMPLINK 3 / arpanet imp addresses
/ define AF_PUP 4 / pup protocols
e.g. BSP / define AF_CHAOS 5 / mit
CHAOS protocols / define AF_NS 6 /
XEROX NS protocols / define AF_IPX AF_NS
/ IPX protocols IPX, SPX, etc. / define
AF_ISO 7 / ISO protocols / define
AF_OSI AF_ISO / OSI is ISO / ...
11
?? ??? ?? - (3)
  • ?? ??
  • ??? ????? ??
  • ?? ?) TCP? UDP ???? ??(1)

?? ?? ? ?
SOCK_STREAM ??? ?? ??? ?? ?? ?? ??? ????(TCP)
SOCK_DGRAM ????? ??? ?? ?? ?? ???? ????(UDP)
??? ???? ?? ?? ?? ??
TCP AF_INET SOCK_STREAM
UDP AF_INET SOCK_DGRAM
12
?? ??? ?? - (4)
  • ????
  • ?? ??? ?? ??? ???? ?? ???? ????? ? ? ?? ??? ??
    ????? ????? ??
  • ?? ?) TCP? UDP ???? ??(2)
  • ?? ?) TCP? UDP ???? ??(3) - ??? ?? ?

??? ???? ?? ?? ?? ?? ????
TCP AF_INET SOCK_STREAM IPPROTO_TCP
UDP AF_INET SOCK_DGRAM IPPROTO_UDP
??? ???? ?? ?? ?? ?? ????
TCP AF_INET SOCK_STREAM 0
UDP AF_INET SOCK_DGRAM 0
13
?? ??? ?? - (5)
  • ?? ?? ??
  • ??? ?? ?? ???? ??

int closesocket ( SOCKET s )

?? 0, ?? SOCKET_ERROR
  • ?? ??

int main (int argc, char argv) // ??
??? ... // socket() SOCKET tcp_sock
socket (AF_INET, SOCK_STREAM, 0) if(tcp_sock
INVALID_SOCKET) err_quit ("socket()")
MessageBox(NULL, "TCP ?? ?? ??", "??", MB_OK)
// closesocket() closesocket(tcp_sock)
// ?? ?? ...
Write a Comment
User Comments (0)
About PowerShow.com