Name: Anonymous 2011-11-07 13:43
If you're so lazy that you have to rely on a lower level protocol for reliability, rather than implementing your own reliability features at the application layer, it's time to stop programming.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
...
struct sockaddr_in sock_in;
struct servent *sp;
struct hostent *host;
...
memset(&sock_in, 0, sizeof (sock_in));
sock_in.sin_family = AF_INET;
f = socket(AF_INET, SOCK_STREAM, 0);
if (f < 0)
error("socket");
if (bind(f, (struct sockaddr*)&sock_in, sizeof sock_in) < 0){
error("bind");
host = gethostbyname(argv[1]);
if(host){
sock_in.sin_family = host->h_addrtype;
memmove(&sock_in.sin_addr, host->h_addr, host->h_length);
}else{
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = inet_addr(argv[1]);
if (sock_in.sin_addr.s_addr == -1)
error("unknown host %s", argv[1]);
}
sp = getservbyname("discard", "tcp");
if (sp)
sock_in.sin_port = sp->s_port;
else
sock_in.sin_port = htons(9);
if (connect(f, (struct sockaddr*)&sock_in, sizeof sock_in) < 0){
error("connect:");#include <u.h>
#include <libc.h>
...
fd = dial(netmkaddr(argv[1], "tcp", "discard"), 0, 0, 0);
if(fd < 0)
sysfatal("can't dial %s: %r", argv[1]);