如何解决运行时错误 - 调试错误,已调用 abort,c++ 网络
所以我一直在尝试制作一个多客户端聊天服务器,使用线程来接收消息和发送消息。制作套接字似乎是成功的,它绑定并侦听,并且还接受(至少我认为是这样)。在试图找出我的问题是什么之后,我一直搜索没有成功,经过调试我意识到代码在同一点失败,这是在启动接收消息的类方法线程之后(线程是在内部创建的) “accept_con()”方法)。
这是类的实现:
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>
#include "server.h"
#include <thread>
#include <ws2tcpip.h>
#include <mutex>
using namespace std;
mutex m;
static bool SUCCESS_con = false;
Server::Server(int port)
{
memset(data,BUFF_SIZE);
this->port = port;
//init winsock,WSA variables - environment supporting socket programming on windows.
WORD version = MAKEWORD(2,2); //version 2.2 or 0x0202
int validws = WSAStartup(version,&wsData);
if (validws != 0) //check if works
{
cerr << "Couldn't initialize winsock" << endl;
exit(1);
}
//create socket - SOCKET is a number,a file handler,means that the writing is going to be into that file num
connection_s = socket(AF_INET,SOCK_STREAM,0);
listening_s = socket(AF_INET,0); //0 can be also replaced with IPPROTO_TCP
// Bind an IP address and a port to the socket.
addr.sin_family = AF_INET; // version 4
addr.sin_port = htons(port); //host to network short - network uses big endian,so it converts.
addr.sin_addr.S_un.S_addr = ip_addr; //any ip - INADDR_ANY,or inet_addr(*address*)
if (listening_s == INVALID_SOCKET)
{
cerr << "Couldn't create a socket." << endl;
exit(1);
}
if (bind(listening_s,(sockaddr*)&addr,sizeof(addr)) < 0)
{
cerr << "Error binding socket." << endl;
exit(1);
}
//tell winsock the socket is for listening
listen(listening_s,SOMAXCONN); // the socket is for listening,being able to listen to maximum amount.
cout << "Listening..." << endl;
}
void Server::accept_con()
{
sockaddr_in client_addr;
int clientSize;
while (true)
{
clientSize = sizeof(client_addr);
connection_s = accept(listening_s,(sockaddr*)&client_addr,&clientSize); //puts the client info into addr.
cout << "Connection from " << "(" << inet_ntoa(client_addr.sin_addr) << "," << ntohs(client_addr.sin_port) << ")" << endl;
add_client(connection_s);
if (connection_s == INVALID_SOCKET)
break;
thread t1(&Server::recieve_messages,this,ref(connection_s)); //calling object methods requires pointer to member
//and the object itself,the third is the parameters but
//there are none.
}
}
void Server::add_client(SOCKET &client)
{
clients.push_back(client);
}
void Server::recieve_messages(SOCKET &client)
{
if(!SUCCESS_con)
{
strcpy_s(data,"Connection established.\n");
cout << "server is running." << endl;
SUCCESS_con = true;
}
send(client,data,BUFF_SIZE,0);
while (true)
{
m.lock();
if (recv(client,0) <= 0)
break;
if (*data == '*')
break;
broadcast_message(client);
m.unlock();
}
close();
}
Server::~Server()
{
close();
}
void Server::broadcast_message(SOCKET& client)
{
for (auto c : clients)
{
send(client,0);
}
}
void Server::close()
{
closesocket(listening_s);
closesocket(connection_s);
WSACleanup();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。