Ruby
2.0.0p247(2013-06-27revision41674)
|
00001 /************************************************ 00002 00003 tcpsocket.c - 00004 00005 created at: Thu Mar 31 12:21:29 JST 1994 00006 00007 Copyright (C) 1993-2007 Yukihiro Matsumoto 00008 00009 ************************************************/ 00010 00011 #include "rubysocket.h" 00012 00013 /* 00014 * call-seq: 00015 * TCPSocket.new(remote_host, remote_port, local_host=nil, local_port=nil) 00016 * 00017 * Opens a TCP connection to +remote_host+ on +remote_port+. If +local_host+ 00018 * and +local_port+ are specified, then those parameters are used on the local 00019 * end to establish the connection. 00020 */ 00021 static VALUE 00022 tcp_init(int argc, VALUE *argv, VALUE sock) 00023 { 00024 VALUE remote_host, remote_serv; 00025 VALUE local_host, local_serv; 00026 00027 rb_scan_args(argc, argv, "22", &remote_host, &remote_serv, 00028 &local_host, &local_serv); 00029 00030 return rsock_init_inetsock(sock, remote_host, remote_serv, 00031 local_host, local_serv, INET_CLIENT); 00032 } 00033 00034 static VALUE 00035 tcp_sockaddr(struct sockaddr *addr, size_t len) 00036 { 00037 return rsock_make_ipaddr(addr); 00038 } 00039 00040 /* 00041 * call-seq: 00042 * TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list] 00043 * 00044 * Lookups host information by _hostname_. 00045 * 00046 * TCPSocket.gethostbyname("localhost") 00047 * #=> ["localhost", ["hal"], 2, "127.0.0.1"] 00048 * 00049 */ 00050 static VALUE 00051 tcp_s_gethostbyname(VALUE obj, VALUE host) 00052 { 00053 rb_secure(3); 00054 return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), 00055 tcp_sockaddr); 00056 } 00057 00058 void 00059 rsock_init_tcpsocket(void) 00060 { 00061 /* 00062 * Document-class: TCPSocket < IPSocket 00063 * 00064 * TCPSocket represents a TCP/IP client socket. 00065 * 00066 * A simple client may look like: 00067 * 00068 * require 'socket' 00069 * 00070 * s = TCPSocket.new 'localhost', 2000 00071 * 00072 * while line = s.gets # Read lines from socket 00073 * puts line # and print them 00074 * end 00075 * 00076 * s.close # close socket when done 00077 * 00078 */ 00079 rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket); 00080 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1); 00081 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1); 00082 } 00083