Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

tcpsocket.c
Go to the documentation of this file.
1 /************************************************
2
3 tcpsocket.c -
4
5 created at: Thu Mar 31 12:21:29 JST 1994
6
7 Copyright (C) 1993-2007 Yukihiro Matsumoto
8
9 ************************************************/
10
11 #include "rubysocket.h"
12
13 /*
14 * call-seq:
15 * TCPSocket.new(remote_host, remote_port, local_host=nil, local_port=nil)
16 *
17 * Opens a TCP connection to +remote_host+ on +remote_port+. If +local_host+
18 * and +local_port+ are specified, then those parameters are used on the local
19 * end to establish the connection.
20 */
21 static VALUE
22 tcp_init(int argc, VALUE *argv, VALUE sock)
23 {
24 VALUE remote_host, remote_serv;
25 VALUE local_host, local_serv;
26
27 rb_scan_args(argc, argv, "22", &remote_host, &remote_serv,
28 &local_host, &local_serv);
29
30 return rsock_init_inetsock(sock, remote_host, remote_serv,
31 local_host, local_serv, INET_CLIENT);
32 }
33
34 static VALUE
35 tcp_sockaddr(struct sockaddr *addr, size_t len)
36 {
37 return rsock_make_ipaddr(addr);
38 }
39
40 /*
41 * call-seq:
42 * TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
43 *
44 * Lookups host information by _hostname_.
45 *
46 * TCPSocket.gethostbyname("localhost")
47 * #=> ["localhost", ["hal"], 2, "127.0.0.1"]
48 *
49 */
50 static VALUE
51 tcp_s_gethostbyname(VALUE obj, VALUE host)
52 {
53 rb_secure(3);
54 return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME),
55 tcp_sockaddr);
56 }
57
58 void
59 rsock_init_tcpsocket(void)
60 {
61 /*
62 * Document-class: TCPSocket < IPSocket
63 *
64 * TCPSocket represents a TCP/IP client socket.
65 *
66 * A simple client may look like:
67 *
68 * require 'socket'
69 *
70 * s = TCPSocket.new 'localhost', 2000
71 *
72 * while line = s.gets # Read lines from socket
73 * puts line # and print them
74 * end
75 *
76 * s.close # close socket when done
77 *
78 */
79 rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket);
80 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
81 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
82 }
83