-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_base.c
More file actions
executable file
·215 lines (198 loc) · 6.7 KB
/
client_base.c
File metadata and controls
executable file
·215 lines (198 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Program: client_base.c
* Author: Paul Girard Ph.D., DIM, UQAC
* Date: Sept 2013
*
* Objective: Show how to use a tcp socket
* on a client program running on
* Sun/ Solaris ; this program transmits
* a message to a server and receives
* an acknowledge each time. A delay of
* one second is introduced between messages.
*
* Options to compile on Solaris:
* ===> gcc client_base.c -lsocket -lnsl -o client_base
* Options to compile on Linux:
* ===> gcc client_base.c -lnsl -o client_base
*
* Execution: 3 parameters: program_name server_name server port_number
* =========> client_base server_machine port_number
* ex. client_base dimensxcn1.uqac.ca 5---
*
*
* Files to include
*/
#include <errno.h>
/* #include <strings.h> */
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>
/*
* The following functions are used in this program
*
* socket, gethostbyname, connect,
* read, write, close.
*/
#define DATA "hahahaha++++++++++++++++++++++++++++++++++"
char local_filename[20]; /* save the local_file_name which is input by user */
char remote_filename[20]; /* save the remote_file_name which is input bu user */
int sock2, msgsock; /* socket descriptor */
struct sockaddr_in server2; /* structure used to assign a name
to a socket sccording internet format*/
size_t length; /* #octets in structure sockadr_in */
struct hostent *hp,*gethostbyname(); /* structure to associate the
server name and its address */
struct hostent hpstruct; /* structure to access the server address*/
char buf[128]; /* message buffer*/
int rval; /* status code for read */
FILE *fptr; /* pointer to file descriptor */
char fileContentBuf[81] ; /*read the file buffer*/
int inputFileName(){
printf("myftp> ");
if(scanf("put %s",local_filename) == EOF || scanf("%s",remote_filename) == EOF ){
return 0 ;
}else
return 1;
}
int getConnection(){
/* 5. Try a connection with the server
*/
if (connect(sock2, (struct sockaddr *)&server2, sizeof(server2)) < 0)
{
perror("Error at connect time");
return 0;
}else
return 1;
}
int getSocketBase(int argc , char *argv[]){
/*
* 1. Validation of the parameters read on the command line
*/
if (argc != 3)
{
printf("Call the program : client_base server_name port_number\n");
return 0;
}
/*
* 2. Socket creation parameters:
* - AF_INET is the internet format
* - SOCK_STREAM specifies a TCP type socket
* - 0 specifies to use the default protocol.
*/
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 < 0)
{
perror("Error in creating a TCP socket");
return 0;
}
/*
* 3. Get a pointer to the network server address structure.
* The server name is taken from the command line (argv[1]):
* ex. "sunens.uqac.ca"
*/
server2.sin_family = AF_INET; /* internet format*/
hp = gethostbyname(argv[1]); /* pointer to the network structure
of the server listening on port
argv[1] */
if (hp == 0)
{
fprintf(stderr, "%s: unknown host ===>", argv[1]);
return 0;
}
hpstruct = *hp; /* save hostent data */
/*
* 4. Fill in the remote address part
*/
server2.sin_family = hpstruct.h_addrtype;
server2.sin_port = htons(atoi(argv[2])); /* char port # ==>integer port #
====> network 16 bits format */
server2.sin_addr = * ((struct in_addr *) hpstruct.h_addr);
return 1;
}
int transferFileName(){
if(write(sock2,local_filename ,strlen(DATA)) < 0)
return 0 ;
else
return 1 ;
}
int openFile(){
if( (fptr = fopen(local_filename,"r") ) == NULL)
{
perror("error opening this file") ;
return 0 ;
}
return 1 ;
}
int sendFile(){
if(openFile()){ /* start read the file and to transfer the file*/
while(fgets(fileContentBuf,81,fptr)){
if(write(sock2, fileContentBuf,strlen(fileContentBuf)) < 0) {
perror("ERROR in the ransmission the file") ;
}else {
/*
if ((rval = read(sock2, buf, 1024)) < 0)
perror("Error in reading a message");
if (rval == 0)
printf("End of connection\n");
else
{
buf[rval]='\0';
printf("===>%s\n", buf);
} */
}
}
}
}
int save(){
/*
* 6. Trnsmit 10 messages to the server and
* receive an ackknowledge from the server each time.
*/
int i ;
for (i=0; i<10; i++)
{
// sleep(1); /* zzzz: wait 1 second (option) */
printf("%s --> %s",local_filename,remote_filename) ;
if(write(sock2, DATA, strlen(DATA)) < 0)
perror("Error in the transmission of message");
else {
/* read a message from the server */
if ((rval = read(sock2, buf, 1024)) < 0)
perror("Error in reading a message");
if (rval == 0)
printf("End of connection\n");
else
{
buf[rval]='\0';
printf("===>%s\n", buf);
}
}
}
}
int main(int argc,char *argv[])
{
if( !getSocketBase(argc,argv) )
{
return 0 ;
}
if( !getConnection() ){
return 0 ;
}
if( !inputFileName() ){
return 0 ;
}
if( !sendFile() ) {
return 0 ;
}
if( !transferFileName())
{
return 0;
}
close(sock2);
puts("End of client program");
return 0;; /* end of client program */
}