-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
120 lines (108 loc) · 2.95 KB
/
Copy pathserver.c
File metadata and controls
120 lines (108 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "pthread.h"
int nclient=3;
char sendBuff[1024];
int cclients=0;
int clients[10];
int conn[10];
pthread_mutex_t mut1=PTHREAD_MUTEX_INITIALIZER;
/*void * fsend(void *arg){
int *connfd=(int *)arg;
char s[1024];
while(1){
//printf("Enter a string\n");
gets(s);
sprintf(sendBuff,"%s",s);
send(*connfd,sendBuff,sizeof(sendBuff),0);
}
close(*connfd);
exit(0);
}*/
void * func(void *arg){
pthread_t sid;
pthread_detach(pthread_self());
int connfd=cclients-1;
printf("Receiving from client %d...\n",cclients);
//pthread_create(&sid, NULL, &fsend,(void *)connfd);
while(1){
//pthread_mutex_lock(&mut1);
char recvBuff[1024];
int p=recv(clients[connfd], recvBuff, sizeof(recvBuff),0);
if(p==0){
printf("Client disconnected\n");
break;}
char s[1024];
sprintf(s,"%s",recvBuff);
printf("Message received to %d: %s",connfd,s);
int d=recvBuff[0]-48;
if(conn[d]){
printf("Sending message to %d\n",d);
send(clients[d],s, sizeof(s),0);
}
else{
sprintf(sendBuff,"This client is not connected anymore\n");
send(clients[connfd],sendBuff,sizeof(sendBuff),0);
}
//pthread_mutex_unlock(&mut1);
}
close(clients[connfd]);
conn[connfd]=0;
printf("Client %d left. Clossed connections.\n",connfd);
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
int sockfd,connfd;
if(argc<2){
printf("Enter the port to connect\n");
exit(1);
}
int port=atoi(argv[1]);
struct sockaddr_in serv_addr,cli_addr;
int numrv;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd==-1){
printf("Socket creation failed\n");
exit(1);
}
else
printf("Socket creation success\n");
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '\0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
for(int i=0;i<10;i++)
conn[i]=0;
if(bind(sockfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr))==-1){
printf("Naming failed\n");
exit(1);
}
else
printf("Socket named\n");
if(listen(sockfd, nclient) == -1){
printf("Failed to listen\n");
exit(1);
}
printf("Listening...\n");
while(1){
connfd = accept(sockfd, (struct sockaddr*)NULL ,0);
clients[cclients]=connfd;
conn[cclients]=1;
cclients++;
sprintf(sendBuff,"Current number of clients: %d\n",cclients-1);
send(connfd,sendBuff,sizeof(sendBuff),0);
pthread_t tid,ptid;
pthread_create(&ptid, NULL, &func,(void *)&connfd);
}
pthread_exit(NULL);
return 0;
}