客户端服务器 tcp/ip 聊天室应用程序

标签 c multithreading sockets client chatroom

我已经将连接到我的服务器的客户端的所有端口号存储在链表中。 想要向连接到服务器的所有客户端发送消息。 所以 client1 向服务器发送消息,服务器将此消息发送给所有其他连接的客户端,但是我应该如何在 send() 中传递链接列表,我存储了所有客户端.ports..... (我用过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;
}

最佳答案

你不需要,你自己迭代列表并发送到每个套接字。

关于客户端服务器 tcp/ip 聊天室应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087432/

相关文章:

c++ - `--defsym` 链接器标志如何将值传递给源代码?

java - Java中同一对象的不同实例上的多线程

django - 守护进程 Django 管理命令

c++ - 无法使用原始套接字打印 TCP 响应

java - 这个多线程同步代码有什么问题?

使用 UDP 套接字的 JavaScript 应用程序 - NodeJS 和 SocketIO

c - 将参数传递给 C 中的函数

c - 15幻灯片益智游戏: Segmentation fault (core dumped) error

将整数复制到 C 中的 char 缓冲区

multithreading - 每个 Hadoop map task 使用多少个核心?