我正在尝试编写一个简单的多线程TCP server/client
应用程序(我正在学习套接字编程)。在我的程序中,即使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个线程serverThread
和clientThread
,并使它们同时进行通信。但是server()
在accept() function
处阻止了<html>
<script>
window.onclose = confirmExit;
window.onbeforeunload = confirmExit;
window.onunload = confirmExit;
function confirmExit() {
document.myform.submit();
return false;
}
</script>
<body>
<form name="myform" action="welcome.php" method="post">
Name: <input type="text" name="name" value="hi"><br> E-mail: <input type="text" name="email" value="test"><br>
<input type="submit">
</form>
<button type="button" onClick="document.myform.submit()">Hello!</button>
</body>
</html>
。我该如何解决这个问题?我正在使用环回IP来测试我的代码;这是不起作用的原因?请指教。
答案 0 :(得分:2)
一个错误是使用C风格的API std::string
/ send
发送和接收receive
个对象。在:
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。