我写了一个服务器和一个客户端,并继续'绑定:非套接字上的套接字操作'.
我已经研究了这个问题,有其他代码在另一个应用程序中运行,并且已经耗尽了8个小时试图找到这个bug.
代码是:
void TCPSocket::buildTCPSocket(int port)
{
initializeSocket1();
getSocket();
bindSocket();
listenToSocket();
acceptSocket();
// now you can send() and recv() with the
// connected client via socket connectedTCPSocket
}
void TCPSocket::getSocket()
{
// Get an internet domain socket AF_INET
if(socket1 = socket(AF_INET, SOCK_STREAM,0) == -1)
{
perror("socket");
exit(1);
}
}
void TCPSocket::bindSocket()
{
// Bind to a port on the host
int myAddressSize = sizeof(myAddress);
int bindReturnValue = bind(socket1, (struct sockaddr *) &myAddress, AddressSize);
if (bindReturnValue == -1)
{
perror("bind"); // …Run Code Online (Sandbox Code Playgroud) 我正在编译我以前编译的代码时,我收到"TCPSocket.h:35:错误:预期','或'......'在数字常量之前".
35号线是 TCPSocket(int port, bool vOutput, const int DIRECTORY_SIZE);
从下面粘贴的类声明
using namespace std;
class TCPSocket
{
public:
#define SEND_BUFFER_LENGTH 80
#define DIRECTORY_SIZE 8192
struct sockaddr_in myAddress, clientAddress;
TCPSocket(int port, bool vOutput, const int DIRECTORY_SIZE);
void buildTCPSocket(int newPort);
void processMessage(char* bufferIn, int currentTCPSocket, int tcpSocket, bool verboseOutput);
int getSocket1();
int getSocket2();
Run Code Online (Sandbox Code Playgroud)
定义或构造函数定义是明显的错误吗?
编辑:好的,所以对于那些将来阅读多年的人来说,这里是更正的构造函数声明:
TCPSocket(int port, bool vOutput);
Run Code Online (Sandbox Code Playgroud)
然后,在构造函数定义中使用定义的DIRECTORY_SIZE.