-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecho_c.c
244 lines (198 loc) · 7.04 KB
/
echo_c.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// echo_c.c
// the client of the echo server that sends a message and its ip address to the echo server
// through TCP or UDP
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "echo_c_functions.h"
int main(int argc, char *argv[])
{
// determine whether the user wants a TCP or UDP connection
int ans_str;
printf("UDP(1) or TCP(2): ");
scanf("%d", &ans_str);
getchar();
// if the user wants a TCP connection
if(ans_str == 2)
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
int childpid, index, i;
char ip_buf[256];
char message_buf[256];
char buffer[256];
char toEcho_s[1024];
char wasRecvdFrom[] = " was received from ";
// create a pipe for executing the command to receive the ip address
int ip_pipe[2];
pipe(ip_pipe);
// output an error if no port is provided
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
// make a TCP socket to connect to the echo server
sockfd = makeSocket(SOCK_STREAM);
// connect to the server
portno = atoi(argv[2]);
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
// fork a child process, output an error if it fails
if((childpid = fork()) == -1)
error("ERROR creating child from echo_c");
// the child process executes the hostname command
if(childpid == 0)
{
// replace the stdout with the pipe
dup2(ip_pipe[1], 1);
// close all pipes
close(ip_pipe[0]); close(ip_pipe[1]);
// execute 'hostname -i'
char *hostname_args[] = {"hostname", "-i", NULL};
execvp(*hostname_args, hostname_args);
// exit the child process
exit(0);
}
// read the output of the 'hostname -i' command from the pipe, output an error if it fails
n = read(ip_pipe[0], ip_buf, sizeof(ip_buf));
if (n < 0)
error("ERROR reading from echo_c");
// close all pipes
close(ip_pipe[0]); close(ip_pipe[1]);
// receive the message from the user
printf("Please enter the message: ");
bzero(message_buf,256);
fgets(message_buf,255,stdin);
// toEcho_s = "messageFromUser"
for(index = 0; message_buf[index] != '\0' && index < strlen(message_buf) && message_buf[index] != '\n'; index++)
toEcho_s[index] = message_buf[index];
// toEcho_s += " was received from "
for(i = 0; wasRecvdFrom[i] != '\0' && i < strlen(wasRecvdFrom); i++, index++)
toEcho_s[index] = wasRecvdFrom[i];
// toEcho_s += "ipAddress"
for(i = 0; ip_buf[i] != '\0' && i < strlen(ip_buf); i++, index++)
toEcho_s[index] = ip_buf[i];
// toEcho_s += ""
toEcho_s[index] = '\0';
// write toEcho_s to echo server and output an error if it fails
n = write(sockfd,toEcho_s,strlen(toEcho_s));
if (n < 0)
error("ERROR writing to socket");
// read from echo server and output an error if it fails
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
// output the message from echo server to the stdout
printf("%s\n",buffer);
// close the socket
close(sockfd);
// terminate the program
return 0;
}
// if the user wants a UDP connection
else if(ans_str == 1)
{
int sock, n, childpid, index, i;
unsigned int length;
char ip_buf[256];
char message_buf[256];
char buffer[256];
char toEcho_s[1024];
struct sockaddr_in server, from;
struct hostent *hp;
char wasRecvdFrom[] = " was received from ";
// create a pipe for executing the command to receive the ip address
int ip_pipe[2];
pipe(ip_pipe);
// output an error if no port is provided
if (argc != 3) {
printf("Usage: server port\n");
exit(1);
}
// make a UDP socket to connect to the echo server
sock= makeSocket(SOCK_DGRAM);
// store server information and get hostname
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp==0) error("Unknown host");
bcopy((char *)hp->h_addr,
(char *)&server.sin_addr,
hp->h_length);
server.sin_port = htons(atoi(argv[2]));
length=sizeof(struct sockaddr_in);
// fork a child process, output an error if it fails
if((childpid = fork()) == -1)
error("ERROR creating child from echo_c");
// the child process executes the hostname command
if(childpid == 0)
{
// replace the stdout with the pipe
dup2(ip_pipe[1], 1);
// close all pipes
close(ip_pipe[0]); close(ip_pipe[1]);
// execute 'hostname -i'
char *hostname_args[] = {"hostname", "-i", NULL};
execvp(*hostname_args, hostname_args);
// exit the child process
exit(0);
}
// read the output of the 'hostname -i' command from the pipe, output an error if it fails
n = read(ip_pipe[0], ip_buf, sizeof(ip_buf));
if (n < 0)
error("ERROR reading from echo_c");
// close all pipes
close(ip_pipe[0]); close(ip_pipe[1]);
// receive the message from the user
printf("Please enter the message: ");
bzero(message_buf,256);
fgets(message_buf,255,stdin);
// toEcho_s = "messageFromUser"
for(index = 0; message_buf[index] != '\0' && index < strlen(message_buf) && message_buf[index] != '\n'; index++)
toEcho_s[index] = message_buf[index];
// toEcho_s += " was received from "
for(i = 0; wasRecvdFrom[i] != '\0' && i < strlen(wasRecvdFrom); i++, index++)
toEcho_s[index] = wasRecvdFrom[i];
// toEcho_s += "ipAddress"
for(i = 0; ip_buf[i] != '\0' && i < strlen(ip_buf); i++, index++)
toEcho_s[index] = ip_buf[i];
// toEcho_s += ""
toEcho_s[index] = '\0';
// write toEcho_s to echo server and output an error if it fails
n=sendto(sock,toEcho_s,
strlen(toEcho_s),0,(const struct sockaddr *)&server,length);
if (n < 0) error("Sendto");
// read from echo server and output an error if it fails
n = recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length);
if (n < 0) error("recvfrom");
// output the message from echo server to the stdout
write(1,"Got an ack: ",12);
write(1,buffer,n);
// close the socket
close(sock);
// terminate the program
return 0;
}
// if the user doesn't respond with UDP or TCP
else
error("ERROR: Must answer '1' or '2'");
// terminate the program
return 0;
}