WinSock Win32 API 的打包类和例子程序winsocket.zip

  • PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 52KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 84
    下载次数
  • 2005-07-30 09:10
    上传日期
WinSock Win32 API 的打包类和例子程序Win Sock Win32 API 的打包类和例子程序WinSock Win32 API 的打包类和例子程序
WinSock Win32 API 的打包类和例子程序winsocket.zip
  • winSocket.cpp
    21.7KB
  • winSocket.h
    2.9KB
  • winSocketEx.cpp
    12.8KB
  • winSocketEx.h
    6.8KB
  • winSocketTestApp.zip
    42.1KB
  • www.pudn.com.txt
    218B
内容介绍
/******************************************* * winSocket.cpp - Implementation of * * winSocket Winsock API wrapper class * * Ryan Lederman ryan@winprog.org * * January 2002 * * THIS CODE IS FREE * * * *******************************************/ #include "winSocket.h" winSocket::winSocket() : m_hSocket(0), m_wVersion(0), m_ErrorNumber(0) { WSADATA wsaD; // Structure initialsed by WSAStartup m_wVersion = MAKEWORD(2,2); // Version number requested memset( m_LastError, 0, ERR_MAXLENGTH ); // Nullify last error memset( &m_sockaddr, 0, sizeof( m_sockaddr ) ); // Nullify structures memset( &m_rsockaddr, 0, sizeof( m_rsockaddr ) ); int result = WSAStartup( m_wVersion, &wsaD ); // Initialize Winsock if( result != 0 ) // WSAStartup failed { set_LastError( "WSAStartup failed!", WSAGetLastError() ); return; } } winSocket::~winSocket() { WSACleanup(); /* Un-initialise Winsock*/ } int winSocket::Create( void ) { /************************************** * FUNCTION: Create * * * * PURPOSE: Initializes the underlying * * SOCKET object for the class. * * * * RETURNS: ERR_WSAERROR upon error, * * otherwise ERR_SUCCESS * * * ***************************************/ if ( (m_hSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET ) { set_LastError( "socket() failed", WSAGetLastError() ); return ERR_WSAERROR; } return ERR_SUCCESS; } int winSocket::Close( void ) { /**************************************** * FUNCTION: Close * * * * PURPOSE: Closes the underlying * * SOCKET object. Does not destroy class * * * * RETURNS: ERR_WSAERROR upon error, * * otherwise ERR_SUCCESS * * * *****************************************/ if ( closesocket( m_hSocket ) == SOCKET_ERROR ) { set_LastError( "closesocket() failed", WSAGetLastError() ); return ERR_WSAERROR; } memset( &m_sockaddr, 0, sizeof( sockaddr_in ) ); memset( &m_rsockaddr, 0, sizeof( sockaddr_in ) ); return ERR_SUCCESS; } int winSocket::Connect( char* strRemote, unsigned int iPort ) { /***************************************** * FUNCTION: Connect * * * * PURPOSE: Establishes TCP/IP connection * * with remote host (strRemote) on port * * # (iPort) * * * * RETURNS: ERR_BADPARAM for invalid * * parameters, ERR_WSAERROR upon error, * * otherwise ERR_SUCCESS * * * ******************************************/ if( strlen( strRemote ) == 0 || iPort == 0 ) return ERR_BADPARAM; hostent *hostEnt = NULL; long lIPAddress = 0; hostEnt = gethostbyname( strRemote ); if( hostEnt != NULL ) { lIPAddress = ((in_addr*)hostEnt->h_addr)->s_addr; m_sockaddr.sin_addr.s_addr = lIPAddress; } else { m_sockaddr.sin_addr.s_addr = inet_addr( strRemote ); } m_sockaddr.sin_family = AF_INET; m_sockaddr.sin_port = htons( iPort ); if( connect( m_hSocket, (SOCKADDR*)&m_sockaddr, sizeof( m_sockaddr ) ) == SOCKET_ERROR ) { set_LastError( "connect() failed", WSAGetLastError() ); return ERR_WSAERROR; } return ERR_SUCCESS; } int winSocket::Listen( int iQueuedConnections ) { /***************************************** * FUNCTION: Listen * * * * PURPOSE: Places the SOCKET in the * * listening state. Requires that Bind() * * be called previously. * * * * RETURNS: ERR_BADPARAM for invalid * * parameters, ERR_WSAERROR upon error, * * otherwise ERR_SUCCESS * * * ******************************************/ if( iQueuedConnections == 0 ) return ERR_BADPARAM; if( listen( m_hSocket, iQueuedConnections ) == SOCKET_ERROR ) { set_LastError( "listen() failed", WSAGetLastError() ); return ERR_WSAERROR; } return ERR_SUCCESS; } int winSocket::Send( SOCKET s, char* strData, int iLen ) { /********************************************** * FUNCTION: Send * * * * PURPOSE: Sends data (strData) to remote * * side on socket s. * * * * RETURNS: ERR_BADPARAM for invalid * * parameters, ERR_WSAERROR upon error, * * otherwise ERR_SUCCESS * * * ***********************************************/ if( strData == NULL || iLen == 0 ) return ERR_BADPARAM; if( send( s, strData, iLen, 0 ) == SOCKET_ERROR ) { set_LastError( "send() failed", WSAGetLastError() ); return ERR_WSAERROR; } return ERR_SUCCESS; } int winSocket::Send( char* strData, int iLen ) { /********************************************** * FUNCTION: Send * * * * PURPOSE: Sends data (strData) to remote * * side on an established TCP/IP connection. * * Requires that Connect be called previously. * * * * RETURNS: ERR_BADPARAM for invalid * * parameters, ERR_WSAERROR upon error, * * otherwise ERR_SUCCESS * * * ***********************************************/ if( strData == NULL || iLen == 0 ) return ERR_BADPARAM; if( send( m_hSocket, strData, iLen, 0 ) == SOCKET_ERROR ) { set_LastError( "send() failed", WSAGetLastError() ); return ERR_WSAERROR; } return ERR_SUCCESS; } int winSocket::Receive( SOCKET s, char* strData, int iLen ) { /*********************************************** * FUNCTION: Receive * * * * PURPOSE: Retreives data from incoming queue * * and copies to (strData). (iLen) will contain * * the length of data read in bytes * * * * RETURNS: ERR_BADPARAM for invalid * * parameters, ERR_WSAERROR upon error, * * otherwise passes what recv() returns. * * * ***********************************************/ if( strData == NULL ) return ERR_BADPARAM; int len = 0; int ret = 0; ret = recv( s, strData, iLen, 0 ); if ( ret == SOCKET_ERROR ) { set_LastError( "recv() failed", WSAGetLastError() ); return ERR_WSAERROR; } return ret; } int winSocket::Receive( char* strData, int iLen ) { /*********************************************** * FUNCTION: Receive * * * * PURPOSE: Retreives data from incoming queue * * and copies to (strData). (iLen) will contain * * the length of data read in bytes * * * * RETURNS: ERR_BADPARAM for invalid * * parameters, ERR_WSAERROR upon error, * * otherwise passes wh
评论
    相关推荐
    • Socket-API.rar
      Socket五种不同的类型API方式编程,流式套接字(stream socket)、数据报套接字(dategram socket)、原始套接字(raw-protocol interface)等。
    • socketAPI.rar
      SocketApi是在netembryo库上修改而成的,方便在嵌入式设备上开发网路应用,由于netembryo库虽然支持SSL,但是有些bug是没有经过验证的。
    • socketAPI.rar
      c/c++ socket 通信,包含了客户端和服务端代码
    • WindowsAPI_Socket.rar
      基于WindowsAPISocket的简易聊天室程序
    • c++ 下Socket编程
      socket即套接字,用于描述地址和端口,是一个通信链的句柄。应用程序通过socket向网络...sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW);前两种较常用。
    • C++用win socket api实现的简单的聊天室程序
      采用面向连结的socket api, 信息发送、接收为阻塞同步方式。为实现多用户聊天,采用了服务器端分发线程处理,主线程阻塞接受客户端socket, 每接收一个socket连接就分发一个线程处理,通过全局变量保存所有客户...
    • VC SOCKET API发送消息数据实例.rar
      VC SOCKET API发送消息数据实例,测试时请先启动服务端应用程序,然后配置好IP地址,在客户端中可两者之间互通消息,实现SOCKET API通信。创建服务器端Socket,类型为SOCK_STREAM,面向连接的通信。
    • socket演示程序
      过去做网络方面的东东的时候,为了试验一些SOCKET API,编写了一个演示的DEMO,可能有朋友用得到,仅在此抛砖引玉。 这个测试工程中对Socket封装的类的文件: Soc.h Soc.cpp 网络传输封装类 SocMesWnd 异步...
    • 基于Socket API编程接口实现的UDP服务器
      本程序基于Socket API编程接口实现的作为UDP服务器使用,网络调试助手作为客户端,发送“GET”可获取开发板系统时间 。固件程序为UDP服务器,默认IP为192.168.1.30。固件创建socket并使用8080作为服务器端口号,通过...
    • 基于Socket API编程接口实现的智能TCP客户端
      固件创建socket并连接服务器,通过socket函数设置服务类型:其中SOCK_STRERM为TCP,SOCK_DGRAM为UDP。通过setsockopt函数设置接收超时时间,不必永久等待。 备注: 本程序是基于正点原子哥的STM32H7开发板程序,参考...