Download presentation
Presentation is loading. Please wait.
Published byAleesha Hodges Modified over 8 years ago
1
Chapter 02. Starting Windows Socket
2
IT COOKBOOK - 1 - Goal Error handling routine for Winsock function error Winsock startup and cleanup Socket creation and close
3
IT COOKBOOK - 2 - Winsock error handling Getting error code Example int WSAGetLastError (void) ; if (socketfunction(...) == error) { int errcode = WSAGetLastError(); printf(error message for errcode); }
4
IT COOKBOOK - 3 - Conversion error code to string (1/4) FormatMessage() function - convert error code to error message DWORD FormatMessage ( DWORD dwFlags, // ① option LPCVOID lpSource, // NULL DWORD dwMessageId, // ② error code DWORD dwLanguageId, // ③ language LPTSTR lpBuffer, // ④ starting address of error string DWORD nSize, // 0 va_list* Arguments // NULL ) ; success: length of error message, fail: 0
5
IT COOKBOOK - 4 - Conversion error code to string(2/4) err_quit() function definition - Error message print and exit #include void err_quit(char *msg) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| 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); }
6
IT COOKBOOK - 5 - Conversion error code to string(3/4) Example of err_quit() function Error display if (socket(...) == SOCKET_ERROR) err_quit("socket()"); if (bind(...) == SOCKET_ERROR) err_quit("bind()"); String in err_quit() Error message for error code
7
IT COOKBOOK - 6 - Conversion error code to string(4/4) err_display() function definition #include void err_display(char *msg) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); printf("[%s] %s", msg, (LPCTSTR)lpMsgBuf); LocalFree(lpMsgBuf); }
8
IT COOKBOOK - 7 - Winsock startup and cleanup (1/3) Winsock startup call –wVersionRequested The highest version of Windows Sockets specification that the caller can use. The high-order byte specifies the minor version number; the low-order byte specifies the major version number. –lpWSAData A pointer to the WSADATA data structure int WSAStartup ( WORD wVersionRequested, LPWSADATA lpWSAData ) ; success: 0, fail: error code
9
IT COOKBOOK - 8 - Winsock startup and cleanup(2/3) Winsock cleanup call int WSACleanup (void) ; 성공 : 0, 실패 : SOCKET_ERROR
10
IT COOKBOOK - 9 - Winsock startup and cleanup(3/3) Code example (InitWinsock.cpp): #include int main(int argc, char* argv[]) { // Winsock startup WSADATA wsa; if(WSAStartup(MAKEWORD(2,2), &wsa) != 0) return -1; MessageBox(NULL, " Winsock startup ", “ success", MB_OK); // Winsock cleanup WSACleanup(); return 0; }
11
IT COOKBOOK - 10 - socket creation and close (1/6) Socket creation call –Returning an integer value result, similar to a file desriptor, that can be used in subsequent socket system calls to refer to the socket SOCKET socket ( int af, // Family int type, // Socket type int protocol // Protocol ) ; success: new socket, fail: INVALID_SOCKET
12
IT COOKBOOK - 11 - socket creation and close(2/6) Family #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 */...
13
IT COOKBOOK - 12 - socket creation and closing (3/6) Socket type –Protocol to be used example) TCP 와 UDP protocol(1) Socket typeproperty SOCK_STREA M Providing reliable data transfer Connection-oriented protocol SOCK_DGRAMProviding unreliable data transfer Connectionless protocol protocolfamilySocket type TCP AF_INET SOCK_STREAM UDPSOCK_DGRAM
14
IT COOKBOOK - 13 - socket creation and close(4/6) Protocol familySocket typeProtocol TCP AF_INET SOCK_STREAMIPPROTO_TCP or 0 UDPSOCK_DGRAMIPPROTO_UDP or 0
15
IT COOKBOOK - 14 - socket creation and close(5/6) Socket closing call –Closing socket and returning the related resource int closesocket ( SOCKET s ) ; success: 0, fail: SOCKET_ERROR
16
IT COOKBOOK - 15 - socket creation and close(6/6) Code example int main(int argc, char* argv[]) { // winsock startup... // socket() SOCKET tcp_sock = socket(AF_INET, SOCK_STREAM, 0); if(tcp_sock == INVALID_SOCKET) err_quit("socket()"); MessageBox(NULL, "TCP socket creation success", “ success", MB_OK); // closesocket() closesocket(tcp_sock); // winsock cleanup... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.