160 likes | 303 Views
Chapter 02. Starting Windows Socket. Goal. Error handling routine for Winsock function error Winsock startup and cleanup Socket creation and close. Winsock error handling. Getting error code Example. int WSAGetLastError (void) ;. if ( socketfunction (...) == error ) {
E N D
Chapter 02. Starting Windows Socket
Goal • Error handling routine for Winsock function error • Winsock startup and cleanup • Socket creation and close
Winsock error handling • Getting error code • Example int WSAGetLastError (void) ; if (socketfunction(...) == error) { int errcode = WSAGetLastError(); printf(error message for errcode); }
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
Conversion error code to string(2/4) • err_quit() function definition - Error message print and exit #include <winsock2.h> #include <stdlib.h> 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); }
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
Conversion error code to string(4/4) • err_display() function definition #include <winsock2.h> #include <stdlib.h> 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); }
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
Winsock startup and cleanup(2/3) • Winsock cleanup call int WSACleanup (void) ; 성공: 0, 실패: SOCKET_ERROR
Winsock startup and cleanup(3/3) • Code example (InitWinsock.cpp): #include <winsock2.h> 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; }
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
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 */ ...
socket creation and closing (3/6) • Socket type • Protocol to be used • example) TCP와 UDP protocol(1)
socket creation and close(4/6) • Protocol
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
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 ... }