-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-client.cpp
62 lines (56 loc) · 1.99 KB
/
1-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
60
61
62
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
using namespace std;
#define ASSERT(err) \
do \
{ \
if (err < 0) \
{ \
printf("[%s][%d] error : %d\n", __FILE__, __LINE__, err); \
fflush(stdout); \
exit(err); \
} \
} while (0)
#define DEBUG printf("[%d]\n", __LINE__)
int main(int argc, char * argv[])
{
struct hostent * server = gethostbyname("localhost");
struct sockaddr_in clientAddr;
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons(51717);
memcpy(&clientAddr.sin_addr.s_addr, server->h_addr, server->h_length);
int ret = 0;
int fd = socket(AF_INET, SOCK_STREAM, 0);
ASSERT(fd);
ret = connect(fd, (struct sockaddr *) & clientAddr, sizeof(clientAddr));
ASSERT(ret);
socklen_t clientLenght = sizeof(clientAddr);
bool exit = false;
while(!exit)
{
DEBUG;
char readBuffer[256] = {0};
DEBUG;
char writeBuffer[256] = {0};
DEBUG;
fgets(&writeBuffer[0], 256, stdin);
printf("size of wb %d\n", strlen(&writeBuffer[0]));
DEBUG;
int writeLength = write(fd, &writeBuffer[0], strlen(&writeBuffer[0]));
printf("%d : %s\n",writeLength, &writeBuffer[0]);
DEBUG;
int readLength = read(fd, &readBuffer[0], 256);
DEBUG;
printf("%d : %s\n",readLength, &readBuffer[0]);
DEBUG;
fflush(stdin);
}
close(fd);
return 0;
}