Skip to content

Commit 92e6364

Browse files
committed
Tuto indien
1 parent 42ad37f commit 92e6364

File tree

7 files changed

+110
-181
lines changed

7 files changed

+110
-181
lines changed

Client/client

-320 Bytes
Binary file not shown.

Client/client.c

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,70 @@
1-
#include <arpa/inet.h> // inet_addr()
2-
#include <netdb.h>
31
#include <stdio.h>
42
#include <stdlib.h>
53
#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};
4+
#include <arpa/inet.h>
5+
#include <unistd.h>
6+
#define SIZE 1024
7+
8+
int sockfd;
9+
10+
void connection(){
11+
char *ip = "127.0.0.1";
12+
int port = 8080;
13+
int e;
14+
15+
struct sockaddr_in server_addr;
2316

24-
while(fgets(data, MAX, fp) != NULL) {
25-
if (send(sockfd, data, sizeof(data), 0) == -1) {
26-
perror("[-]Error in sending file.");
17+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
18+
if(sockfd < 0){
19+
perror("[-]Error in socket");
20+
exit(1);
21+
}
22+
printf("[+]Server socket created successfully.\n");
23+
24+
server_addr.sin_family = AF_INET;
25+
server_addr.sin_port = port;
26+
server_addr.sin_addr.s_addr = inet_addr(ip);
27+
28+
e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
29+
if(e == -1){
30+
perror("[-]Error in socket");
2731
exit(1);
28-
}
29-
printf("Sent %d bytes to server : %s \n", n, data);
30-
bzero(data, MAX);
3132
}
33+
printf("[+]Connected to Server.\n");
3234

33-
printf("File sent successfully!\n");
3435
}
3536

3637

38+
void send_file(int sockfd){
39+
FILE *fp;
40+
char *filename = "test.txt";
41+
42+
fp = fopen(filename, "rb");
43+
if(fp == NULL){
44+
perror("[-]Error in reading file.");
45+
exit(1);
46+
}
3747

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);
48+
char data[SIZE] = {0};
49+
while(fgets(data, SIZE, fp) != NULL){
50+
if(send(sockfd, data, sizeof(data), 0) == -1){
51+
perror("[-]Error in sending file.");
52+
exit(1);
5553
}
54+
bzero(data, SIZE);
5655
}
56+
5757
}
5858

59-
int main()
60-
{
61-
int sockfd, connfd;
62-
struct sockaddr_in servaddr, cli;
59+
int main(){
6360

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");
61+
connection();
8762

88-
// function for chat
89-
func(sockfd);
63+
/*while(1){
9064
91-
// close the socket
92-
close(sockfd);
93-
}
65+
}*/
66+
67+
send_file(sockfd);
68+
69+
70+
}

Client/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bonjour je m'appelle simon
1+
BONJOUR JE M'APPELLE SIMON

Serveur/rcv.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/server

-216 Bytes
Binary file not shown.

Serveur/server.c

Lines changed: 56 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,79 @@
11
#include <stdio.h>
2-
#include <netdb.h>
3-
#include <netinet/in.h>
42
#include <stdlib.h>
53
#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
4+
#include <arpa/inet.h>
125

13-
int sockfd;
6+
#define SIZE 1024
147

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));
8+
int sockfd, new_stock;
279

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);
3210

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");
11+
void start_server(){
12+
char *ip = "127.0.0.1";
13+
int port = 8080;
14+
int e;
4015

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);
16+
//int sockfd, new_stock;
17+
struct sockaddr_in server_addr, new_addr;
18+
socklen_t addr_size;
19+
char buffer[SIZE];
4920

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);
21+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
22+
if(sockfd < 0){
23+
perror("[-]Error in socket");
24+
exit(1);
5525
}
56-
else
57-
printf("server accept the client...\n");
26+
printf("[+]Server socket created successfully.\n");
5827

59-
return connfd;
60-
}
28+
server_addr.sin_family = AF_INET;
29+
server_addr.sin_port = port;
30+
server_addr.sin_addr.s_addr = inet_addr(ip);
6131

32+
e = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
33+
if(e < 0){
34+
perror("[-]Error in bind");
35+
exit(1);
36+
}
37+
printf("[+]Binding successfull.\n");
6238

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);
39+
if(listen(sockfd, 10) == 0){
40+
printf("[+]Listening....\n");
41+
}else{
42+
perror("[-]Error in listening");
43+
exit(1);
8644
}
87-
return;
88-
}
8945

46+
addr_size = sizeof(new_addr);
47+
new_stock = accept(sockfd, (struct sockaddr*)&new_addr, &addr_size);
48+
}
9049

91-
// Function designed for chat between client and server.
92-
void func(int connfd)
93-
{
94-
char buff[MAX];
50+
void write_file(int param){
9551
int n;
96-
// infinite loop for chat
97-
for (;;) {
98-
bzero(buff, MAX);
52+
FILE *fp;
53+
char *filename = "rcv.txt";
54+
char buffer[SIZE];
55+
56+
fp = fopen(filename, "w");
57+
if(fp == NULL){
58+
perror("[-]Error in creating file.");
59+
exit(1);
60+
}
9961

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");
62+
while(1){
63+
n = recv(param, buffer, SIZE, 0);
64+
if(n <= 0){
10565
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-
66+
return;
67+
}
68+
fprintf(fp, "%s", buffer);
69+
bzero(buffer, SIZE);
11570
}
71+
return;
11672
}
11773

118-
// Driver function
119-
int main()
120-
{
121-
int connfd = start_server();
122-
123-
// Function for chatting between client and server
124-
func(connfd);
12574

126-
// After chatting close the socket
127-
close(sockfd);
128-
}
75+
int main(){
76+
77+
start_server();
78+
write_file(new_stock);
79+
}

Serveur/test.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)