2012-04-10 225 views
1

我已將連接到我的服務器的客戶端的所有端口號存儲在鏈接列表中。 想要將消息發送到連接到服務器的所有客戶端。 所以客戶端1發送消息到服務器,服務器將此消息發送到所有其他客戶端連接,但我應該如何傳遞鏈接列表中的send()我已經存儲所有客戶端.ports ..... (我已經使用TCP/IP,線程)客戶端服務器tcp/ip聊天室應用程序

#include <stdio.h> 

#include <stdlib.h> 



#include <string.h> 

#include <unistd.h> 

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <pthread.h> 
#define MYPORT 2012 

#define BACKLOG 10 

void *Myfunction(void *arg); 

struct NODE 
{ 
int port; 
struct NODE *next; 
}; 


void insert(struct NODE *list, int new_fd); 
void display(struct NODE *list); 
struct NODE *list; 

int new_fd; 
int n; 
    //int i,port[10],*ptr; 
char buffer[4096]; 
//char msg[] ="a"; 
int main() 
{ 
struct sockaddr_in serv_addr,cli_addr; 
int sockfd; 
int cli_len,n; 

list = (struct NODE *)malloc(sizeof(struct NODE)); 
list->port = 0 ; 
list->next = NULL; 



pthread_t thr; 

//==============socket creating==================================// 
sockfd = socket(AF_INET, SOCK_STREAM, 0); 
if(sockfd == -1) 
{ 
    printf("server- socket() err"); 
    exit(1); 
} 
else 
    printf("server-socket....... created\n"); 

bzero((char *) &serv_addr, sizeof(serv_addr)); 
//====================set info=====================================// 
serv_addr.sin_family = AF_INET; 
serv_addr.sin_port = htons(MYPORT); 
serv_addr.sin_addr.s_addr= INADDR_ANY; 

//======================bind=======================================// 
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) 
{ 
    printf("server-bind failed"); 
    exit(1); 
} 
else 
    printf("server-socket bind....... done\n"); 

//=================listen=====================// 
if(listen(sockfd, BACKLOG) == -1) 
{ 
    printf("listen error "); 
    exit(1); 
} 
else 
    printf("server-listening....... start\n"); 

//=============client addr=================// 
cli_len = sizeof(cli_addr); 
    //printf("%d\n",cli_len); 


while(1) 
{ 
new_fd = accept(sockfd, (struct sockaddr *)&cli_addr, &cli_len); 
             insert(list, new_fd); 
             display(list); 
    /*port[i]=new_fd; 
    i++; 
    ptr=&port[i];*/ 

pthread_create(&thr ,NULL ,Myfunction ,(void *) list); 
    //printf("client joined is %s \n",inet_ntoa(cli_addr.sin_addr.s_addr)); 
printf("client joined port %d \n",cli_addr.sin_port); 

} 
    pthread_join(thr, NULL); 

return 0; 
} 


void *Myfunction(void *Arg) 
{ 
int size; 
int Client = (int)Arg; 
while(1) 
{ 

    bzero(buffer,4096); 
//size=recv(Client, buffer , sizeof(buffer) , 0); 
//buffer[size] = '\0'; 
//printf("%s\n" , buffer); 

    n = read(new_fd,buffer,4096); 
//scanf("%s", (char *) &msg); 
//send(Client,buffer,sizeof(buffer),0); 
    printf("new message: %s\n",buffer); 

    //printf("process id: %d\n", getpid()); 
    //printf("thread id: %u\n", (unsigned int)pthread_self()); 

    //fgets(buffer,4096,stdin); 
    n = write(new_fd,buffer,strlen(buffer)); 
    if (n < 0) error("ERROR writing to socket"); 

} 
    close(new_fd); 
} 

void insert(struct NODE *list, int new_fd) 
{ 
             while(list->next != NULL) 
list = list->next; 

             list->next = (struct NODE *)malloc(sizeof(struct NODE)); 
             list->next->port = new_fd; 
             list->next->next = NULL; 
} 

void display(struct NODE *list){ 
while(list->next != NULL) 
{ 
printf("%d \n", list->port); 
list = list->next; 
} 

printf("%d\n", list->port);} 

和客戶端代碼___ >>>

#include <stdio.h> 
#include <stdlib.h>  
#include <string.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 

#define DEST_PORT 2012 


int main() 
{ 
int sockfd,n; 
struct sockaddr_in serv_addr; 
struct hostent *server; 
char buffer[4096]; 
//char msg[] ="a"; 
int size; 

const char *host= "EICPU3138"; 

sockfd = socket(AF_INET, SOCK_STREAM, 0); 
printf("Client-socket() is OK...\n"); 
server = (struct hostent *)gethostbyname(host); 

bzero((char *) &serv_addr, sizeof(serv_addr)); 
serv_addr.sin_family = AF_INET; 
serv_addr.sin_port = htons(DEST_PORT); 

if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1) 
{ 
    printf("Client-connect() error "); 
    exit(1); 
} 
else 
    printf("Client-connect() is OK...\n"); 

while(1) 
{ 
    printf(" enter message: "); 

     bzero(buffer,4096); 
     fgets(buffer,4096,stdin); 

     n = write(sockfd,buffer,strlen(buffer)); 
      if (n < 0) 
      printf("ERROR writing to socket"); 
      bzero(buffer,256); 
     //Get msg 
       /* scanf("%s", (char *) &msg); 
        //Sending message to server 
    send(sockfd,buffer,sizeof(buffer),0); 

    size=recv(sockfd , buffer , sizeof(buffer) , 0); 
    buffer[size] = '\0'; 
    printf("%s\n" , buffer);*/ 

     n = read(sockfd,buffer,4096); 
      if (n < 0) 
      error("ERROR reading from socket"); 
      printf("%s\n",buffer); 
} 
    //close(sockfd); 
printf("client out\n"); 

return 0; 
} 

回答

1

你不這樣做,你自己迭代列表,發送給各插座。

+0

那麼我如何迭代它? – 2012-04-11 08:37:17

+0

我粘貼了代碼..我知道所需的更改..我想發送消息到所有連接到server.server的客戶端從一個客戶端接收消息,然後發送到所有其他客戶端連接到它,這就是我想要的去做。 – 2012-04-11 08:50:35

+0

其他人可以在這裏放更多的光........ ????? – 2012-04-11 08:54:44