[英]TCP Socket - Server not accepting any connections UNIX
我正在嘗試編寫一個簡單的多線程TCP server/client
應用程序(我正在學習套接字編程)。 在我的程序中,即使client
嘗試連接, server
也會永遠阻止accept()
函數。
這是我的代碼;
#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個線程serverThread
和clientThread
創建並提出並發通信。 但是server()
繼續阻塞accept() function
。 我該如何解決? 我正在使用環回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)
綁定到端口1-1023通常需要特殊特權。 嘗試使用非特權端口,例如8080。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.