1 / 16

Chapter 02. Starting Windows Socket

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 ) {

Download Presentation

Chapter 02. Starting Windows Socket

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 02. Starting Windows Socket

  2. Goal • Error handling routine for Winsock function error • Winsock startup and cleanup • Socket creation and close

  3. Winsock error handling • Getting error code • Example int WSAGetLastError (void) ; if (socketfunction(...) == error) { int errcode = WSAGetLastError(); printf(error message for errcode); }

  4. 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. 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); }

  6. 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. 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); }

  8. 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. Winsock startup and cleanup(2/3) • Winsock cleanup call int WSACleanup (void) ; 성공: 0, 실패: SOCKET_ERROR

  10. 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; }

  11. 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. 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. socket creation and closing (3/6) • Socket type • Protocol to be used • example) TCP와 UDP protocol(1)

  14. socket creation and close(4/6) • Protocol

  15. 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. 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 ... }

More Related