posts - 495,comments - 227,trackbacks - 0

          sockutil.cpp

          #include "stdafx.h"
          #include <iostream.h>
          #include <winsock2.h>
          #include "sockutil.h"
          void ShowError(unsigned int nError)
          {
          void* lpMsgBuf;
          FormatMessage(
          FORMAT_MESSAGE_ALLOCATE_BUFFER |
          FORMAT_MESSAGE_FROM_SYSTEM,
          NULL,
          nError,
          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
          (LPTSTR) &lpMsgBuf,
          0, NULL );

          cout <<"(" << nError << "):" << lpMsgBuf;
          LocalFree(lpMsgBuf);
          }
          BOOL ErrorHandle(const char * position, BOOL condition, const char * file, unsigned int line)
          {
          if(!condition)
          {
          return condition;
          }
          cout <<file << "(" << line << ")" << endl;
          cout <<position<< endl;
          unsigned int nError = GetLastError();
          ShowError(nError);
          return condition;
          }
          void InitializeAddress(DWORD ip, UINT port, sockaddr_in & addr)
          {
          memset(&addr,0,sizeof(addr));
          addr.sin_family = AF_INET;
          addr.sin_addr.s_addr= ip;
          addr.sin_port = htons(port);
          }
          int SendData(SOCKET hSocket, const char * data, int length)
          {
          int result;
          int pos = 0;
          while(pos < length)
          {
          result = send(hSocket, data + pos, length - pos , 0);
          if(result > 0 )
          {
          pos += result;
          }else{
          return result;
          }
          }
          return length;
          }

          demo1.cpp

          // demo1.cpp : Defines the entry point for the console application.
          //
          #include "stdafx.h"
          #include <stdio.h>
          #include <winsock2.h>
          #pragma comment(lib,"ws2_32")
          #include "sockutil.h"
          int main(int argc, char* argv[])
          {
          unsigned short wVersion;
          WSADATA wsa;
          wVersion = MAKEWORD(2,2);
          if(ERRORHANDLE(WSAStartup(wVersion, &wsa) != 0))
          {
          return -1;
          }
          SOCKET hSocket;
          int a = PROTO_ICMP;
          hSocket = socket(AF_INET,SOCK_STREAM,0);
          if(ERRORHANDLE(hSocket == INVALID_SOCKET))
          {
          WSACleanup();
          return -1;
          }
          sockaddr_in addr;
          InitializeAddress(INADDR_ANY, 2000, addr);
          if(ERRORHANDLE(SOCKET_ERROR == bind(hSocket, (const sockaddr*) & addr, sizeof(addr))))
          {
          closesocket(hSocket);
          WSACleanup();
          return -1;
          }
          if(ERRORHANDLE(SOCKET_ERROR == listen(hSocket,5)))
          {
          closesocket(hSocket);
          WSACleanup();
          return -1;
          }
          SOCKET hClient;
          int size;
          char buffer[2048];
          int length;
          size = sizeof(addr);
          while(INVALID_SOCKET != (hClient = accept(hSocket,(sockaddr*)&addr, & size)))
          {
          size = sizeof(addr);
          while((length = recv(hClient, buffer, sizeof(buffer),0)) > 0)
          {
          SendData(hClient,buffer, length);
          }
          closesocket(hClient);
          }
          closesocket(hSocket);
          WSACleanup();
          return 0;
          }
          posted on 2008-07-03 15:21 SIMONE 閱讀(366) 評論(0)  編輯  收藏 所屬分類: C++
          主站蜘蛛池模板: 隆林| 唐河县| 日喀则市| 嫩江县| 栖霞市| 永寿县| 南江县| 朔州市| 宁化县| 佛山市| 九龙坡区| 乌审旗| 太康县| 乳山市| 松阳县| 彭泽县| 桦甸市| 于田县| 莫力| 钦州市| 海南省| 柘荣县| 西青区| 巫山县| 平和县| 兰州市| 民县| 长子县| 通山县| 杭锦旗| 筠连县| 灌南县| 郑州市| 武胜县| 富川| 临夏县| 互助| 门源| 沁源县| 南投县| 溧阳市|