Skip to content

Commit 42ad37f

Browse files
committed
V1
1 parent f56ba24 commit 42ad37f

File tree

11 files changed

+229
-0
lines changed

11 files changed

+229
-0
lines changed

Client/client

16.4 KB
Binary file not shown.

Client/client.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <arpa/inet.h> // inet_addr()
2+
#include <netdb.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <strings.h> // bzero()
7+
#include <sys/socket.h>
8+
#include <unistd.h> // read(), write(), close()
9+
#define MAX 1024
10+
#define PORT 8080
11+
#define SA struct sockaddr
12+
13+
void send_file(int sockfd, const char *filename) {
14+
FILE *fp;
15+
// Open the file for reading
16+
fp = fopen(filename, "rb");
17+
if (fp == NULL) {
18+
printf("Error opening file for reading...\n");
19+
return;
20+
}
21+
int n;
22+
char data[MAX] = {0};
23+
24+
while(fgets(data, MAX, fp) != NULL) {
25+
if (send(sockfd, data, sizeof(data), 0) == -1) {
26+
perror("[-]Error in sending file.");
27+
exit(1);
28+
}
29+
printf("Sent %d bytes to server : %s \n", n, data);
30+
bzero(data, MAX);
31+
}
32+
33+
printf("File sent successfully!\n");
34+
}
35+
36+
37+
38+
void func(int sockfd)
39+
{
40+
char buff[MAX];
41+
int n;
42+
for (;;) {
43+
bzero(buff, sizeof(buff));
44+
printf("Enter your command : ");
45+
n = 0;
46+
while ((buff[n++] = getchar()) != '\n')
47+
;
48+
write(sockfd, buff, sizeof(buff));
49+
50+
// Check if the command is for uploading a file
51+
if (strncmp("sectrans -up", buff, 12) == 0) {
52+
// Extract filename from the command
53+
char *filename = strtok(buff + 13, " \n");
54+
send_file(sockfd, filename);
55+
}
56+
}
57+
}
58+
59+
int main()
60+
{
61+
int sockfd, connfd;
62+
struct sockaddr_in servaddr, cli;
63+
64+
// socket create and verification
65+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
66+
if (sockfd == -1) {
67+
printf("socket creation failed...\n");
68+
exit(0);
69+
}
70+
else
71+
printf("Socket successfully created..\n");
72+
bzero(&servaddr, sizeof(servaddr));
73+
74+
// assign IP, PORT
75+
servaddr.sin_family = AF_INET;
76+
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
77+
servaddr.sin_port = htons(PORT);
78+
79+
// connect the client socket to server socket
80+
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr))
81+
!= 0) {
82+
printf("connection with the server failed...\n");
83+
exit(0);
84+
}
85+
else
86+
printf("connected to the server..\n");
87+
88+
// function for chat
89+
func(sockfd);
90+
91+
// close the socket
92+
close(sockfd);
93+
}

Client/client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* send message (maximum size: 1024 bytes) */
2+
int sndmsg(char msg[1024], int port);

Client/libclient.so

16.3 KB
Binary file not shown.

Client/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bonjour je m'appelle simon

Serveur/libserver.so

16.6 KB
Binary file not shown.

Serveur/server

16.4 KB
Binary file not shown.

Serveur/server.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <stdio.h>
2+
#include <netdb.h>
3+
#include <netinet/in.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <sys/socket.h>
7+
#include <sys/types.h>
8+
#include <unistd.h> // read(), write(), close()
9+
#define MAX 1024
10+
#define PORT 8080
11+
#define SA struct sockaddr
12+
13+
int sockfd;
14+
15+
int start_server(){
16+
int connfd, len;
17+
struct sockaddr_in servaddr, cli;
18+
// socket create and verification
19+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
20+
if (sockfd == -1) {
21+
printf("socket creation failed...\n");
22+
exit(0);
23+
}
24+
else
25+
printf("Socket successfully created..\n");
26+
bzero(&servaddr, sizeof(servaddr));
27+
28+
// assign IP, PORT
29+
servaddr.sin_family = AF_INET;
30+
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
31+
servaddr.sin_port = htons(PORT);
32+
33+
// Binding newly created socket to given IP and verification
34+
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
35+
printf("socket bind failed...\n");
36+
exit(0);
37+
}
38+
else
39+
printf("Socket successfully binded..\n");
40+
41+
// Now server is ready to listen and verification
42+
if ((listen(sockfd, 5)) != 0) {
43+
printf("Listen failed...\n");
44+
exit(0);
45+
}
46+
else
47+
printf("Server listening..\n");
48+
len = sizeof(cli);
49+
50+
// Accept the data packet from client and verification
51+
connfd = accept(sockfd, (SA*)&cli, &len);
52+
if (connfd < 0) {
53+
printf("server accept failed...\n");
54+
exit(0);
55+
}
56+
else
57+
printf("server accept the client...\n");
58+
59+
return connfd;
60+
}
61+
62+
63+
void receive_file(int connfd, char *filename) {
64+
char buff[MAX];
65+
FILE *fp;
66+
int n;
67+
68+
printf("Receiving file...%s\n", filename);
69+
70+
// Open the file for writing
71+
fp = fopen(filename, "wb");
72+
if (fp == NULL) {
73+
printf("Error opening file for writing...\n");
74+
return;
75+
}
76+
77+
while (1) {
78+
n = recv(sockfd, buff, MAX, 0);
79+
printf("received %d bytes to server : %s \n", n, buff);
80+
if (n <= 0){
81+
break;
82+
return;
83+
}
84+
fprintf(fp, "%s", buff);
85+
bzero(buff, MAX);
86+
}
87+
return;
88+
}
89+
90+
91+
// Function designed for chat between client and server.
92+
void func(int connfd)
93+
{
94+
char buff[MAX];
95+
int n;
96+
// infinite loop for chat
97+
for (;;) {
98+
bzero(buff, MAX);
99+
100+
// read the message from client and copy it in buffer
101+
read(connfd, buff, sizeof(buff));
102+
// if msg contains "Exit" then server exit and chat ended.
103+
if (strncmp("exit", buff, 4) == 0) {
104+
printf("Server Exit...\n");
105+
break;
106+
} else if (strncmp("sectrans -list", buff, 14)==0){
107+
printf("LIST....\n");
108+
//Do something......
109+
} else if (strncmp("sectrans -up", buff, 12) == 0) {
110+
// Receive file from client
111+
char *filename = strtok(buff + 13, " \n");
112+
receive_file(connfd, filename);
113+
}
114+
115+
}
116+
}
117+
118+
// Driver function
119+
int main()
120+
{
121+
int connfd = start_server();
122+
123+
// Function for chatting between client and server
124+
func(connfd);
125+
126+
// After chatting close the socket
127+
close(sockfd);
128+
}

Serveur/server.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int startserver(int port);
2+
int stopserver();
3+
4+
/* read message sent by client */
5+
int getmsg(char msg_read[1024]);

Serveur/test.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)