如何在不关闭的情况下发送套接字? [英] How do I get the socket to send without close?
问题描述
嘿伙计们,
我对c ++很陌生,但我正在尝试编写一个双向套接字连接(一个用于发送充当客户端和一个接收充当服务器)。
接收部分工作正常,但发送部分有问题。
如果我关闭套接字,只想发送的消息会发送,并且有问题,我无法关闭套接字,因为服务器(在这种情况下是其他程序)期望套接字保持打开状态。
有没有办法在不关闭发送套接字的情况下发送消息?
Hey guys,
I am pretty new to c++, but I am trying to program a two way socket connection (one for sending acts as a client and one for receiving acts as a server).
The receive part works perfectly, but I have a problem with the sending part.
The message want to send only gets send if I close the socket, and there is the problem, I can't close the socket because the server ( in this case an other program) expects the socket to stay open.
Is there any way I can send the message without closing the sending socket?
if (function == "connect" ){
status_s = getaddrinfo(&host_const[0], &port_const[0], &host_info_s, &host_info_list_s);
if (status_s != 0) std::cout << "getaddrinfo error" << gai_strerror(status_s) ;
std::cout << "Creating a sending socket..." << std::endl;
socketfd_s = socket(host_info_list->ai_family, host_info_list->ai_socktype,
host_info_list->ai_protocol);
if (socketfd_s == -1) std::cout << "socket error " ;
std::cout << "Host: " << &host_const[0] << std::endl;
std::cout << "Port: " << &port_const[0] << std::endl;
std::cout << "function: " << function << std::endl;
function.erase(std::remove(function.begin(), function.end(), '\n'), function.end());
std::cout << "Connect()ing..." << std::endl;
status_s = connect(socketfd_s, host_info_list_s->ai_addr, host_info_list_s->ai_addrlen);
if (status_s == -1) std::cout << "connect error" ;
cout << "status: " << status_s;
cout << "socket connector: " << socketfd_s ;
std::cout << "client connected....." << std::endl;
}
else {//if (function != "connect" )
std::cout << "send()ing message on anwser..." << std::endl;
char *msg_s = "some_string";
int len_s;
ssize_t bytes_sent_s;
std::cout << msg_s;
len_s = strlen(msg_s);
std::cout << len_s << std::endl;
std::cout << "Message not sent...." << std::endl;
//send(socketfd_s, msg_s, len_s, 0);
bytes_sent_s = sendto(socketfd_s, msg_s, len_s, 0, host_info_list_s->ai_addr, host_info_list_s->ai_addrlen);
freeaddrinfo(host_info_list);
//close(socketfd_s);
//bytes_sent_s = send(socketfd_s, msg_s, len_s, 0);
//send(socketfd_s, msg_s, len_s, 0);
std::cout << "Message sent....\n" << std::endl;
std::cin.ignore();
cout << "param1" << socketfd_s;
cout << "param2" << len_s;
cout << "param3" << msg_s;
}
连接到套接字工作正常,只是其他部分就是问题,你可以看到我已经尝试过发送和发送,但结果相同。
啊和整个thing在while(1)循环中运行以读取多条消息。
谢谢
The connect to the socket works perfectly, just the else part is the problem, as you can see I already tried send and sendto, but had the same result.
Ah and the whole thing runs in a while (1) loop to read more than one message.
Thank you
推荐答案
您应该使用发送
来发送消息,而不是sendto
,这是针对无连接套接字的。请参见 http://linux.die.net/man/2/sendto [ ^ ]。
You should be usingsend
to send your messages, rather thansendto
, which is for connectionless sockets. See http://linux.die.net/man/2/sendto[^].
我建议你试试您可以在此页面中找到的代码示例:套接字教程 [ ^ ]。
I suggest you to try the code sample you may find in this page: "Sockets Tutorial"[^].
这篇关于如何在不关闭的情况下发送套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!