-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneralpolling.c
68 lines (56 loc) · 2.08 KB
/
generalpolling.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
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <memory.h>
#define SERVER_PORT 8801 // define the defualt connect port id
#define CLIENT_PORT ((20001+rand())%65536) // define the defualt client port as a random port
#define BUFFER_SIZE 1024
void usage(char *name)
{
printf("usage: %s IpAddr\n",name);
}
char *string="\x03\x00\x00\x61<omc>\n\t<packcd>6002</packcd>\n\t<taskid>277</taskid>\n\t<style>1</style>\n\t<areaid>13</areaid>\n</omc>\n";
int main(int argc, char **argv)
{
int servfd = 0, clifd = 0, length = 0;
struct sockaddr_in servaddr, cliaddr;
socklen_t socklen = sizeof(servaddr);
char buf[BUFFER_SIZE] = {0};
memcpy(buf, string, 0x61+4);
if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
srand(time(NULL));//initialize random generator
memset(&cliaddr, 0, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(CLIENT_PORT);
cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_aton("192.168.60.141",&servaddr.sin_addr);
servaddr.sin_port = htons(SERVER_PORT);
if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
{
printf("bind to port %d failure!\n",CLIENT_PORT);
exit(1);
}
if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0)
{
printf("can't connect to %s!\n",argv[1]);
exit(1);
}
int wsize = send(clifd, buf, 0x61+4, 0);
if(wsize <= 0)
{
printf("send error\n");
exit(1);
}
wsize = recv(clifd, buf, BUFFER_SIZE, 0);
close(clifd);
return 0;
}