-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-thread.cpp
73 lines (61 loc) · 1.71 KB
/
client-thread.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Make the necessary includes and set up the variables. */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
int main()
{
int server_sockfd;
int len;
struct sockaddr_in address;
int ret;
char read_buf[10];
/* Create a socket for the client. */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server. */
address.sin_family = AF_INET;
//address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_addr.s_addr = inet_addr("192.168.0.14");
address.sin_port = htons(10006);
len = sizeof(address);
/* Now connect our socket to the server's socket. */
ret = connect(server_sockfd, (struct sockaddr *)&address, len);
if(ret < 0)
{
printf("\t *** socket client connect() error!\n");
}
printf("client is connected with server_sockfd = %d\n", server_sockfd);
if(ret == -1)
{
perror("oops: client3");
exit(1);
}
//signal(SIGPIPE, SIG_IGN);
/* We can now read/write via server_sockfd. */
while(1)
{
memset(&read_buf, 0, sizeof(read_buf));
ret = read(server_sockfd, &read_buf, 10);
if(ret < 0)
{
printf("\t *** socket client read() error!\n");
}
else
{
printf("client-rcvd msg: %s\n", read_buf);
}
unsigned char send_buf[2] = {0xa6, 0x51};
ret = write(server_sockfd, &send_buf, 2);
if(ret < 0)
{
printf("\t *** socket client read() error!\n");
}
}
close(server_sockfd);
exit(0);
}