我在执行程序时遇到错误(在linux中)这个程序模拟聊天。我使用套接字,线程和其他标题。
我有一个服务器 - 客户端结构,首先我必须以服务器模式连接,以便客户端(来自其他终端)可以向我发送消息并且我回答,在这种情况下,从服务器模式发送的每条消息都必须到达所有连接客户。
执行时,我将./main -s -p 8000
(p选项用于传递端口)
然而,我得到一个奇怪的错误:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (`core' generado)
如果您没有给我执行错误,请激活客户端模式(从另一个shell),它将是:
./main -p 8235 -c 10.8.7.1
老实说,我不知道它是什么错误,我是这类编程的新手
这是main
:
#include <iostream>
#include "Socket.h"
#include <string>
#include <sys/socket.h>
#include <cerrno>
#include <system_error>
#include <fstream>
#include <thread>
#include <atomic>
#include <unistd.h>
#include <list>
#include <set>
#include <stdlib.h>
#include <getopt.h>
//g++ -std=c++11 -g main.cpp socket.cpp -o main
using namespace std;
set<pair<long int, int> > clients;
set<pair<long int, int> >::iterator it1;
int port;
void thread_send(bool &quit, sockaddr_in remote_address, Socket objeto2)
{
while (quit != true)
{
string message_text = "";
getline(cin, message_text);
Message a;
message_text.copy(a.text, sizeof(a.text) - 1, 0);
a.text[message_text.size()] = '\0';
for (it1 = clientes.begin(); it1 != clientes.end(); it1++)
{
objeto2.send_to(a, remote_address, quit);
}
}
}
void thread_receive(bool &quit, Socket objeto2)
{
while (quit != true)
{
Message a;
sockaddr_in local_direction;
objeto2.receive_from(a, local_direction, quit);
port = local_direction.sin_port;
char* dir = inet_ntoa(local_direction.sin_addr);
int remote_port = ntohs(local_direction.sin_port);
a.text[254] = '\0';
cout << "the system " << /*remote_ip*/ ":" << remote_port << " send the message " << a.text << "'\n";
string stv(dir);
long int sli = stol(stv, nullptr, 10);
pair<long int, int > par(sli, port);
clients.insert(par);
}
}
int main(int argc, char* argv[])
{
string ip_dir = "";
int puerto = 8001;
bool help_option = false;
bool server_option = false;
bool client_option = false;
string port_option = "";
long int li_dec;
cout << "argc: " << argc << '\n';
int op;
while ((op = getopt(argc, argv, "hspc:01")) != -1)
{
std::cout << "opcion " << op << endl;
switch (op)
{
case '0':
case '1':
printf("opción %c\n", op);
break;
case 'h':
cout << "option h\n" << endl;
cout << "HOW TO RUN PROGRAM: ./nameofexecutable -options arguments (or no)" << endl;
help_option = 1;
break;
case 's': //server option
cout << "opción s\n" << endl;
server_option = 1;
if (ip_dir.empty()) {
ip_dir = "127.0.0.1";
}
else {
ip_dir = string(optarg);
//li_dec= stol(ip_dir,nullptr, 10);
}
//clientes.insert(puerto);
break;
case 'p':
port_option = string(optarg);
puerto = atoi(port_option.c_str());
break;
case 'c':
client_option = 1;
if (ip_dir.empty()) {
ip_dir = "127.0.0.1";
}
else {
ip_dir = string(optarg);
}
break;
case '?':
break;
default:
fprintf(stderr, "?? getopt devolvió código ""de error 0%o ??\n", op);
}
}
if (optind < argc)
{
cout << "-- arguments no opction --\n";
for (; optind < argc; ++optind)
{
cout << "argv[" << optind << "]: " << argv[optind] << '\n';
}
}
sockaddr_in local_address = make_ip_address(ip_dir, puerto);
sockaddr_in remote_address = make_ip_address(ip_dir, puerto);
try
{
Socket objeto2(local_address);
thread one(&thread_send, ref(quit), remote_address, ref(objeto2));
thread two(&thread_receive, ref(quit), ref(objeto2));
while (!quit)
{
sleep(1);
}
one.join();
two.join();
}
catch (system_error& e)
{
cerr << program_invocation_name << ":" << e.what() << '\n';
cout << "Has been an error " << endl;
}
}
我想澄清一下我有一个Socket类
此课程的主要功能是send_to()
和receive_from()
感谢任何帮助,谢谢