forked from Kevin19950607/Socket_greek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw_client.c
77 lines (59 loc) · 1.32 KB
/
rw_client.c
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <malloc.h>
#define MESSAGE_SIZE 10240
#define SERVER_PORT 3000
#define SERVER_ADDR "127.0.0.1"
void senddata(int fd)
{
char* query;
query = (char*)malloc(MESSAGE_SIZE+1);
for(int i=0; i<MESSAGE_SIZE; i++)
{
query[i] = 'a';
}
query[MESSAGE_SIZE] = '\0';
const char* cp = query;
long len = strlen(cp); // query
while(len)
{
long n_written = send(fd, cp, len, 0);
fprintf(stdout, "send into buffer %ld \n", n_written);
if(n_written < 0)
{
printf("send\n");
return ;
}
len -= n_written;
cp += n_written;
}
}
int main(int agvc, char* argv[])
{
int clientfd;
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVER_PORT);
serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
clientfd = socket(AF_INET, SOCK_STREAM, 0);
if(clientfd < 0)
{
printf("creat socket error");
return -1;
}
int result = connect(clientfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if(result < 0)
{
printf("connect error");
return -1;
}
senddata(clientfd);
close(clientfd);
printf("send data finish\n"); // 非阻塞验证
return 0;
}