-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_class.cpp
More file actions
125 lines (115 loc) · 3.19 KB
/
client_class.cpp
File metadata and controls
125 lines (115 loc) · 3.19 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
/**************************************************************************/
/* Generic client example is used with connection-oriented server designs */
/**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#define SERVER_PORT 12345
class client
{
private:
/* data */
public:
int len, rc;
int sockfd;
char send_buf[80];
char recv_buf[80];
struct sockaddr_in6 addr;
int PORT;
client(int);
~client();
};
client::client(int port)
{
PORT = 0;
}
client::~client()
{
}
int main (int argc, char *argv[])
{
int len, rc;
int sockfd;
char send_buf[80];
char recv_buf[80];
struct sockaddr_in6 addr;
/*************************************************/
/* Create an AF_INET6 stream socket */
/*************************************************/
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("socket");
exit(-1);
}
/*************************************************/
/* Initialize the socket address structure */
/*************************************************/
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
addr.sin6_port = htons(SERVER_PORT);
/*************************************************/
/* Connect to the server */
/*************************************************/
rc = connect(sockfd,
(struct sockaddr *)&addr,
sizeof(struct sockaddr_in6));
if (rc < 0)
{
perror("connect");
close(sockfd);
exit(-1);
}
printf("Connect completed.\n");
while (1)
{
/*************************************************/
/* Enter data buffer that is to be sent */
/*************************************************/
memset(send_buf, '\0', sizeof(char)*80);
for (int k=0; k<80; k++)
{
send_buf[k]='\0';
}
printf("Enter message to be sent:\n");
gets(send_buf);
/*************************************************/
/* Send data buffer to the worker job */
/*************************************************/
len = send(sockfd, send_buf, strlen(send_buf) + 1, 0);
if (len != strlen(send_buf) + 1)
{
perror("send");
}
printf("%d bytes sent\n", len);
if (len==2)
{
if (send_buf[0]=='%')
{
break;
close(sockfd);
exit(-1);
}
}
/*************************************************/
/* Receive data buffer from the worker job */
/*************************************************/
len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
if (len != strlen(send_buf) + 1)
{
perror("recv");
// close(sockfd);
// exit(-1);
}
printf("%d bytes received\n", len);
}
/*************************************************/
/* Close down the socket */
/*************************************************/
close(sockfd);
return 0;
}