I have stored all port numbers of client connected to my server in linked list. Want to send message to all clients connected to server. so client1 send message to server ,server sends this message to all other clients connected,but how should i pass the linked list in send() where i have stored all clients .ports..... (i have used TCP/IP, threading)
#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);}
and the client code___>>>
#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;
}
You don't, you iterate the list yourself and send to each socket.