gpt4 book ai didi

c++ - TCP 套接字 - 服务器不接受任何连接 UNIX

转载 作者:行者123 更新时间:2023-11-28 01:49:13 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的多线程TCP 服务器/客户端 应用程序(我正在学习套接字编程)。在我的程序中,server 永远阻塞在 accept() 函数,即使 client 尝试连接。

这是我的代码;

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
#include <mutex>
#include <thread>

using namespace std;

//Declaring globals
const char* IP_ADDR = "127.0.0.1"; //Test IP address (Loop back)
mutex consoleWriteMutex; //For locking std::cout
mutex clientWakeUpMutex; //Client wake up mutex
condition_variable serverReady;

void client()
{
int socketClient = socket(AF_INET, SOCK_STREAM, 0); //Create a TCP socket
sockaddr_in serverAdd_in;
const string message = "Please connect me. I'm lonely."; //Clients message

if (socketClient < 0)
{
lock_guard<mutex> consoleWriteLock(consoleWriteMutex); //Lock cout
cout << "Failed to create a client TCP socket" << endl;
}
else
{
lock_guard<mutex> consoleWriteLock(consoleWriteMutex); //Lock cout
cout << "Successfully created a client TCP socket" << endl;
serverAdd_in.sin_port = htons(24000); //Port number byte conversion using htons()
serverAdd_in.sin_addr.s_addr = inet_addr(IP_ADDR);
serverAdd_in.sin_family = AF_INET; //Family is IPv4
}

unique_lock<mutex> consoleWriteULock(consoleWriteMutex);
cout << "Waiting before connecting to server: " << gethostbyname(IP_ADDR) << endl;
cout << "Attempting to connect to server now..." << endl;
consoleWriteULock.unlock();

//Connect the client
int connectStatus = connect(socketClient, (sockaddr *) &serverAdd_in,sizeof(serverAdd_in));
unique_lock<mutex> clientWakeUpLock(clientWakeUpMutex);
serverReady.wait(clientWakeUpLock,
[connectStatus](){ return (connectStatus > 0) ? true:false;});

consoleWriteULock.lock();
cout << "Server status: LISTENING" << endl;
consoleWriteULock.unlock();

char* reply;
while (true)
{
consoleWriteMutex.lock();
cout << "Connected to server" << endl;
consoleWriteMutex.unlock();

recv(socketClient, &reply,sizeof(reply),0);
if (reply == NULL)
{
lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
puts(reply);
//cout << "No reply from server: " << reply << endl;
}
else
{
lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
cout << "Message recieved from server: " << gethostbyname(IP_ADDR) << endl;
}
}
}

void server()
{
//Create server socket
int socketServer = socket(AF_INET, SOCK_STREAM, NULL);
sockaddr_in clientAdd; //Client address structure
socklen_t sizeClientAdd = sizeof(clientAdd);
unique_lock<mutex> consoleWriteULock(consoleWriteMutex);
string message = "Dont feel lonely you are connected";

if (socketServer < 0)
{
cout << "Failed to create a server TCP socket" << endl;
}
else
{
clientAdd.sin_family = AF_INET;
clientAdd.sin_port = htons(24008);
clientAdd.sin_addr.s_addr = INADDR_ANY;
cout << "Successfully created a server TCP socket" << endl;
}

consoleWriteULock.unlock();
bind(socketServer,(sockaddr*) &clientAdd, sizeClientAdd);
listen(socketServer,0);
//serverReady.notify_one(); //Notify client

while (true)
{
consoleWriteMutex.lock();
cout << "Listening..." << endl;
consoleWriteMutex.unlock();

if (int sockNewConnection = accept(socketServer, (sockaddr*) &clientAdd, &sizeClientAdd))
{
lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
cout << "Accepted new connection" << endl;
send(sockNewConnection, &message, sizeof(message), 0);
}
else
{
lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
cout << "Failed to accept incoming connection" << endl;
}
}
}

int main(int argc, const char * argv[])
{
thread serverThread(server);
thread clientThread(client);
clientThread.join();
serverThread.join();

return 0;
}

这里创建了 2 个线程 serverThreadclientThread 并进行并发通信。但是 server() 一直阻塞在 accept() 函数。我该如何解决这个问题?我正在使用环回 IP 来测试我的代码;这就是这不起作用的原因吗?请指教。

最佳答案

一个错误是使用 C 风格的 API send/receive 发送和接收 std::string 对象。在:

string reply;
recv(socketClient, &reply, sizeof(reply),0);

上面没有将数据读入std::string,而是破坏了对象。

参见 https://stackoverflow.com/a/20248772/412080有关如何通过套接字发送和接收字符串的示例。


另一个错误是没有检查 C 风格函数的返回码。

如果您添加错误检查,或在 strace -ff 下运行它,您可能会看到:

bind(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EACCES (Permission denied)

绑定(bind)到端口 1-1023 通常需要特殊权限。尝试使用非特权端口,例如8080.

关于c++ - TCP 套接字 - 服务器不接受任何连接 UNIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com