Skip to content

Commit f0804bb

Browse files
committed
First Commit
0 parents  commit f0804bb

File tree

4 files changed

+374
-0
lines changed

4 files changed

+374
-0
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
INCLUDE_FILES = message.h
3+
BUILD_FILES = server.c client.c
4+
TARGET_FILES = server client
5+
6+
CC = gcc -Wall -O0
7+
8+
all: $(BUILD_FILES)
9+
$(CC) server.c -o server -lpthread
10+
$(CC) client.c -o client -lpthread
11+
12+
clean:
13+
rm -f $(TARGET_FILES)

client.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Code For File Transfer between client and server
2+
3+
// Libraries Included
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <unistd.h>
9+
#include <sys/types.h>
10+
#include <sys/socket.h>
11+
#include <netinet/in.h>
12+
#include <netdb.h>
13+
#include <ctype.h>
14+
#include <pthread.h>
15+
16+
#define PORT 8080
17+
#define Max_Size 1000
18+
19+
20+
// void *send_data(int *sock_fd){
21+
22+
// char buffer[Max_Size];
23+
// while(1){
24+
// bzero(buffer,Max_Size);
25+
26+
// //printf("Write message to server");
27+
// scanf("%s",buffer);
28+
29+
// int send_data = send(*sock_fd,buffer,sizeof(buffer),0);
30+
// if(send_data<0){
31+
// perror("Sending Failure");
32+
// exit(1);
33+
// }
34+
// }
35+
36+
// }
37+
38+
void *recieve(void *sock_fd){
39+
40+
int soc_fd = *(int *) sock_fd;
41+
42+
char buffer[Max_Size];
43+
bzero(buffer,Max_Size);
44+
char cache[Max_Size];
45+
bzero(cache,Max_Size);
46+
int rev_data;
47+
while(1) {
48+
49+
// printf(".\n");
50+
51+
bzero(buffer, Max_Size);
52+
int n = recv(soc_fd, buffer, Max_Size-1, MSG_DONTWAIT);
53+
54+
int len = strlen(buffer);
55+
56+
if (len > 0 && strcmp(buffer, cache) != 0) {
57+
printf("%s", buffer);
58+
bzero(cache, Max_Size);
59+
memcpy(cache, buffer, len);
60+
}
61+
62+
sleep(2);
63+
}
64+
close(soc_fd);
65+
return NULL;
66+
}
67+
68+
int main(int argc, char *argv[]){
69+
struct sockaddr_in sockinfo;
70+
int sock_fd;
71+
72+
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0){
73+
perror("socket failed");
74+
exit(EXIT_FAILURE);
75+
}
76+
77+
sockinfo.sin_family = AF_INET;
78+
sockinfo.sin_port = htons(PORT);
79+
inet_aton("127.0.0.1",&(sockinfo.sin_addr));
80+
memset(&(sockinfo.sin_zero),'\0',8);
81+
82+
83+
if(connect(sock_fd,(struct sockaddr *)&(sockinfo),sizeof(sockinfo))<0){
84+
perror("Failed");
85+
exit(EXIT_FAILURE);
86+
}
87+
88+
pthread_t rec;
89+
pthread_create(&rec,NULL,recieve,(void *)&sock_fd);
90+
91+
char buffer[Max_Size];
92+
93+
while(1){
94+
printf("> ");
95+
bzero(buffer,Max_Size);
96+
fgets(buffer,Max_Size-1,stdin);
97+
98+
int n = write(sock_fd,buffer,strlen(buffer));
99+
if(n < 0){
100+
break;
101+
}
102+
}
103+
printf("Lost connection to server\n");
104+
pthread_exit(NULL);
105+
close(sock_fd);
106+
return 0;
107+
108+
109+
}

message.h

Whitespace-only changes.

server.c

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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

Comments
 (0)