我正在学习C ++和套接字,而且我遇到了问题,我已经创建了一个小样本应用来模拟它。我创建一个侦听套接字,然后我连接到套接字,并将客户端套接字存储在SOCKET *
类型的向量中。
在第三个客户端套接字连接后,我迭代向量并关闭它。第一个成功,但最后一个失败,出现-1错误,就好像套接字已经关闭一样。然而只有1个插座实际关闭。
以下是代码
WindowsSocket socketManager(&bitsLibrary);
socketManager.createSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 500, 1024, "127.0.0.1");
socketManager.bindAndStartListening();
vector<SOCKET *> clientSocketList;
int i = 0;
while (i < 3)
{
sockaddr_in client;
SOCKET clientSock = socketManager.acceptClientAndReturnSocket(&client);
cout << "Client Port: " << ntohs(client.sin_port) << endl;
//Add the client socket to the vector
clientSocketList.push_back(&clientSock);
i++;
string data = socketManager.receiveDataOnSocket(&clientSock);
socketManager.sendToSocket(&clientSock, "Hello, here is a response");
}
int index = 1;
for (std::vector<SOCKET *>::iterator it = clientSocketList.begin(); it != clientSocketList.end(); ++it)
{
cout << "Closing socket " << index << " of " << clientSocketList.size() << endl;
try
{
SOCKET *socket = (SOCKET *)*it;
socketManager.closeSocket(socket);
}
catch (SocketException ex)
{
cout << "Failed to close client socket. Error: " << ex.what() << endl;
}
index++;
}
//socketManager.closeSocket(socketManager.returnSocket());
//socketManager.closeSocket(&clientSock);
return EXIT_SUCCESS;
创建套接字函数如下:
bool WindowsSocket::createSocket(int family, int socketType, int protocol, int port, int bufferLength, string ipAddress)
{
stringstream logstream;
//Call the base method to do the prep work e.g. create the buffer
BaseSocket::createsocket(port, bufferLength);
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
logstream << "WSAStartup failed with error: " << iResult;
this->bitsLibrary->writeToLog(logstream.str(), "WindowsSocket", "createSocket");
logstream.clear();
logstream.str(string());
return false;
}
this->serverSocket = socket(family, socketType, protocol);
this->serv_addr.sin_family = family;
if (ipAddress.empty())
{
this->serv_addr.sin_addr.s_addr = INADDR_ANY;
}
else
{
this->serv_addr.sin_addr.s_addr = inet_addr(ipAddress.c_str());
}
this->serv_addr.sin_port = htons(port);
return true;
}
bindAndStartListening函数如下:
bool WindowsSocket::bindAndStartListening(int backlog)
{
stringstream logstream;
//iResult = ::bind(this->serverSocket, result->ai_addr, (int)result->ai_addrlen);
iResult = ::bind(this->serverSocket, (SOCKADDR *)&this->serv_addr, sizeof(this->serv_addr));
if (iResult != 0)
{
logstream << "Socket binding failed with error: " << iResult;
this->bitsLibrary->writeToLog(logstream.str(), "WindowsSocket", "bindAndStartListening");
logstream.clear();
logstream.str(string());
FreeAddrInfo(result);
closesocket(this->serverSocket);
WSACleanup();
return false;
}
freeaddrinfo(result);
iResult = listen(this->serverSocket, backlog);
if (iResult == SOCKET_ERROR)
{
throw SocketException(this->getErrorStringFromErrorCode(WSAGetLastError()).c_str());
return false;
}
logstream << "Socket has binded and is now listening";
this->bitsLibrary->writeToLog(logstream.str(), "WindowsSocket", "bindAndStartListening");
return true;
}
acceptClientAndReturnSocket函数如下:
SOCKET WindowsSocket::acceptClientAndReturnSocket(sockaddr_in *clientAddr)
{
SOCKET clientSocket = INVALID_SOCKET;
//sockaddr_in clientAddr;
socklen_t sin_size = sizeof(struct sockaddr_in);
clientSocket = accept(this->serverSocket, (struct sockaddr*)clientAddr, &sin_size);
return clientSocket;
}
当我调试``clientSocketList`包含的内容时,每个项似乎包含相同的值,如下面的屏幕截图所示:
我不明白为什么将相同的客户端套接字添加到向量中。
答案 0 :(得分:3)
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {LoginPage} from '../login/login';
import {
FirebaseAuth,
AngularFire,
FirebaseListObservable
} from 'angularfire2';
@Component({
templateUrl: 'home.html'
})
export class HomePage {
firelist: FirebaseListObservable<any>;
chat: any;
constructor(public nav: NavController, public af: AngularFire, public auth: FirebaseAuth) {
this.firelist = this.af.database.list('/chat', { //mengambil data
query: {
orderByChild: 'negativtimestamp', //order dari data yang terbaru
}
});
}
chatSend(va, vi) { //mengirim pesan chat
this.af.database.list('/chat').push({ // menambahkan data chat ke firebase
uid: window.localStorage.getItem('uid'),
img: window.localStorage.getItem('photoURL'),
username: window.localStorage.getItem('displayName'),
chat_text: va.chatText,
timestamp: Date.now(),
negativtimestamp: -Date.now() //buat nanti ambil data yang terbaru
})
this.chat = '';
}
logout() { // melakukan logout
window.localStorage.removeItem('email'); // remove email dari localStorage
window.localStorage.removeItem('uid'); // remove uid dari localStorage
window.localStorage.removeItem('displayName'); // remove displayName dari localStorage
this.auth.logout();
this.nav.setRoot(LoginPage);// kembali ke halaman LoginPage
}
}
循环的每次迭代都会创建一个新的while(i < 3)
。您保存它的地址,但在迭代结束时它被销毁。您要保存的是堆栈中对象的地址,并且每次迭代最新的SOCKET
都在同一地址。
clientSock
是clientSocketList
,而您可能想使用std::vector<SOCKET*>
。