Multi user chat server using TCP as transport layer protocol
server.c ( server program )
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
pthread_mutex_t mutex;
int clients[20];
int n=0;
void sendtoall(char *msg,int curr){
int i;
pthread_mutex_lock(&mutex);
for(i = 0; i < n; i++) {
if(clients[i] != curr) {
if(send(clients[i],msg,strlen(msg),0) < 0) {
printf("sending failure n");
continue;
}
}
}
pthread_mutex_unlock(&mutex);
}
void *recvmg(void *client_sock){
int sock = *((int *)client_sock);
char msg[500];
int len;
while((len = recv(sock,msg,500,0)) > 0) {
msg[len] = '';
sendtoall(msg,sock);
}
}
int main(){
struct sockaddr_in ServerIp;
pthread_t recvt;
int sock=0 , Client_sock=0;
ServerIp.sin_family = AF_INET;
ServerIp.sin_port = htons(1234);
ServerIp.sin_addr.s_addr = inet_addr("127.0.0.1");
sock = socket( AF_INET , SOCK_STREAM, 0 );
if( bind( sock, (struct sockaddr *)&ServerIp, sizeof(ServerIp)) == -1 )
printf("cannot bind, error!! n");
else
printf("Server Startedn");
if( listen( sock ,20 ) == -1 )
printf("listening failed n");
while(1){
if( (Client_sock = accept(sock, (struct sockaddr *)NULL,NULL)) < 0 )
printf("accept failed n");
pthread_mutex_lock(&mutex);
clients[n]= Client_sock;
n++;
// creating a thread for each client
pthread_create(&recvt,NULL,(void *)recvmg,&Client_sock);
pthread_mutex_unlock(&mutex);
}
return 0;
}
client.c (client program )
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
char msg[500];
void *recvmg(void *my_sock)
{
int sock = *((int *)my_sock);
int len;
// client thread always ready to receive message
while((len = recv(sock,msg,500,0)) > 0) {
msg[len] = '';
fputs(msg,stdout);
}
}
int main(int argc,char *argv[]){
pthread_t recvt;
int len;
int sock;
char send_msg[500];
struct sockaddr_in ServerIp;
char client_name[100];
strcpy(client_name, argv[1]);
sock = socket( AF_INET, SOCK_STREAM,0);
ServerIp.sin_port = htons(1234);
ServerIp.sin_family= AF_INET;
ServerIp.sin_addr.s_addr = inet_addr("127.0.0.1");
if( (connect( sock ,(struct sockaddr *)&ServerIp,sizeof(ServerIp))) == -1 )
printf("n connection to socket failed n");
//creating a client thread which is always waiting for a message
pthread_create(&recvt,NULL,(void *)recvmg,&sock);
//ready to read a message from console
while(fgets(msg,500,stdin) > 0) {
strcpy(send_msg,client_name);
strcat(send_msg,":");
strcat(send_msg,msg);
len = write(sock,send_msg,strlen(send_msg));
if(len < 0)
printf("n message not sent n");
}
//thread is closed
pthread_join(recvt,NULL);
close(sock);
return 0;
}
Running server.c
gcc server.c -pthread
./a.out
Running client.c
gcc client.c -pthread
./a.out client_name
server.c:35:14: error: empty character constant
35 | msg[len] = ”;
| ^~
i keep seeing this same error no matter what i try can you please help me?
LikeLike
edit the line as msg[len] = ‘\0’, means null char, the end of the string. It will work as expected.
LikeLike