/////////////////////////////////////////////////////////////////////////////// // // (c) Digi International Inc. 2002. All Rights Reserved // // Digi provides this document "as is," without warranty of any kind, // either expressed or implied, including, but not limited to, the implied // warranties of fitness or merchantability for a particular purpose. Digi // may make improvements and/or changes in this document or in the product(s) // and/or the program(s) described in this document at any time. // // This document could include technical inaccuracies or typographical errors. // Changes are periodically made to the information herein; these changes may // be incorporated in new editions of the publication. // // Digi International Inc. // 11001 Bren Road East // Minnetonka, MN 55343, USA // Tel: +1 (952) 912-3444 // Fax: +1 (952) 912-4952 // http://www.digi.com/ // /////////////////////////////////////////////////////////////////////////////// // MySocket.cpp : // #include "stdafx.h" #include #include "MySocket.h" //--------------------------------------------------------------------------- // // BuildSockAddr // // Description: // // Local utility function for converting an IP address in string format // and a port number to a SOCKADDR representation. // // Parameters: // // SockAddr - Pointer to SOCKADDR struct to be filled in // IPAddress - NULL terminated IP address string (e.g. "10.0.0.1") // Port - Address port number // // Return Value: // // Remarks: // // This function assumes that SockAddr is a valid pointer. // // If IPAddress == NULL, then INADDR_ANY is used. // //--------------------------------------------------------------------------- static void BuildSockAddr(SOCKADDR * const SockAddr, char const * const IPAddress, WORD const Port) { SOCKADDR_IN * const SIn = (SOCKADDR_IN *)SockAddr; assert(SIn != NULL); SIn->sin_family = AF_INET; SIn->sin_addr.S_un.S_addr = (IPAddress != NULL) ? inet_addr(IPAddress) : htonl(INADDR_ANY); SIn->sin_port = htons(Port); } //--------------------------------------------------------------------------- // // MySocketOpen // // Description: // // Creates a new socket and binds to a local name. // // Parameters: // // Type - Must be either SOCK_STREAM or SOCK_DGRAM // Port - Local port number to bind to // // Return Value: // // If no error occurs, a new SOCKET descriptor. Otherwise, // INVALID_SOCKET. // // Remarks: // //--------------------------------------------------------------------------- SOCKET MySocketOpen(int const Type, WORD const Port) { assert((Type == SOCK_STREAM) || (Type == SOCK_DGRAM)); int Protocol = (Type == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP; SOCKET Socket = WSASocket(AF_INET, Type, Protocol, NULL, 0, 0); if (Socket != INVALID_SOCKET) { SOCKADDR SockAddr = {0}; BuildSockAddr(&SockAddr, NULL, Port); if (bind(Socket, &SockAddr, sizeof(SockAddr)) != 0) { closesocket(Socket); Socket = INVALID_SOCKET; } } return Socket; } //--------------------------------------------------------------------------- // // MySocketConnect // // Description: // // Establishes a new connection to a peer. // // Parameters: // // Socket - Valid socket descriptor returned from MySocketOpen // ToIPAddress - IP address of peer to connect to // ToPort - Port number of peer to connect to // // Return Value: // // Zero if no error occurs, otherwise SOCKET_ERROR. // See help on WSAConnect in MSDN for more details. // // Remarks: // //--------------------------------------------------------------------------- int MySocketConnect(SOCKET const Socket, char const * const ToIPAddress, WORD const ToPort) { SOCKADDR SockAddr = {0}; BuildSockAddr(&SockAddr, ToIPAddress, ToPort); return WSAConnect(Socket, &SockAddr, sizeof(SockAddr), NULL, NULL, NULL, NULL); } //--------------------------------------------------------------------------- // // MySocketSend // // Description: // // Transmits data to a peer on a connection oriented socket. // // Parameters: // // Socket - Valid socket descriptor returned from MySocketOpen // Data - Pointer to buffer containing data to send // Length - Number of bytes Data points to // // Return Value: // // Zero if no error occurs, otherwise SOCKET_ERROR. // See help on WSASend in MSDN for more details. // // Remarks: // //--------------------------------------------------------------------------- int MySocketSend(SOCKET const Socket, BYTE const * const Data, DWORD const Length) { WSABUF WSABuf = {0}; WSABuf.buf = (char *)Data; WSABuf.len = Length; DWORD LengthSent = 0; return WSASend(Socket, &WSABuf, 1, &LengthSent, 0, NULL, NULL); } //--------------------------------------------------------------------------- // // MySocketSendTo // // Description: // // Transmits a datagram to a peer using a connectionless socket. // // Parameters: // // Socket - Valid socket descriptor returned from MySocketOpen // Data - Pointer to buffer containing data to send // Length - Number of bytes Data points to // ToIPAddress - IP address of the peer that receives the datagram // ToPort - Port number of the peer that receives the datagram // // Return Value: // // Zero if no error occurs, otherwise SOCKET_ERROR. // See help on WSASendTo in MSDN for more details. // // Remarks: // //--------------------------------------------------------------------------- int MySocketSendTo(SOCKET const Socket, BYTE const * const Data, DWORD const Length, char const * const ToIPAddress, WORD const ToPort) { SOCKADDR SockAddr = {0}; BuildSockAddr(&SockAddr, ToIPAddress, ToPort); WSABUF WSABuf = {0}; WSABuf.buf = (char *)Data; WSABuf.len = Length; DWORD LengthSent = 0; return WSASendTo(Socket, &WSABuf, 1, &LengthSent, 0, &SockAddr, sizeof(SockAddr), NULL, NULL); } //--------------------------------------------------------------------------- // // MySocketRecv // // Description: // // Retrieves data buffered for the connected socket // // Parameters: // // Socket - Valid socket descriptor returned from MySocketOpen // Data - Pointer to buffer that should receive the data // Length - When called this is the number of bytes Data points to // Upon return it is the number of bytes actually copied // into the buffer // // Return Value: // // Zero if no error occurs, otherwise SOCKET_ERROR. // See help on WSARecv in MSDN for more details. // // Remarks: // //--------------------------------------------------------------------------- int MySocketRecv(SOCKET const Socket, BYTE * const Data, DWORD * const Length) { WSABUF WSABuf = {0}; DWORD Flags = 0; WSABuf.buf = (char *)Data; WSABuf.len = *Length; return WSARecv(Socket, &WSABuf, 1, Length, &Flags, NULL, NULL); } //--------------------------------------------------------------------------- // // MySocketRecvFrom // // Description: // // Retrieves a datagram buffered for the connectionless socket // // Parameters: // // Socket - Valid socket descriptor returned from MySocketOpen // Data - Pointer to buffer that should receive the data // Length - When called this is the number of bytes Data points to // Upon return it is the number of bytes actually copied // into the buffer // FromIPAddress - Buffer to recieve a human readable string // representation of the IP address of the peer that // sent the data // FromIPAddressLength - When called this is the length of FromIPAddress // Upon return it is the length of the string copied // into FromIPAddress // // Return Value: // // Zero if no error occurs, otherwise SOCKET_ERROR. // See help on WSARecvFrom in MSDN for more details. // // Remarks: // //--------------------------------------------------------------------------- int MySocketRecvFrom(SOCKET const Socket, BYTE * const Data, DWORD * const Length, char * const FromIPAddress, DWORD * const FromIPAddressLength) { int ReturnValue = 0; WSABUF WSABuf = {0}; WSABuf.buf = (char *)Data; WSABuf.len = *Length; DWORD Flags = 0; SOCKADDR SockAddr = {0}; int SockAddrLength = sizeof(SockAddr); ReturnValue = WSARecvFrom(Socket, &WSABuf, 1, Length, &Flags, &SockAddr, &SockAddrLength, NULL, NULL); if (ReturnValue == 0) { ReturnValue = WSAAddressToString(&SockAddr, SockAddrLength, NULL, FromIPAddress, FromIPAddressLength); } return ReturnValue; } //--------------------------------------------------------------------------- // // MySocketClose // // Description: // // Closes the socket descriptor // // Parameters: // // Socket - Valid socket descriptor returned from MySocketOpen // // Return Value: // // Zero if no error occurs, otherwise SOCKET_ERROR. // See help on closesocket in MSDN for more details. // // Remarks: // //--------------------------------------------------------------------------- int MySocketClose(SOCKET const Socket) { shutdown(Socket, SD_BOTH); return closesocket(Socket); }