C++套接字 - 服务器不接受多个客户端 (linux)
C++ Sockets - Server doesn't accept multiple clients (linux)
我有工作的服务器和客户端代码。服务器和客户端可以正常连接和聊天。但是当我打开另一个客户端终端时,客户端显示的是Awaiting confirmation from the server
,没有其他内容。虽然服务器和客户端#1仍然可以聊天。
我搜索了多线程,但他们显示的例子或代码片段是先进的。也许一点解释或一个例子会有很大帮助!
下面的代码正在工作。我有一个工作的服务器,但它只接受一个连接。如何使服务器允许多个连接?这样我可以使程序看起来像一个群聊。
client.cpp(当客户端#2连接时,代码在第40行冻结)
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main()
{
char a;
int client;
int portNum = 1500;
int bufsize = 1024;
char* buffer = new char[bufsize];
bool isExit = false;
char* ip = "127.0.0.1";
struct sockaddr_in direc;
if ((client = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "nError creating socket..." << endl;
exit(0);
}
cout << "nSocket created successfully..." << endl;
direc.sin_family = AF_INET;
direc.sin_port = htons(portNum);
inet_pton(AF_INET, ip, &direc.sin_addr);
if (connect(client,(struct sockaddr *)&direc, sizeof(direc)) == 0)
cout << "Connection to the server " << inet_ntoa(direc.sin_addr) << endl;
cout << "Awaiting confirmation from the server..." << endl; //line 40
recv(client, buffer, bufsize, 0);
cout << "n=> Enter # to terminate the connectionn" << endl;
do {
cout << "Client: ";
do {
cin >> buffer;
send(client, buffer, bufsize, 0);
if (*buffer == '#') {
send(client, buffer, bufsize, 0);
*buffer = '*';
isExit = true;
}
} while (*buffer != 42);
cout << "Server: ";
do {
recv(client, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '*';
isExit = true;
}
} while (*buffer != 42);
cout << endl;
} while (!isExit);
cout << "=> Connection terminated.nGoodbye";
close(client);
return 0;
}
server.cpp
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main()
{
int client, server;
int bufsize = 1024;
int portNum = 1500;
bool isExit = false;
char* buffer = new char[bufsize];
struct sockaddr_in direc;
socklen_t tamano;
pid_t pid;
if ((client = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "nError establishing socket..." << endl;
exit(1);
}
cout << "nSocket server has been created..." << endl;
direc.sin_family = AF_INET;
direc.sin_addr.s_addr = htons(INADDR_ANY);
direc.sin_port = htons(portNum);
if ((bind(client, (struct sockaddr*)&direc,sizeof(direc))) < 0) {
cout << "nError binding connection..." << endl;
return -1;
}
tamano = sizeof(direc);
cout << "Looking for clients..." << endl;
listen(client, 1);
while ((server = accept(client,(struct sockaddr *)&direc,&tamano)) > 0) {
strcpy(buffer, "Server connected...n");
send(server, buffer, bufsize, 0);
cout << "Connected with the client, you are good to go..." << endl;
cout << "Enter # to end the connectionn" << endl;
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '*';
isExit = true;
}
} while (*buffer != '*');
do {
cout << "nServer: ";
do {
cin >> buffer;
send(server, buffer, bufsize, 0);
if (*buffer == '#') {
send(server, buffer, bufsize, 0);
*buffer = '*';
isExit = true;
}
} while (*buffer != '*');
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer == '*';
isExit = true;
}
} while (*buffer != '*');
} while (!isExit);
cout << "n=> Connection terminated... " << inet_ntoa(direc.sin_addr);
close(server);
cout << "nGoodbye..." << endl;
isExit = false;
}
close(client);
return 0;
}
如何让服务器接受多个连接?
谢谢!
为了正确支持多个连接,您应该为每个传入的连接启动一个新线程。每个新连接由accept()
返回的唯一套接字描述符标识。一个简单的例子:
while ((accepted = accept(client,(struct sockaddr *)&direc,&tamano)) > 0) {
/*Create the thread and pass the socket descriptor*/
if( pthread_create(new_thread, &thread_attributes, &handle_tcp_connection, (void *)accepted) != 0){
perror("create thread");
exit(EXIT_FAILURE);
}
}
您将需要使用select
或poll
和状态机模式来执行您想要执行的操作。这意味着您需要在数据从发送数据的客户端传入时对其进行处理。请看这里的工作示例:
相关文章:
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- C/C++ Linux 上的多线程服务器/客户端崩溃
- 无法在 matlab 中的 UDP 客户端和 Linux 中的服务器之间发送数据
- Linux服务器和Windows客户端上的C 源代码
- 在C linux中服务器和客户端之间的共享内存
- C++websockets服务器/客户端跨平台linux/windows
- 使用Qt在Linux上实现了一个C++HTTPS SOAP客户端
- 将TCP客户端(在Linux / C ++上)连接到TCP服务器(在Windows C#上)
- 是否可以为同时使用Windows和Linux的客户端使用相同的DLL
- 在linux中使用命名管道的简单客户端/服务器程序
- 基于linux的HTML web客户端的QT REST开发
- 当客户端连接越来越少的时候(在linux上使用套接字编程),服务器监听的明智方法是什么?
- Linux套接字作为多播服务器和客户端
- Linux服务器,发送文件到windows客户端(Socket)
- 如何在 Linux 中通过 C++ 中的套接字从客户端读取输入
- 使用c++ TCP客户端套接字在特定的网络接口Linux/Unix
- rpc server c++ Linux服务器和Windows客户端
- 插入不工作,如果没有在phpmyadmin / linux mysql客户端
- C++套接字 - 服务器不接受多个客户端 (linux)
- c++ Sockets -客户端给出分段错误(linux)