Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4149374
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: C/C++

2014-12-11 11:09:06

以前的一段代码,使用中一直没有问题。
同事简单修改了一下,出现
10022 WSAEINVAL
The socket has not been bound with bind.

点击(此处)折叠或打开

  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <process.h>
  4. #include <winsock.h>
  5. #include <iostream>
  6. #include <windows.h>
  7. #include <fstream>
  8. #include <string>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <thread>

  12. using namespace std;

  13. void initializeSockets()
  14.     {
  15.     try{
  16.         logEvents("SERVER", "Initializing the server");
  17.         WSADATA wsadata;
  18.         if (WSAStartup(0x0202,&wsadata)!=0){
  19.             cout<<"Error in starting WSAStartup()\n";
  20.             logEvents("SERVER", "Error in starting WSAStartup()");
  21.         }else{

  22.             logEvents("SERVER", "WSAStartup was suuccessful");
  23.         }

  24.         gethostname(localhost,20);
  25.         cout<<"hostname: "<<localhost<< endl;
  26.         if((hp=gethostbyname(localhost)) == NULL) {
  27.             cout << "gethostbyname() cannot get local host info?"
  28.                 << WSAGetLastError() << endl;
  29.             logEvents("SERVER", "Cannot get local host info. Exiting....");
  30.             exit(1);
  31.         }

  32.         //Create the server socket
  33.         if((serverSocket = socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
  34.             throw "can't initialize socket";

  35.         //Fill-in Server Port and Address info.
  36.         serverSocketAddr.sin_family = AF_INET;
  37.         serverSocketAddr.sin_port = htons(port);
  38.         serverSocketAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  39.         //Bind the server port
  40.         if (bind(serverSocket,(LPSOCKADDR)&serverSocketAddr,sizeof(serverSocketAddr)) == SOCKET_ERROR)
  41.             throw "can't bind the socket";
  42.         cout << "Bind was successful" << endl;
  43.         logEvents("SERVER", "Socket bound successfully.");

  44.         if(listen(serverSocket,10) == SOCKET_ERROR)
  45.             throw "couldn't set up listen on socket";
  46.         else
  47.             cout << "Listen was successful" << endl;
  48.         logEvents("SERVER", "Socket now listening...");
  49.         //Connection request accepted.
  50.         acceptUserConnections();
  51.     }

  52.     catch(char* desc)
  53.     {
  54.         cerr<<str<<WSAGetLastError()<<endl;
  55.         logEvents("SERVER", desc);
  56.     }

  57.     logEvents("SERVER", "Closing client socket...");
  58.     closesocket(clientSocket);
  59.     logEvents("SERVER", "Closed. \n Closing server socket...");
  60.     closesocket(serverSocket);
  61.     logEvents("SERVER", "Closed. Performing cleanup...");

  62.     WSACleanup();

  63. }

发现删除#include .,就没有问题。基本就可以确认,引入thread的使用,socket调用bind函数时,使用std::bind()
,而不是winsock的bind,只要在bind加上::,引入全局域作用符即可。
阅读(2448) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~