[Click] Help, about handler
wubaochuan
wubaochuan at seu.edu.cn
Thu Jan 20 02:24:04 EST 2011
Hi Beyers, Cliff,
Thank you for all your help.
I can telnet Click's ControlSocket.
Now I can connect to Click's element ControlSocket with a C
program. I summarize the solution of my problem, maybe it is helpful to
other newbies like me.
First I run /*click test.click*/, and then run the C program
/*client.c*/. The following is test.click and client.c.
test.click,
================start of test.click ==================
ControlSocket("TCP", 1234);
is::InfiniteSource(DATA \<00 00 c0 ae 67 ef 00 00 00 00 00 00 08 00
45 00 00 28 00 00 00 00 40 11 77 c3 01 00 00 01
02 00 00 02 13 69 13 69 00 14 d6 41 55 44 50 20
70 61 63 6b 65 74 21 0a>)//, LIMIT 5, STOP true)
-> Strip(14)
-> Align(4, 0) // in case we're not on x86
-> CheckIPHeader(BADSRC 18.26.4.255 2.255.255.255 1.255.255.255)
-> Print(ok)
-> Discard;
================end of test.click ==================
client.c, a simple tcp client.
================start of client.c ===================
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
char buf[BUFSIZ];
char bytes[20];
int sockfd;
int len;
struct sockaddr_in address;
int result;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
perror("socket() error");
exit(1);
}
address.sin_family = AF_INET;
inet_pton(AF_INET, "10.3.16.138", &address.sin_addr.s_addr); //
10.3.16.138 is IP of the machine which Click running on. You may change
it to proper IP.
address.sin_port = htons(1234); // Click's ControlSocket's port
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if (result == -1)
{
perror("connect() error");
exit(1);
}
else
{
printf("connect success\n");
}
FILE *controlfile = fdopen(sockfd, "r+");
fgets(buf, sizeof(buf)-1, controlfile);// get
"Click::ControlSocket/1.3"
fputs("read is.count\n", controlfile); // just as you input "read
is.count" when telnet Click.
fgets(buf, sizeof(buf)-1, controlfile);// get "200 Read handler
'is.count' OK"
fgets(buf, sizeof(buf)-1, controlfile);// get "DATA 7", "7" means
there is 7 bytes more. maybe you can get other number, such as 6.
sscanf(buf, "%*s%s", bytes); // put "7" into bytes[]
fread(buf, sizeof(char), atoi(bytes), controlfile);// get the
following 7B, which is the result of "read is.count"
printf("%s\n", buf);
close(sockfd);
fclose(controlfile);
return 0;
}
================end of client.c ====================
After I run /*click test.click*/, the click process is listening on port
1234, I can examine this using the following command: netstat -ntlp |
grep 1234,
On my machine, the above command yields: tcp 0 0
0.0.0.0:1234 0.0.0.0:* LISTEN 9987/click
And then, I can telnet click using : /*telnet 10.3.16.138 1234, */just
as the following:
root at wubaochuan-desktop:~# telnet 10.3.16.138 1234
Trying 10.3.16.138...
Connected to 10.3.16.138.
Escape character is '^]'.
Click::ControlSocket/1.3
read is.count
200 Read handler 'is.count' OK
DATA 8
13283904read is.count
200 Read handler 'is.count' OK
DATA 8
13341696
write is.reset
200 Write handler 'is.reset' OK
read is.count
200 Read handler 'is.count' OK
DATA 5
57792
quit
200 Goodbye!
Connection closed by foreign host.
root at wubaochuan-desktop:~#
Then I compiled client.c, and run it, I can get result of "read is.count".
ps: /*Unix network programming*/ is a good book. I am using Ubuntu 9.10
Linux, the compiler is gcc4.4.1.
Thank you.
Yours
Chuan
More information about the click
mailing list