socket通讯

服务端

   WORD wVersionRequested;
    WSADATA wsaData;

    //  初始化socket
    wVersionRequested = MAKEWORD(2, 2);
    if(0 != WSAStartup(wVersionRequested, &wsaData))
    {
        printf("WSAStartup Failed \n");
        printf("WSAStartup Error = %d \n", WSAGetLastError());
        return 1; //  代表失败
    }

    /**
     *  AF_INET 地址族,使用IP(Internet procotl)协议
     *  SOCK_STREAM 使用TCP
     *  最后一个参数指定协议,6是TCP ,17UDP
     */
    SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //  成功返回socket ID,失败返回INVALID_SOCKET
    if (s == INVALID_SOCKET)
    {
      printf("Failed socket\n");
      printf("Socket Error = %d\n", WSAGetLastError()); //  返回错误号
      return -1;
    }
    else
    {
      printf("创建用于监听的套接口,套接口号:[%u]\n", s); 
    }


    //  绑定使用bind()
    struct sockaddr_in Sadd; //  描述本机的端口和IP地址;
    Sadd.sin_family = AF_INET; //  AF_INET 地址族,使用IP(Internet procotl)协议
    Sadd.sin_port = htons(5555); //  端口号,字节序转换函数,把给定系统所采用的字节序称为主机字节序;为了避免不同主机之间的交换数据时对字节序解释不同而导致错误
    Sadd.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//地址转换函数,返回 32 位地址;
    if (SOCKET_ERROR == bind(s, (sockaddr*)&Sadd, sizeof(Sadd))) //  如果错误返回SOCKET_ERROR
    {
      closesocket(s);       
      return WSAGetLastError();
    }
    else
    {
      printf("绑定成功\n");
      printf("本地Ip地址:[%s],本地端口号:[%u]\n", inet_ntoa(Sadd.sin_addr), ntohs(Sadd.sin_port));
    }
    //监听状态
    if (SOCKET_ERROR == listen(s, 2))  //int status = listen(socket_id,3) 3指定了正在等待连接的最大队列长度
    {
      printf("Failed listen()\n");
      printf("listen Error=%d\n", WSAGetLastError());
    }
    //循环接受客户的连接请求
    struct sockaddr_in Cadd;
    int caddLen = sizeof(Cadd);
    SOCKET c;
    while (TRUE)
    {
      printf("进入监听状态.....\n\n");
      c = accept(s, (sockaddr*)& Cadd, &caddLen); // SOCKET child_sock = accept(socket_id,(struct sockaddr_in*)client_addr,sizeof(child_addr));if error 返回INVALID_SOCKET
      if (c == INVALID_SOCKET)
      {
        printf("Failed accept()\n");
        printf("accept Error=%d\n", WSAGetLastError());
      }
      else
        printf("可以在套接口[%u]上发送接收数据了!\n", c);
      //开始发送、接收 注意都要在新套接口c上进行
      //结束发送、接收
      char buffer[32];
      recv(c, buffer, 32, 0);
      printf("%s\n",buffer);
      closesocket(c);
      printf("与主机IP地址是:[%s]\n|端口号是:[%u]的连接完毕\n", inet_ntoa(Cadd.sin_addr), ntohs(Cadd.sin_port));
      char xx;
      printf("\n需要退出吗?(Y\n)");
      scanf("%c", &xx);
      if (xx == 'Y' || xx == 'y')
      {
        break;
      }
    }
    closesocket(s);
    WSACleanup(); //  结束之前,调用WSACleanup()
    return 0;

客户端

 WORD wVersionRequested;
  WSADATA wsaData;
  int err;

  //初始化socket资源
  wVersionRequested = MAKEWORD(2, 2);
  err = WSAStartup(wVersionRequested, &wsaData);
  if(err != 0)
  {
    printf("WSAStartup Failed\n");
    printf("WSAStartup Error=%d\n", WSAGetLastError());
    return 1;  
  }

  //创建用于监听的流式套接口s,使用socket()
  //AF_INET决定了要用ipv4地址
  //SOCK_STREAM流式Socket
  SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (s == INVALID_SOCKET)
  {
    printf("Failed socket\n");
    printf("socket Error=%d\n", WSAGetLastError());
  }
  else
    printf("已创建用于监听的套接口,套接口号:[%u]\n", s);

  //本地地址bind(可以不做这部分,如果不绑定,系统将自动分配)
  struct sockaddr_in Cadd;
  Cadd.sin_family=AF_INET;
  Cadd.sin_port=htons(4444);
  Cadd.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
  if (bind(s,(sockaddr*)&Cadd,sizeof(Cadd))==SOCKET_ERROR)
  {
    printf("Failed bind()/n");
  }

  //要连接的服务器地址信息
  struct sockaddr_in Sadd;
  Sadd.sin_family = AF_INET;
  Sadd.sin_port = htons(5555);
  Sadd.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  //将套接口s与远程主机相连
  if (connect(s, (sockaddr*)& Sadd, sizeof(Sadd)) == INVALID_SOCKET)
  {
    printf("Failed connect()\n");
    printf("connect Error=%d\n", WSAGetLastError());
  }
  else
  {
    //开始发接数据
    printf("连接成功,可以开始发送接收数据了!\n");
    printf("服务器IP地址:[%s]\n 端口号:[%u]\n", inet_ntoa(Sadd.sin_addr), ntohs(Sadd.sin_port));
    send(s,"hello",strlen("hello")+1,0);
    //结束发接数据
  }
  //关闭套接字s,终止对动态链接库的访问
  closesocket(s);
  printf("连接完毕\n");
  WSACleanup();
  return 0;
上一篇:一次神奇的 sql 查询经历,group by 慢查询优化记录


下一篇:sqlyog版本选择