1
+ // Code For File Transfer between client and server
2
+
3
+ // Libraries Included
4
+
5
+ #include <unistd.h>
6
+ #include <stdio.h>
7
+ #include <sys/socket.h>
8
+ #include <stdlib.h>
9
+ #include <netinet/in.h>
10
+ #include <string.h>
11
+ #include <pthread.h>
12
+ #include <dirent.h>
13
+
14
+ #define Max_Client 30
15
+ #define Max_Size 1000
16
+
17
+ struct client_details {
18
+ int sockno ;
19
+ char ip [INET_ADDRSTRLEN ];
20
+ };
21
+
22
+ struct sockaddr_in sockinfo ;
23
+ struct dirent * myfile ;
24
+ pthread_attr_t pthread_attr ;
25
+ int client [Max_Client ];
26
+ struct client_details cd [Max_Client ];
27
+ DIR * mydir ;
28
+ int opt = 1 ;
29
+ int PORT ;
30
+ pthread_mutex_t lock ;
31
+ char shared_buffer [Max_Size ];
32
+ pthread_t new_client ;
33
+
34
+ int enable_listen (int * server_fd ,int PORT ){
35
+
36
+ if ((* server_fd = socket (AF_INET ,SOCK_STREAM ,0 ))< 0 ){
37
+ perror ("Socket()" );
38
+ return -1 ;
39
+ }
40
+
41
+ sockinfo .sin_family = AF_INET ;
42
+ sockinfo .sin_port = htons (PORT );
43
+ sockinfo .sin_addr .s_addr = INADDR_ANY ;
44
+ memset (& (sockinfo .sin_zero ),'\0' ,8 );
45
+
46
+ if (setsockopt (* server_fd , SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,& opt , sizeof (opt ))< 0 )
47
+ {
48
+ perror ("setsockopt failed" );
49
+ return -1 ;
50
+ }
51
+
52
+ if (bind (* server_fd ,(struct sockaddr * )& sockinfo ,sizeof (sockinfo ))< 0 ){
53
+ perror ("Bind()" );
54
+ return -1 ;
55
+ }
56
+
57
+ if (listen (* server_fd ,30 )< 0 ){
58
+ perror ("Listen()" );
59
+ return -1 ;
60
+ }
61
+
62
+ printf ("Accepting connections on port %d.\n" , (int )PORT );
63
+ return 0 ;
64
+ }
65
+
66
+ // void *send_to_all(int *client_fd){
67
+
68
+ // while(1){
69
+ // char buffer[Max_Size];
70
+ // printf("Message to clients");
71
+ // scanf("%s\n",buffer);
72
+
73
+ // int size_data = send(*client_fd,buffer,sizeof(buffer),0);
74
+ // if(size_data<0){
75
+ // perror("Sendind failure");
76
+ // exit(1);
77
+ // }
78
+ // }
79
+
80
+ // }
81
+
82
+ // void *recieve(int *client_fd){
83
+
84
+ // char buffer[Max_Size];
85
+
86
+ // int i;
87
+ // for(int i = 0;i<Max_Client;i++){
88
+ // if(client[i]==(*client_fd)){
89
+ // break;
90
+ // }
91
+ // }
92
+ // int size_data;
93
+ // while((size_data=recv(*client_fd,buffer,sizeof(buffer),0)) > 0){
94
+
95
+ // buffer[size_data]='\0';
96
+
97
+ // printf("Message recieved from %s:%d",cd[i].ip,cd[i].sockno);
98
+ // printf("%s\n",buffer);
99
+
100
+ // }
101
+ // // if(size_data==-1){
102
+ // // perror("Receiving");
103
+ // // exit(1);
104
+ // // }
105
+
106
+ // if(size_data ==0){
107
+ // client[i]=-1;
108
+ // printf("Host disconnected , ip %s , port %d \n" , cd[i].ip , cd[i].sockno);
109
+ // pthread_kill(*client_fd,0);
110
+ // pthread_exit(NULL);
111
+ // close(*client_fd);
112
+ // }
113
+ // }
114
+
115
+ void * handle_message (void * cli_fd ){
116
+ // Send to all , Send to perticular Client, Receive for Client
117
+ int client_fd = * (int * )cli_fd ;
118
+ int n ,i ;
119
+ pthread_mutex_lock (& lock );
120
+ for (i = 0 ;i < Max_Client ;i ++ ){
121
+ if (client [i ]== (client_fd )){
122
+ break ;
123
+ }
124
+ }
125
+ pthread_mutex_unlock (& lock );
126
+
127
+ char buffer [Max_Size ], message [Max_Size ], command [Max_Size ], msg_token ;
128
+
129
+ bzero (buffer , Max_Size );
130
+ sprintf (buffer , "Welcome to the Chat Server" );
131
+ n = write (client_fd , buffer , strlen (buffer ));
132
+ if (n < 0 ) return NULL ;
133
+ printf ("client %d has joined\n" , i );
134
+
135
+ pthread_mutex_lock (& lock );
136
+ bzero (shared_buffer , Max_Size );
137
+ sprintf (shared_buffer , "Client %d has joined\n" , i );
138
+ pthread_mutex_unlock (& lock );
139
+
140
+ while (1 ){
141
+ bzero (buffer ,Max_Size );
142
+ int recv_data = recv (client_fd ,buffer ,Max_Size - 1 ,MSG_DONTWAIT ); /* NON-BLOCKING READ */
143
+ if (recv_data < 0 ){
144
+ //printf("Write Message to client");
145
+ n = write (client_fd ,shared_buffer ,strlen (shared_buffer ));
146
+ if (n < 0 ){
147
+ printf ("Client %d has disconnected\n" , i );
148
+ pthread_mutex_lock (& lock );
149
+ bzero (shared_buffer , Max_Size );
150
+ sprintf (shared_buffer , "Client %d has disconnected" , i );
151
+ pthread_mutex_unlock (& lock );
152
+ break ;
153
+ }
154
+ }
155
+ else {
156
+ printf ("Client %d: %s" ,i ,buffer );
157
+
158
+ pthread_mutex_lock (& lock );
159
+ bzero (shared_buffer , Max_Size );
160
+ sprintf (shared_buffer , "Client %d: %s" , i , buffer );
161
+ pthread_mutex_unlock (& lock );
162
+ }
163
+ sleep (2 );
164
+ }
165
+ printf ("Client %d has exited\n" , i );
166
+ pthread_mutex_lock (& lock );
167
+ client [i ]= -1 ;
168
+ pthread_mutex_unlock (& lock );
169
+ close (client_fd );
170
+ return NULL ;
171
+ }
172
+
173
+ void handle_new_connection (int * server_fd ){
174
+
175
+ // Client details such as ip and port
176
+ struct sockaddr_in client_info ;
177
+ memset (& client_info ,0 ,sizeof (client_info ));
178
+ socklen_t client_len = sizeof (client_info );
179
+ int new_client_fd = accept (* server_fd ,(struct sockaddr * )& client_info ,& client_len );
180
+ if (new_client_fd < 0 ){
181
+ perror ("accept()" );
182
+ exit (1 );
183
+ }
184
+ char client_ipv4 [INET_ADDRSTRLEN ];
185
+ // convert binary to test form
186
+ inet_ntop (AF_INET ,& client_info .sin_addr , client_ipv4 , INET_ADDRSTRLEN );
187
+
188
+ printf ("Incoming connection from %s:%d.\n" , client_ipv4 , client_info .sin_port );
189
+
190
+ int i ;
191
+ for (i = 0 ;i < Max_Client ;i ++ ){
192
+ if (client [i ]== -1 ){
193
+ client [i ]= new_client_fd ;
194
+ cd [i ].sockno = client_info .sin_port ;
195
+ strcpy (cd [i ].ip ,client_ipv4 );
196
+ break ;
197
+ }
198
+ }
199
+
200
+
201
+ if (pthread_create (& new_client ,NULL ,(void * )& handle_message , (void * )& new_client_fd )!= 0 ){
202
+ perror ("pthread_create()" );
203
+ exit (1 );
204
+ }
205
+ }
206
+
207
+ int main (int argc , char * argv []){
208
+
209
+ int server_fd , acc_fd ;
210
+
211
+ int i ;
212
+ for (i = 0 ;i < Max_Client ;i ++ ){
213
+ client [i ]= -1 ;
214
+ }
215
+ memset (& cd ,0 ,sizeof (cd ));
216
+
217
+ PORT = argc > 1 ? atoi (argv [1 ]) : 0 ;
218
+ if (!PORT ){
219
+ printf ("Enter Port :" );
220
+ scanf ("%d" ,& PORT );
221
+ }
222
+
223
+ if (enable_listen (& server_fd ,PORT )< 0 ){
224
+ perror ("Listen()" );
225
+ exit (EXIT_FAILURE );
226
+ }
227
+
228
+ printf ("Waiting for incoming connections.\n" );
229
+
230
+ // initialize pthread attribute to create detached threads
231
+
232
+ if (pthread_attr_init (& pthread_attr ) != 0 ){
233
+ perror ("Pthread_attr_init()" );
234
+ exit (1 );
235
+ }
236
+
237
+ if (pthread_attr_setdetachstate (& pthread_attr ,PTHREAD_CREATE_DETACHED )!= 0 ){
238
+ perror ("pthread_attr_setdetachstate()" );
239
+ exit (1 );
240
+ }
241
+
242
+ while (1 ){
243
+
244
+ handle_new_connection (& server_fd );
245
+
246
+ }
247
+ pthread_join (new_client , NULL );
248
+ pthread_mutex_destroy (& lock );
249
+ close (server_fd );
250
+ return 0 ;
251
+
252
+ }
0 commit comments