Created
February 26, 2010 01:43
-
-
Save raggi/315298 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is example code for an ML response. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
int main() { | |
int a, c, s, r; | |
struct sockaddr_in addr; | |
char ping[5] = "PING"; | |
char pong[5] = " "; | |
#ifdef USE_SELECT | |
fd_set reads; | |
struct timeval tv = {1, 0}; | |
#endif | |
memset(&addr, 0, sizeof(addr)); | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(1234); | |
a = inet_aton("127.0.0.1", &addr.sin_addr); | |
if (a <= 0) exit(EXIT_FAILURE); | |
s = socket(PF_INET, SOCK_STREAM, 0); | |
if (s < 0) exit(EXIT_FAILURE); | |
c = connect(s, (struct sockaddr *) &addr, sizeof(addr) ); | |
if (c < 0) exit(EXIT_FAILURE); | |
write(s, ping, 6); | |
#ifdef USE_SELECT | |
FD_ZERO(&reads); | |
FD_SET(s, &reads); | |
while(select(1,&reads,NULL,NULL,&tv) == -1) {} | |
#endif | |
r = read(s, pong, sizeof(pong)); | |
if (r < 0) | |
exit(EXIT_FAILURE); | |
else | |
printf("%s", pong); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake/clean' | |
cc = 'cc' | |
cflags = %w[-ansi -pedantic] | |
file 'client.c' | |
desc "build client.out" | |
file 'client.out' => 'client.c' do | |
sh cc, *(cflags + %w[-o client.out client.c]) | |
end | |
CLEAN.include('client.out') | |
desc "build with select" | |
task :select => :clean do | |
cflags << '-DUSE_SELECT' | |
end | |
desc "run server" | |
task :server do | |
sh Gem.ruby, "server.rb" | |
end | |
desc "run client" | |
task :client => 'client.out' do | |
sleep 0.5 | |
sh './client.out' | |
end | |
desc "run server and client" | |
multitask :run => [:server, :client] | |
task :default => :run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
module Srv | |
def receive_data(d) | |
if d =~ /ping/i | |
puts d | |
send_data("PONG\n") | |
close_connection_after_writing | |
end | |
end | |
def unbind | |
EM.stop | |
end | |
end | |
host, port = '127.0.0.1', 1234 | |
EM.run do | |
EM.add_timer(5) { abort "server timeout" } | |
EM.start_server host, port, Srv | |
puts 'server ready' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment