c++ Socket传输

服务端-监听接收

#include "winsock2.h"
#pragma     comment(lib,"WS2_32.LIB")

int bufi;

class mserver{

private:
      SOCKET ssock,csock;
      sockaddr_in saddr,caddr;
      int len;
      char buf[4096];

public:
      mserver(){

      }
      mserver(int port,int maxclient){
          saddr.sin_family=AF_INET;
          saddr.sin_port=htons(port);
          saddr.sin_addr.s_addr=INADDR_ANY;
          ssock=socket(AF_INET,SOCK_STREAM,0);
          if(ssock==INVALID_SOCKET){
              printf("build socket error\n");
          }
          if(bind(ssock,(struct sockaddr*)&saddr,sizeof(saddr))==SOCKET_ERROR){
              printf("socket bind fail\n");
          }
          len=sizeof(caddr);
      }

      void close(){
          closesocket(ssock);
          closesocket(csock);
      }

      void beginlisten(){
          if(listen(ssock,10)==SOCKET_ERROR){
              printf("listen fail\n");
          }
      }

      void maccept(){
          sockaddr a;
          csock=accept(ssock,(struct sockaddr*)&caddr,&len);
          if(csock==INVALID_SOCKET){
              printf("accept fail : %d\n",WSAGetLastError());
           
          }
          printf("client from %s connected\n",inet_ntoa(caddr.sin_addr));
      }

      int receive(){
          memset(buf,0,4096);
          if(recv(csock,buf,4096,0)==SOCKET_ERROR){
              return 0;
          }
          printf("message received : %s\n",buf);
          send(csock,buf,4096,0);
          return 1;
      }
   
};

int _tmain(int argc, _TCHAR* argv[])
{
    WSADATA wsaData;
      if (WSAStartup(MAKEWORD(2,1),&wsaData)){
           printf("Winsock initializing fail\n");
           WSACleanup();
           return 0;
      }
      mserver server(12345,10);
      server.beginlisten();
      server.maccept();
      while(server.receive());
      server.close();
      WSACleanup();
      return 0;

}

 

 

客户端发送数据

#include "winsock2.h"
#pragma     comment(lib,"WS2_32.LIB")


class mclient{
private:
      SOCKET sock;
      sockaddr_in saddr;
      int len;
      char buf[4096];
public:
      mclient(){
          sock=socket(AF_INET,SOCK_STREAM,0);
          len=sizeof(saddr);
      }

      bool mconnect(char* addr,int port){
          saddr.sin_family=AF_INET;
          saddr.sin_port=htons(port);
          saddr.sin_addr.s_addr=inet_addr(addr);
          if(connect(sock,(struct sockaddr*)&saddr,len)==SOCKET_ERROR){
              printf("connect fail : %d",WSAGetLastError());
              return false;
          }
          return true;
      }

      void close(){
          closesocket(sock);
      }

      void msend(char* mes){
          send(sock,mes,strlen(mes),0);
      }

      bool receive(){
          memset(buf,0,4096);
          if(recv(sock,buf,4096,0)==SOCKET_ERROR){
              return false;
          }
          else{
              printf("received from server : %s\n",buf);
              return true;
          }
      }

};


int main()
{
    while(1){
      WSADATA wsaData;
      if (WSAStartup(MAKEWORD(2,1),&wsaData)){
           printf("Winsock initializing fail\n");
           WSACleanup();
           return 0;
      }
      mclient a;
      if(!a.mconnect("127.0.0.1",12345)){
          return 0;
      }
      char buf[1024];
      while(scanf("%s",buf)){
          if(strcmp(buf,"quit")==0){
              break;
          }
          a.msend(buf);
          a.receive();

      }
      a.close();
      WSACleanup();
    }
      return 0
}

 ps:如果不在同一函数中声明client与server,将会报错。我用了全局变量后在用了函数声明赋值。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

c++ Socket传输,布布扣,bubuko.com

c++ Socket传输

上一篇:Android应用程序国际化


下一篇:Unity外包团队:关于手机unity游戏开发的技术选型