2013-12-14 106 views
0

问题: 我需要一些帮助,在我的代码中有错误。聊天客户端工作时,我只有一个客户端运行,但如果我使用更多的客户端。只有最后一个客户端消息才会显示在我的服务器上。我的client.c似乎工作,因为它发送,但由于某种原因recv()没有得到以前的客户端发送()。c中的聊天客户端

代码如何工作: 我建立了我的服务器,并在新客户端连接时产生一个新线程。该线程将处理我从客户端获得的消息并将其打印在服务器屏幕上。

代码:

CLIENT.C

#define _GNU_SOURCE 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <errno.h> 
#include <netdb.h> 
#include <unistd.h> 
#include <signal.h> 
#include <assert.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 

int main(int argc, char ** argv){ 

//get port 
//int port = atoi(argv[1]); 
int server_port = atoi(argv[1]); 
char * name =argv[2]; 
int namelength = strlen(name); 



//set up server adress and socket 
int sock = socket(AF_INET,SOCK_STREAM,0); 
struct sockaddr_in server; 
memset(&server, 0, sizeof(server)); 
server.sin_family = AF_INET; 
server.sin_addr.s_addr = inet_addr("127.0.0.1"); 
server.sin_port = htons(server_port); 


//connect 
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { 
    perror("connect failed"); 
    exit(1); 
                    } 
//set up client name                  

char * buff = malloc(5000*sizeof(char)); 
//get the chatting 
//char * other_message = malloc(5000*sizeof(char)); 
while(1){ 
    printf("ENTER MESSAGE:\n"); 
    char message[5000]; 
    strcpy(message, name); 
    strcat(message,": "); 
    printf("%s", message); 

    scanf("%[^\n]",buff); 
    getchar(); 
    strcat(message,buff); 

    int sent = send(sock , message , strlen(message) , MSG_DONTWAIT); 
    if (sent == -1) 
     perror("Send error: "); 
    else 
     printf("Sent bytes: %d\n", sent); 

    } 

return 0;   

}

SERVER.C

#define _GNU_SOURCE 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <errno.h> 
#include <pthread.h> 
#include <netdb.h> 
#include <unistd.h> 
#include <signal.h> 
#include <assert.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 

pthread_t * threads = NULL; 
int * client_fd = NULL; 
int num_clients; 
int thread_num; 
void * client_handler(void * cl) 
{ 

int * client = (int *)cl; 
char * message = malloc(5000*sizeof(char)); 
printf("Connected: %d\n",*client); 
int byte=1; 
//recieve the message from clients 
while(1) 
{ 
    byte=recv(*client, message , 5000 , 0); 
    if(byte< 0) 
     break; 

    //send message to all other clients 

    printf("%s\n",message); 
    printf("Recieved bytes:%d\n",byte); 
    memset(message, 0, 5000); 


    /*for(i=0;i<num_clients;i++) 
     if(client_fd[i]!=*client) 
      send(*client , message , strlen(message),0);*/ 
} 
printf("finished: %d\n",*client); 
return NULL; 
} 


int main(int argc, char ** argv) 
{ 

//get the port 
int port = atoi(argv[1]); 

//set up socket 
int socket_fd = socket(AF_INET,SOCK_STREAM,0); 
struct sockaddr_in server,client; 
memset(&server, 0, sizeof(server)); 
server.sin_family = AF_INET; 
server.sin_addr.s_addr = INADDR_ANY; 
server.sin_port = htons(port); 

//bind 
if(bind(socket_fd, (struct sockaddr*)&server,sizeof(server)) < 0){ 
    perror("binding error\n"); 
    exit(1); 
                    } 
//listen                  
if(listen(socket_fd, 10) <0){ 
    perror("binding error\n"); 
    exit(1); 
           } 
//accept incoming connectionns 

threads = malloc(10*sizeof(pthread_t)); 
client_fd = malloc(10*sizeof(int)); 
int i=0; 
int c = sizeof(struct sockaddr_in); 
while(1) 
{ 

    int c_fd = accept(socket_fd,(struct sockaddr *)&client, (socklen_t*)&c); 
    if(c_fd < 0) 
     printf("error"); 
    client_fd[i]=c_fd; 
    pthread_create(&threads[i],NULL,client_handler,(void *)(&c_fd)); 
    i++; 
    num_clients=i; 
}  
return 0; 
} 
+6

您是否听说过调试器? –

+0

你是什么意思? – user3103072

+0

@ user3103072 - 哦,经典回复。您正在编写网络应用程序,但没有调试工具的概念? –

回答

2
  • 发送C风格的字符串与strlen的()。不发送终止空。使用strlen()+ 1

  • 忽略recv()返回的值。 TCP是一种只能传输字节/八位字节的流媒体协议。它不会传递任何更复杂的内容。 recv()可能会返回聊天行的一个字节,所有聊天行或其中任何内容。要传输比一个字节更复杂的消息,你需要一个协议,你必须处理它。你的是'聊天行是空终止的字符串',所以你需要在循环中调用recv(),并使用返回的值连接接收的字节,直到空到达。

  • 尝试使用“%s”打印非字符串。在确定已收到空值之前,您不能尝试打印接收到的数据。

+0

我删除了所有的侮辱。如果您愿意,您可以恢复更改。 –

+0

@eznme - 不,我没有打扰:) –