-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
executable file
·59 lines (51 loc) · 1.3 KB
/
client.cpp
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
#include "client.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
static void error(const char *msg)
{
perror(msg);
exit(1);
}
void client_main(int port)
{
struct sockaddr_in serv_addr;
struct hostent *server;
int n;
char buffer[256];
int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0)
{
error("ERROR opening socket");
}
server = gethostbyname("localhost");
if (server == NULL)
{
error("ERROR, no such host\n");
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(port);
if (connect(socketDescriptor,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR connecting");
}
n = write(socketDescriptor,"12345XXX",8);
if (n < 0)
{
error("ERROR writing to socket");
}
memset(buffer,0,256);
n = read(socketDescriptor,buffer,255);
if (n < 0)
{
error("ERROR reading from socket.");
}
printf("CLIENT: %s\n",buffer);
close(socketDescriptor);
}