Skip to content

Commit d8aad56

Browse files
committed
working 8-file-transfer-protocol
1 parent d72d0b6 commit d8aad56

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ Module.symvers
5151
Mkfile.old
5252
dkms.conf
5353
.dropbox
54+
server

7-full-duplex-chat-tcp-ip/client.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ int main(int argc, char *argv[])
5454
struct sockaddr_in server_address;
5555

5656
bzero(&server_address, sizeof(server_address));
57-
//what type of address we are woking with
58-
server_address.sin_family = AF_INET;
59-
57+
58+
struct hostent *host;
59+
host=gethostbyname("127.0.0.1");
6060
//for taking the port number and htons converts the port # to the appropriate data type we want to write
6161
//to specifying the port
6262
//htons : conversion functions
6363
server_address.sin_port = htons(PORT);
6464

6565
//structure within structure A.B.c
66-
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
66+
memcpy((char *)&server_address.sin_addr.s_addr,host->h_addr_list[0],host->h_length);
67+
68+
//what type of address we are woking with
69+
server_address.sin_family = host->h_addrtype;
6770

68-
6971
//connect returns us a response that connection is establlised or not
7072
//check for the error with the connection
7173
if (connect(network_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == -1)

8-file-transfer-protocol/client.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Title : File Transfer Protocol
3+
* Name : Aditya Pratap Singh Rajput
4+
* Subject : Network Protocols And Programming
5+
6+
Note : Please consider the TYPOS in comments.
7+
Thanks.
8+
*/
9+
10+
#include "stdio.h"
11+
#include "stdlib.h"
12+
#include "string.h"
13+
//headers for socket and related functions
14+
#include <sys/types.h>
15+
#include <sys/socket.h>
16+
#include <sys/stat.h>
17+
//for including structures which will store information needed
18+
#include <netinet/in.h>
19+
#include <unistd.h>
20+
//for gethostbyname
21+
#include "netdb.h"
22+
#include "arpa/inet.h"
23+
24+
// defining constants
25+
#define PORT 9002
26+
#define BACKLOG 5
27+
int main()
28+
{
29+
int size;
30+
int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
31+
struct sockaddr_in server_address, client_address;
32+
socklen_t client_length;
33+
client_length = sizeof(client_address);
34+
struct stat x;
35+
char buffer[100], file[1000];
36+
FILE *filePointer;
37+
bzero(&server_address, sizeof(server_address));
38+
server_address.sin_family = AF_INET;
39+
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
40+
server_address.sin_port = htons(PORT);
41+
42+
connect(socket_descriptor,(struct sockaddr*)&server_address,sizeof(server_address));
43+
44+
while (1){
45+
printf("File name : ");
46+
scanf("%s",buffer);
47+
send(socket_descriptor,buffer,strlen(buffer)+1,0);
48+
printf("%s\n","File Output : ");
49+
recv(socket_descriptor,file,sizeof(file),0);
50+
printf("%s",file);
51+
}
52+
return 0;
53+
}

8-file-transfer-protocol/server.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Title : File Transfer Protocol
3+
* Name : Aditya Pratap Singh Rajput
4+
* Subject : Network Protocols And Programming
5+
6+
Note : Please consider the TYPOS in comments.
7+
Thanks.
8+
*/
9+
10+
#include "stdio.h"
11+
#include "stdlib.h"
12+
#include "string.h"
13+
//headers for socket and related functions
14+
#include <sys/types.h>
15+
#include <sys/socket.h>
16+
#include <sys/stat.h>
17+
//for including structures which will store information needed
18+
#include <netinet/in.h>
19+
#include <unistd.h>
20+
//for gethostbyname
21+
#include "netdb.h"
22+
#include "arpa/inet.h"
23+
24+
// defining constants
25+
#define PORT 9002
26+
#define BACKLOG 5
27+
int main()
28+
{ int size;
29+
int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
30+
struct sockaddr_in server_address, client_address;
31+
socklen_t client_length;
32+
struct stat x;
33+
char buffer[100], file[1000];
34+
FILE *filePointer;
35+
bzero(&server_address, sizeof(server_address));
36+
server_address.sin_family = AF_INET;
37+
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
38+
server_address.sin_port = htons(PORT);
39+
if (bind(socket_descriptor, (struct sockaddr *)&server_address, sizeof(server_address)) == -1)
40+
{
41+
perror("bind");
42+
}
43+
listen(socket_descriptor, 5);
44+
printf("Server has started working ...");
45+
int client_descriptor = accept(socket_descriptor,(struct sockaddr*)&client_address,&client_length);
46+
47+
while(1){
48+
bzero(buffer,sizeof(buffer));
49+
bzero(file,sizeof(file));
50+
recv(client_descriptor,buffer,sizeof(buffer),0);
51+
filePointer = fopen(buffer,"r");
52+
stat(buffer,&x);
53+
size=x.st_size;
54+
fread(file,sizeof(file),1,filePointer);
55+
send(client_descriptor,file,sizeof(file),0);
56+
}
57+
return 0;
58+
}

8-file-transfer-protocol/text.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
this is coming from txt file.
2+
.

0 commit comments

Comments
 (0)