我正在尝试创建一个服务器客户端程序,其中2个客户端可以相互发送消息,就像在Facebook中聊天一样。我能够达到多个客户端可以连接到服务器的程度,但似乎我找不到服务器从客户端接收任何消息的方法。
到目前为止我的代码:
服务器:
#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 <pthread.h>
#include <unistd.h>
#include <list>
#define BUFFERSIZE 1024
#define PORTNUM 5001
#define MAXPARTICIPANTS 10
using namespace std;
int sockfd = 0;
int connfd = 0;
int clients[MAXPARTICIPANTS] = {};
bool dupe;
int receiver = 0;
struct sockaddr_in server_addr;
char sendBuff[BUFFERSIZE];
char receiveBuff[BUFFERSIZE];
void *getNewParticipant(void *arg){
while (1){
dupe = false;
connfd = accept(sockfd, (struct sockaddr*)NULL, NULL);
for(int i=0; i<MAXPARTICIPANTS; i++){
if(clients[i] == connfd){
dupe = true;
break;
}
}
if(!dupe){
for(int i=0; i<MAXPARTICIPANTS; i++){
if(clients[i] == 0){
clients[i] = connfd;
cout << connfd << " joined" << endl;
break;
}
}
for(int i=0; i<MAXPARTICIPANTS; i++){
cout << clients[i] << " ";
}
cout << endl;
sprintf(sendBuff, "%d joined the conversation", connfd);
for(int i=0; i<MAXPARTICIPANTS; i++){
if(clients[i] != 0){
write(clients[i], sendBuff, strlen(sendBuff));
}
}
}
sleep(1);
}
}
void *handleMessages(void *arg){
while(1){
if((receiver = read(sockfd, receiveBuff, sizeof(receiveBuff)-1)) > 0){
receiveBuff[receiver] = 0;
if(fputs(receiveBuff, stdout) == EOF){
cout << "ERROR" << endl;
}
cout << endl;
}
/*int receiver = recv(sockfd, receiveBuff, BUFFERSIZE, 0);
//if(receiver > 0){
cout << receiver << ": " << receiveBuff << endl;
for(int i=0; i<MAXPARTICIPANTS; i++){
if(clients[i] != 0){
write(clients[i], receiveBuff, strlen(receiveBuff));
}
}
//}*/
sleep(1);
}
}
int main(void){
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
cout << "Failed to create Socket" << endl;
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORTNUM);
if(bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){
cout << "Failed to bind socket" << endl;
return -1;
}
cout << "Setup complete. Waiting for clients...." << endl;
if(listen(sockfd, MAXPARTICIPANTS) < 0){
cout << "Failed to Listen" << endl;
return -1;
}
pthread_t tid[2];
pthread_create(&tid[0], NULL, getNewParticipant, NULL);
pthread_create(&tid[1], NULL, handleMessages, NULL);
while (1) {
}
}
客户端
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFERSIZE 1024
#define PORTNUM 5001
using namespace std;
int main(){
int sockfd = 0, connfd = 0;
struct sockaddr_in server_addr;
char recvBuff[BUFFERSIZE];
char sendBuff[BUFFERSIZE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
cout << "Failed to create Socket" << endl;
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORTNUM);
if(connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr) ) <0){
cout << "Failed to connect" << endl;
}
while(1){
if((connfd = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0){
recvBuff[connfd] = 0;
if(fputs(recvBuff, stdout) == EOF){
cout << "Error" << endl;
}
cout << endl;
}
cout << "input: " << endl;
do{
cin.getline(sendBuff, BUFFERSIZE);
cout<< "input: " << sendBuff << endl;
if(strlen(sendBuff) > 0){
write(sockfd, sendBuff, strlen(sendBuff));
//send(sockfd, sendBuff, BUFFERSIZE, 0);
cout << "sent" << endl;
}
}while (*sendBuff != 42);
sleep(1);
}
}
请告诉我问题的原因是什么,以及如何解决。
谢谢。