server.c
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h> #define BUF_SIZE 100
#define MAX_CLNT 256 void *handle_clnt(void* arg);
void send_msg(char *msg,int len);
void error_handling(char* message); int clnt_cnt = ;
int clnt_socks[MAX_CLNT];
pthread_mutex_t mutx; int main(int argc,char* argv[])
{
int serv_sock,clnt_sock;
struct sockaddr_in serv_addr,clnt_addr;
int adr_sz;
pthread_t t_id; if(argc != )
{
printf("usage: %s <port>\n",argv[]);
exit();
} pthread_mutex_init(&mutx,NULL);
serv_sock = socket(PF_INET,SOCK_STREAM,);
memset(&serv_addr,,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[])); if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -)
error_handling("bind error");
if(listen(serv_sock,) == -)
error_handling("listen error"); while()
{
adr_sz = sizeof(clnt_addr);
clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&adr_sz); pthread_mutex_lock(&mutx);
clnt_socks[clnt_cnt++] = clnt_sock;
pthread_mutex_unlock(&mutx); pthread_create(&t_id,NULL,handle_clnt,(void*)&clnt_sock);
pthread_detach(t_id);
printf("connected client ip:%s \n",inet_ntoa(clnt_addr.sin_addr));
}
close(serv_sock);
return ;
} void* handle_clnt(void* arg)
{
int clnt_sock = *((int*)arg);
int str_len = ,i;
char msg[BUF_SIZE]; while((str_len = read(clnt_sock,msg,sizeof(msg))) != )
send_msg(msg,str_len);
pthread_mutex_lock(&mutx);
for(i = ;i < clnt_cnt;i++)
{
if(clnt_sock == clnt_socks[i])
{
while(i++ < clnt_cnt-)
clnt_socks[i] = clnt_socks[i+];
break;
}
}
clnt_cnt--;
pthread_mutex_unlock(&mutx);
close(clnt_sock);
return NULL;
} void send_msg(char* msg,int len)
{
int i;
pthread_mutex_lock(&mutx);
for(i = ;i < clnt_cnt;i++)
write(clnt_socks[i],msg,len);
pthread_mutex_unlock(&mutx);
}
void error_handling(char* message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit();
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/socket.h> #define BUF_SIZE 200
#define NAME_SIZE 20 void *send_msg(void* arg);
void *recv_msg(void* arg);
void error_handling(char* message); char name[NAME_SIZE] = "[DEFAULT]";
char msg[BUF_SIZE];
int main(int argc,char* argv[])
{
int sock;
struct sockaddr_in serv_addr;
pthread_t send_thread,recv_thread;
void * thread_return; if(argc != )
{
printf("usage: %s <ip> <port> <name>\n");
exit();
} sprintf(name,"[%s]",argv[]);
sock = socket(PF_INET,SOCK_STREAM,); memset(&serv_addr,,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[]);
serv_addr.sin_port = htons(atoi(argv[])); if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -)
error_handling("connect error"); pthread_create(&send_thread,NULL,send_msg,(void*)&sock);
pthread_create(&recv_thread,NULL,recv_msg,(void*)&sock);
pthread_join(send_thread,&thread_return);
pthread_join(recv_thread,&thread_return);
close(sock);
return ;
} void* send_msg(void* arg)
{
int sock = *((int*)arg);
char name_msg[NAME_SIZE+BUF_SIZE];
while()
{
fgets(msg,BUF_SIZE,stdin);
if(!strcmp(msg,"q\n") || !strcmp(msg,"Q\n"))
{
close(sock);
exit();
}
sprintf(name_msg,"%s %s",name,msg);
write(sock,name_msg,strlen(name_msg));
}
return NULL;
} void* recv_msg(void* arg)
{
int sock = *((int*)arg);
char name_msg[BUF_SIZE+NAME_SIZE];
int str_len;
while()
{
str_len = read(sock,name_msg,NAME_SIZE+BUF_SIZE-);
if(str_len == -)
return (void*)-;
name_msg[str_len] = ;
fputs(name_msg,stdout);
}
return NULL;
} void error_handling(char* message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit();
}