-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cxx
170 lines (132 loc) · 3.92 KB
/
client.cxx
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
// Socket client
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/******************************************************************************\
******************************************************************************
******************************************************************************
********************************* MAIN *********************************
******************************************************************************
******************************************************************************
\******************************************************************************/
int main(int argc, char* argv[])
{
//
// First, parse command args - get the port number
//
// rudimentary form of arg checking
int nPortNumber = 0;
if (argc != 3)
{
::fprintf(stdout, "ERROR: Insufficient number of parameters!\n");
::fprintf(stdout, "Usage:\n");
::fprintf(stdout, "client -pn <PortNumber>\n");
return -1;
}
else
{
if (::strcmp(argv[1], "-pn") == 0)
{
nPortNumber = ::atoi(argv[2]);
}
else
{
::fprintf(stdout, "ERROR: Invalid switch!\n");
return -1;
}
}
::fprintf(stdout, "INFO: Client started.\n");
//
// Resolve our host's network address
//
::fprintf(stdout, "INFO: Resolving Host='localhost'...\n");
char* szHostname = "localhost";
struct hostent* pHostInfo = ::gethostbyname(szHostname);
if (pHostInfo == NULL)
{
::fprintf(stderr, "ERROR: Unknown host=%s.\n", szHostname);
::exit(1);
}
::fprintf(stdout, "INFO: Host='localhost' successfully resolved.\n");
//
// Create the socket & connect to it
//
// create the socket
::fprintf(stdout, "INFO: Opening the socket...\n");
int iSockDscrptr = ::socket(AF_INET, SOCK_STREAM, 0);
if (iSockDscrptr < 0)
{
::perror("socket");
::exit(1);
}
// specify the address of the server
struct sockaddr_in socketAddress;
::memset(&socketAddress, 0, sizeof(struct sockaddr_in));
socketAddress.sin_family = AF_INET;
socketAddress.sin_port = htons(nPortNumber); // htons is used for 'network byte order'
::memcpy(&socketAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length);
// make the conection
int iRetCode = ::connect(iSockDscrptr, (struct sockaddr *) &socketAddress, sizeof(struct sockaddr_in));
if (iRetCode < 0)
{
::perror("connect");
::exit(1);
}
::fprintf(stdout, "INFO: Socket successfully opened.\n");
//
// Send a request
//
// send something to the server
::fprintf(stdout, "INFO: Sending msg to the server...\n");
char* szMessage = "12345678901234567|servicefilename.ext";
iRetCode = ::send(iSockDscrptr, szMessage, strlen(szMessage), 0);
if (iRetCode < 0)
{
::perror("send");
::exit(1);
}
::fprintf(stdout, "INFO: Msg successfully sent to server.");
//
// Receive a response
//
::fprintf(stdout, "INFO: Checking for a response from the server...\n");
char szBuffer[1024];
iRetCode = ::recv(iSockDscrptr, szBuffer, sizeof(szBuffer), 0);
if (iRetCode > 0)
{
// extract the return code
char szServerRetCode[2];
szServerRetCode[0] = szBuffer[0];
szServerRetCode[1] = '\0';
int iServerRetCode = ::atoi(szServerRetCode);
if (iServerRetCode == 0)
{
::fprintf(stdout, "INFO: Response from server successfully received.\n");
}
else if (iServerRetCode == 1)
{
::fprintf(stderr, "ERROR: Call failed due to invalid data.\n");
}
else
{
::fprintf(stdout, "Opps, it didn't seem to work\n");
::fprintf(stderr, "ERROR: Failed to receive msg from server, Error Code = %d\n", iServerRetCode);
}
}
else
{
::fprintf(stderr, "ERROR: Nothing received back from the server.\n");
}
//
// Close the socket
//
::fprintf(stdout, "INFO: Closing socket...\n");
::close(iSockDscrptr);
::fprintf(stdout, "INFO: Socket successfully closed.\n");
return 0;
}