-
Notifications
You must be signed in to change notification settings - Fork 0
/
daytime-client.cc
140 lines (117 loc) · 3.33 KB
/
daytime-client.cc
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
//------------------------------------------------------------------------
// Program: client
//
// Purpose: allocate a socket, connect to a server, and print all output
//
// Syntax: client host port
//
// host - name of a computer on which server is executing
// port - protocol port number server is using
//
//------------------------------------------------------------------------
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void
printUsage()
{
printf( "Usage: client <host> <port> <name>\n");
printf( "\n");
printf( " host: host where the server is running.\n");
printf( " port: port that the server is using.\n");
printf( "\n");
printf( "Examples:\n");
printf( "\n");
printf( " client localhost 422422\n");
printf( " client lore 1345\n");
printf( "\n");
}
int
main(int argc, char **argv)
{
// Check command-line argument for protocol port and extract
// host and port number
if ( argc < 4 ) {
printUsage();
exit(1);
}
// Extract host name
char * host = argv[1];
// Extract port number
int port = atoi(argv[2]);
char * name = argv[3];
// Initialize socket address structure
struct sockaddr_in socketAddress;
// Clear sockaddr structure
memset((char *)&socketAddress,0,sizeof(socketAddress));
// Set family to Internet
socketAddress.sin_family = AF_INET;
// Test for port legal value
if (port > 0) {
socketAddress.sin_port = htons((u_short)port);
}
else {
fprintf(stderr,"bad port number %s\n", argv[2]);
exit(1);
}
// Get host table entry for this host
struct hostent *ptrh = gethostbyname(host);
if ( ptrh == NULL ) {
fprintf(stderr, "invalid host: %s\n", host);
perror("gethostbyname");
exit(1);
}
// Copy the host ip address to socket address structure
memcpy(&socketAddress.sin_addr, ptrh->h_addr, ptrh->h_length);
// Get TCP transport protocol entry
struct protoent *ptrp = getprotobyname("tcp");
if ( ptrp == NULL ) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
perror("getprotobyname");
exit(1);
}
// Create a tcp socket
int sock = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sock < 0) {
fprintf(stderr, "socket creation failed\n");
perror("socket");
exit(1);
}
// Connect the socket to the specified server
if (connect(sock, (struct sockaddr *)&socketAddress,
sizeof(socketAddress)) < 0) {
fprintf(stderr,"connect failed\n");
perror("connect");
exit(1);
}
// In this application we don't need to send anything.
// For your HTTP client you will need to send the request
// as specified in the handout using send().
char buffer[100+1];
int m = read(sock,buffer,100);
buffer[m]=0;
printf("buffer=%s\n", buffer);
write(sock, name, strlen(name));
write(sock,"\r\n",2);
// Receive reply
// Data received
char buf[1000];
int n = recv(sock, buf, sizeof(buf), 0);
while (n > 0) {
// Write n characters to stdout
write(1,buf,n);
// Continue receiving more
n = recv(sock, buf, sizeof(buf), 0);
}
// Close the socket.
closesocket(sock);
// Terminate the client program gracefully.
exit(0);
}