[英]Windows socket api bind function compile issues
我試圖在Windows上綁定一個TCP套接字,並且當期望該函數返回整數時,出現編譯錯誤。 我不是c ++程序員,通常不能在Windows上工作,因此無法跟蹤此問題。 我注意到的一件事是,如果我不包括winsock頭文件,那么我使用的所有其他處理套接字的函數(例如send,recv,accept和connect)都會引發編譯錯誤,但沒有為bind函數引發一個錯誤。 這使我相信,我在某種程度上引入了與期望不同的綁定函數。
整個應用程序包含的頭文件列表
#include <fstream>
#include <string>
#include <ctime>
#include <mutex>
#include <stdio.h>
#include <strsafe.h>
#include <map>
#include <iterator>
#include <process.h>
#include <queue>
#include <iostream>
#include <winsock.h>
包含的庫是
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "User32.lib")
嘗試使用bind函數的類是
//implement an interface for udp and tcp socket classes to implement
class Socket
{
public:
virtual void sendm(const char *buf) = 0;
virtual int recvm(char *buf) = 0;
struct hostent *phe; /* pointer to host information entry */
struct servent *pse; /* pointer to service information entry */
struct protoent *ppe; /* pointer to protocol information entry */
struct sockaddr_in sin; /* an Internet endpoint address */
int type, status; /* socket descriptor and socket type */
SOCKET s; /* socket */
};
class TCPSocket : public Socket {
public:
TCPSocket(const char *host, const char *service, bool master_socket){
initialize_socket(host, service, master_socket);
}
~TCPSocket(void);
SOCKET initialize_socket(const char *host, const char *service, bool master_socket) {
cout << "host: " << host << endl;
cout << "service: " << service << endl;
char* transport = "tcp";
int qlen = 10;
int portbase = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if (pse = getservbyname(service, transport) ) {
sin.sin_port = htons(ntohs((u_short)pse->s_port) + portbase);
}
else if ((sin.sin_port = htons((u_short)atoi(service))) == 0)
{
cout << "can't get " << service << " service" << endl;
exit(1);
}
/* Map protocol name to protocol number */
if ( (ppe = getprotobyname(transport)) == 0) {
cout << "Can't get \"" << transport << "\" protocol entry "<< endl;
exit(1);
}
/* Map host name to IP address, allowing for dotted decimal */
if (master_socket) {
sin.sin_addr.s_addr = htonl( INADDR_ANY );
}
else {
if ( phe = gethostbyname(host) ) {
memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
}
else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
cout << "Can't get \"" << host << "\" IP address "<< endl;
exit(1);
}
}
type = SOCK_STREAM;
s = socket(AF_INET, type, ppe->p_proto);
if (s == INVALID_SOCKET) {
cout << "Socket Error: " << GetLastError() << endl;
exit(1);
}
if (master_socket) {
status = bind(s, (sockaddr *)&sin, sizeof(sin));
/* if (status != 0) { */
/* cout << "Bind Error: " << service << " port: " << GetLastError(); */
/* } */
status = listen(s, qlen);
if (status != 0) {
cout << "can't listen on " << service << " port: " << GetLastError();
exit(1);
}
} else {
int status = connect(s, (struct sockaddr*)&sin, sizeof(sin));
if ( status != 0 ) {
cout << "could not connect" << endl;
exit(1);
}
}
return s;
}
SOCKET acceptSocket() {
return accept(s, (struct sockaddr*)&sin, (int*)sizeof(sin));
}
void sendm(const char *buf) {
send(s, buf, strlen(buf), 0);
}
int recvm(char *buf) {
return recv(s, buf, BUFFER_SIZE, 0);
}
};
msdn上的bind文檔說此函數返回一個int。
我在嘗試編譯時遇到的編譯錯誤是
error C2440: '=' : cannot convert from 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' to 'int'
with
[
_Forced=false,
_Ret=void,
_Fun=SOCKET &,
_V0_t=sockaddr *,
_V1_t=unsigned int,
_V2_t=std::_Nil,
_V3_t=std::_Nil,
_V4_t=std::_Nil,
_V5_t=std::_Nil,
<unnamed-symbol>=std::_Nil
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
我正在在VirtualBox中的VM上運行Windows 8。 我正在使用Visual Studios 2012命令開發人員命令行來編譯我的代碼。 關於為什么綁定不能按預期工作的任何建議將不勝感激。
這使我相信,我在某種程度上引入了與期望不同的綁定函數。
是的,您正在拉入std :: bind 。 如果在<mutex>
內部<mutex>
,則此行#include <functional>
包括std::bind
。 您可能有一個using namespace std;
使std
名稱空間中所有名稱可見的位置。
另外,您想要的綁定位於Winsock2.h
,但我沒有看到#include
。
請閱讀為什么“使用命名空間標准”; 在C ++中被認為是不好的做法? 。 我建議您刪除所有using指令(這可能是導致此問題的原因)並完全限定所有條件。
當使用<mutex>
並具有“ using namespace std”時
使用::bind()
代替bind()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.