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 閱讀(357) 評論(0)  編輯  收藏 所屬分類: C++
          主站蜘蛛池模板: 华坪县| 金阳县| 台东市| 曲水县| 阿拉尔市| 石渠县| 台安县| 南乐县| 康马县| 和政县| 乐平市| 大厂| 吴堡县| 灵台县| 子长县| 满洲里市| 辉南县| 西丰县| 图木舒克市| 敦化市| 交口县| 吉水县| 天等县| 武宁县| 通化市| 浦北县| 夏邑县| 河西区| 胶南市| 庄浪县| 瓦房店市| 安仁县| 南汇区| 蕲春县| 迁安市| 大同市| 洛宁县| 宜州市| 高雄市| 棋牌| 永年县|