📄 wsutil.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/winagent/wsutil.c,v 1.1.1.1 2001/11/05 17:49:19 tneale Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1988-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: wsutil.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:19 tneale * Tornado shuffle * * Revision 1.6 2001/01/19 22:25:13 paul * Update copyright. * * Revision 1.5 2000/03/17 00:15:56 meister * Update copyright message * * Revision 1.4 1998/02/25 04:58:38 sra * Update copyrights. * * Revision 1.3 1997/03/20 06:53:30 sra * DFARS-safe copyright text. Zap! * * Revision 1.2 1997/02/25 16:34:43 mrf * Added RCS log and copyright notice * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*//*--------------------------------------------------------- WSUTIL.C -- Contains utility routines ---------------------------------------------------------*/#include "wsagent.h"//// Public functions.///******************************************************************* NAME: MsgBox SYNOPSIS: A printf-like interface to MessageBox. ENTRY: hwndParent - The "owning" parent window. fuType - A set of MB_* flags. pszFormat - A printf-like format string. ... - Other printf-like arguments as needed. RETURNS: int - The result of the MessageBox API.********************************************************************/int MsgBox( HWND hwndParent, UINT fuType, LPSTR pszFormat, ... ){ char szOutput[MAX_PRINTF_OUTPUT]; va_list ArgList; va_start( ArgList, pszFormat ); wvsprintf( szOutput, pszFormat, ArgList ); va_end( ArgList ); return MessageBox( hwndParent, szOutput, szAppName, fuType );} // MsgBox#if 0/******************************************************************* NAME: WinPrintf SYNOPSIS: A printf-like interface to TextOut ENTRY: hdc - Display context for the text. row - Starting row (* tmHeight). col - Starting column (* tmAveCharWidth). pszFormat - A printf-like format string. ... - Other printf-like arguments as needed.********************************************************************/VOID WinPrintf( HDC hdc, int row, int col, LPSTR pszFormat, ... ){ char szOutput[MAX_PRINTF_OUTPUT]; int cbOutput; va_list ArgList; va_start( ArgList, pszFormat ); cbOutput = wvsprintf( szOutput, pszFormat, ArgList ); va_end( ArgList ); TextOut( hdc, col * tmAveCharWidth, row * tmHeight, szOutput, cbOutput );} // WinPrintf#endif/******************************************************************* NAME: SockerrToString SYNOPSIS: Maps a socket error (like WSAEINTR) to a displayable form (like "Interrupted system call"). ENTRY: serr - The error to map. RETURNS: LPSTR - The displayable form of the error. Will be "Unknown" for unknown errors.********************************************************************/LPSTR SockerrToString( SOCKERR serr ){ switch( serr ) { case WSAENAMETOOLONG : return "Name too long"; case WSANOTINITIALISED : return "Not initialized"; case WSASYSNOTREADY : return "System not ready"; case WSAVERNOTSUPPORTED : return "Version is not supported"; case WSAESHUTDOWN : return "Can't send after socket shutdown"; case WSAEINTR : return "Interrupted system call"; case WSAHOST_NOT_FOUND : return "Host not found"; case WSATRY_AGAIN : return "Try again"; case WSANO_RECOVERY : return "Non-recoverable error"; case WSANO_DATA : return "No data record available"; case WSAEBADF : return "Bad file number"; case WSAEWOULDBLOCK : return "Operation would block"; case WSAEINPROGRESS : return "Operation now in progress"; case WSAEALREADY : return "Operation already in progress"; case WSAEFAULT : return "Bad address"; case WSAEDESTADDRREQ : return "Destination address required"; case WSAEMSGSIZE : return "Message too long"; case WSAEPFNOSUPPORT : return "Protocol family not supported"; case WSAENOTEMPTY : return "Directory not empty"; case WSAEPROCLIM : return "EPROCLIM returned"; case WSAEUSERS : return "EUSERS returned"; case WSAEDQUOT : return "Disk quota exceeded"; case WSAESTALE : return "ESTALE returned"; case WSAEINVAL : return "Invalid argument"; case WSAEMFILE : return "Too many open files"; case WSAEACCES : return "Access denied"; case WSAELOOP : return "Too many levels of symbolic links"; case WSAEREMOTE : return "The object is remote"; case WSAENOTSOCK : return "Socket operation on non-socket"; case WSAEADDRNOTAVAIL : return "Can't assign requested address"; case WSAEADDRINUSE : return "Address already in use"; case WSAEAFNOSUPPORT : return "Address family not supported by protocol family"; case WSAESOCKTNOSUPPORT : return "Socket type not supported"; case WSAEPROTONOSUPPORT : return "Protocol not supported"; case WSAENOBUFS : return "No buffer space is supported"; case WSAETIMEDOUT : return "Connection timed out"; case WSAEISCONN : return "Socket is already connected"; case WSAENOTCONN : return "Socket is not connected"; case WSAENOPROTOOPT : return "Bad protocol option"; case WSAECONNRESET : return "Connection reset by peer"; case WSAECONNABORTED : return "Software caused connection abort"; case WSAENETDOWN : return "Network is down"; case WSAENETRESET : return "Network was reset"; case WSAECONNREFUSED : return "Connection refused"; case WSAEHOSTDOWN : return "Host is down"; case WSAEHOSTUNREACH : return "Host is unreachable"; case WSAEPROTOTYPE : return "Protocol is wrong type for socket"; case WSAEOPNOTSUPP : return "Operation not supported on socket"; case WSAENETUNREACH : return "ICMP network unreachable"; case WSAETOOMANYREFS : return "Too many references"; default : return "Unknown"; }}/******************************************************************* NAME: ResetSocket SYNOPSIS: Performs a "hard" close on the given socket. ENTRY: sock - The socket to close. RETURNS: SOCKERR - 0 if successful, !0 if not.********************************************************************/SOCKERR ResetSocket( SOCKET sock ){ LINGER linger; if( sock == INVALID_SOCKET ) { // // Ignore invalid sockets. // return 0; } // // Enable linger with a timeout of zero. This will // force the hard close when we call closesocket(). // // We ignore the error return from setsockopt. If it // fails, we'll just try to close the socket anyway. // linger.l_onoff = TRUE; linger.l_linger = 0; setsockopt( sock, SOL_SOCKET, SO_LINGER, (char FAR *)&linger, sizeof(linger) ); // // Close the socket. // return closesocket( sock );} // ResetSocket/******************************************************************* NAME: CreateSocket SYNOPSIS: Creates a data socket for the specified address & port. ENTRY: psock - Will receive the new socket ID if successful. type - The socket type (SOCK_DGRAM or SOCK_STREAM). address - The IP address for the socket. port - The port for the socket. RETURNS: SOCKERR - 0 if successful, !0 if not.********************************************************************/SOCKERR CreateSocket( SOCKET FAR * psock, int type, ULONG address, WORD port ){ SOCKET sNew; SOCKERR serr; // // Create the socket. // sNew = socket( PF_INET, type, 0 ); serr = ( sNew == INVALID_SOCKET ) ? WSAGetLastError() : 0; if( serr == 0 ) { SOCKADDR_IN sockAddr; // // Bind an address to the socket. // sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = address; sockAddr.sin_port = port; if( bind( sNew, (SOCKADDR FAR *)&sockAddr, sizeof(sockAddr) ) != 0 ) { serr = WSAGetLastError(); } } if( serr != 0 ) { ResetSocket( sNew ); sNew = INVALID_SOCKET; } *psock = sNew; return serr;} // CreateSocket
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -