vc++2010 웹 서버와 클라이언트 구현

7537 단어 필기
다음 코드는 온라인 정리 서버입니다.
// webserver4.cpp :              。
//
/*
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

*/
#include "stdafx.h"
#include 
#include 

#pragma comment(lib,"WS2_32")  
int _tmain(int argc, _TCHAR* argv[])
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 1, 1);

    err = WSAStartup( wVersionRequested, &wsaData);
    if(err != 0)
    {
        return 0;
    }
    if(LOBYTE(wsaData.wVersion) != 1 ||
        HIBYTE(wsaData.wVersion) != 1)
    {
        WSACleanup();
        return 0;
    }

    SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);

    SOCKADDR_IN addrSrv;
    addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_port = htons(8888);

    bind(sockSrv,(SOCKADDR *)&addrSrv,sizeof(SOCKADDR));

    listen(sockSrv,5);

    SOCKADDR_IN addrClient;
    int len = sizeof(SOCKADDR);

    while(1)
    {
        SOCKET sockConn = accept(sockSrv, (SOCKADDR *)&addrClient,&len);
        char sendBuf[100];

        sprintf(sendBuf,"Welcome!");

        send(sockConn,sendBuf,strlen(sendBuf)+1,0);

        char recvBuf[1000];
        int n = recv(sockConn,recvBuf,1000,0);
        recvBuf[n] = 0;
        printf("%s
"
,recvBuf); closesocket(sockConn); } }

클라이언트
// vcHttp.cpp :              。
//






 #include   "stdafx.h"   
  #include   "winsock.h"   
  #pragma   comment(lib,"ws2_32.lib")   
  #define   winsock_version   0x0101   
int _tmain(int argc, _TCHAR* argv[])
  {   
  //I   create     C:/Inetpub/wwwroot/test/test.asp   ,start   the   web   service   
  //start   my   program,   the   result   is   OK.   
  //If   it   works,it   is   written   by   masterz,otherwise   I   don't   know   who   write   it.   
          SOCKADDR_IN   saServer;   
  LPHOSTENT   lphostent;   
  WSADATA   wsadata;   
          SOCKET   hsocket;   
  int   nRet;   
  const   char*   host_name="127.0.0.1";   
  char*   req="POST   /test/test.asp   HTTP/1.0/r/n"   
  "From:   local/r/n"   
  "User-Agent:   post_test/1.0/r/n"   
  "Content-Type:   application/x-www-form-urlencoded/r/n"   
  "Content-Length:   20/r/n/r/n"   
  "type=12345&name=aaaa";   
  if(WSAStartup(winsock_version,&wsadata))   
  printf("can't   initial   socket");   
          lphostent=gethostbyname(host_name);   
          if(lphostent==NULL)   
  printf("lphostent   is   null");   
  hsocket   =   socket(AF_INET,   SOCK_STREAM,   IPPROTO_TCP);   
          saServer.sin_family   =   AF_INET;   
  //   Use   def.   now,   need   to   handle   general   case   
  saServer.sin_port   =   htons(8888);   
  saServer.sin_addr   =   *((LPIN_ADDR)*lphostent->h_addr_list);   
          nRet   =   connect(hsocket,   (LPSOCKADDR)&saServer,   sizeof(SOCKADDR_IN));   
  if   (nRet   ==   SOCKET_ERROR)   
  {   
  printf("can't   connect");   
  closesocket(hsocket);   
  return 0;   
  }   
  else   
  printf("connected   with   %s/n",host_name);   
  nRet   =   send(hsocket,   req,   strlen(req),   0);   
  if   (nRet   ==   SOCKET_ERROR)   
  {   
  printf("send()   failed");   
  closesocket(hsocket);   

  }   
  else   
  printf("send()   OK/n");   
  char   dest[1000];   
  nRet=1;   
  while(nRet>0)   
  {   
  nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);   
  if(nRet>0)   
  dest[nRet]=0;   
  else   
  dest[0]=0;   
  printf("/nReceived   bytes:%d/n",nRet);   
  printf("Result:/n%s",dest);   
  }   
  char a;
  a=getchar();
  }

좋은 웹페이지 즐겨찾기