我有工作服务器和客户端代码。 服务器和客户端可以正确连接和聊天。 但是当我打开另一个客户端时,客户端就是说Awaiting confirmation from the server而不是别的。 虽然服务器和客户端#1仍然可以聊天。
我search了multithreading,但他们展示的例子或代码片段是先进的。 也许一个小小的解释或例子会有很大的帮助!
下面的代码正在工作。 我有一个工作服务器,但它只接受一个连接。 如何让服务器允许多个连接? 这样我可以使程序看起来像一个群聊。
client.cpp(当客户端#2连接时,代码在第40行冻结)
Linux套接字和multithreading程序
检查连接是否打开或closures?(在Linux中的C)
为什么需要IP_MULTICAST_IF和IPV6_MULTICAST_IF?
如何在Linux上的C / C + +与ipv6 udp套接字组播?
即使有更多的cmsghdr对象,CMSG_NXTHDR()也会返回NULL
#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,0); if (*buffer == '#') { send(client,0); *buffer = '*'; isExit = true; } } while (*buffer != 42); cout << "Server: "; do { recv(client,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,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,&tamano)) > 0) { strcpy(buffer,"Server connected...n"); send(server,0); cout << "Connected with the client,you are good to go..." << endl; cout << "Enter # to end the connectionn" << endl; cout << "Client: "; do { recv(server,0); cout << buffer << " "; if (*buffer == '#') { *buffer = '*'; isExit = true; } } while (*buffer != '*'); do { cout << "nServer: "; do { cin >> buffer; send(server,0); if (*buffer == '#') { send(server,0); *buffer = '*'; isExit = true; } } while (*buffer != '*'); cout << "Client: "; do { recv(server,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; }
我如何让服务器接受多个连接?
谢谢!
由于PROTOBUF服务器中的string数据typesvariables而导致段错误,并通过recv端的套接字来实现通信
在UDP中recvfrom没有得到任何东西
带有IPv6地址的Python TCP套接字失败
Linux / Unix Socket自连接
为了正确支持多个连接,您应该为每个传入连接启动一个新线程。 每个新连接都由accept()返回的唯一套接字描述符来标识。 一个简单的例子:
while ((accepted = accept(client,&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和状态机模式来做你想做的事情。 这意味着您将需要处理来自哪个客户端发送的数据。 看看这里的一个工作的例子。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。