Socket
Object Hierarchy:
Description:
[
CCode ( type_id =
"g_socket_get_type ()" ) ]
[
Version ( since =
"2.22" ) ]
public class Socket :
Object,
DatagramBased,
Initable
Example: Sockets:
public static void server (SocketAddress address) throws Error {
Socket socket = new Socket (SocketFamily.IPV4, SocketType.STREAM, SocketProtocol.TCP);
assert (socket != null);
socket.bind (address, true);
socket.set_listen_backlog (10);
socket.listen ();
for (int i = 0; true ; i = (i + 1) % 10) {
Socket connection = socket.accept ();
print (@"Accepted ($i)\n");
connection.send (i.to_string ().data);
}
}
public static void client (SocketAddress address) throws Error {
Socket socket = new Socket (SocketFamily.IPV4, SocketType.STREAM, SocketProtocol.TCP);
assert (socket != null);
socket.connect (address);
print ("Connected\n");
uint8 buffer[100];
ssize_t len;
len = socket.receive (buffer);
stdout.write (buffer, len);
print ("\n");
}
public static int main (string[] args) {
try {
if (args.length != 2) {
print ("%s server|client\n", args[0]);
return 0;
}
InetAddress address = new InetAddress.loopback (SocketFamily.IPV4);
InetSocketAddress inetaddress = new InetSocketAddress (address, 2001);
if (args[1] == "server") {
server (inetaddress);
} else if (args[1] == "client") {
client (inetaddress);
} else {
print ("Unknown option.\n");
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg gio-2.0 GLib.Socket.vala
Content:
Properties:
Creation methods:
Methods:
- public Socket accept (Cancellable? cancellable = null) throws Error
- public bool bind (SocketAddress address, bool allow_reuse) throws Error
- public bool check_connect_result () throws Error
- public bool close () throws Error
- public IOCondition condition_check (IOCondition condition)
- public bool condition_timed_wait (IOCondition condition, int64 timeout_us, Cancellable? cancellable = null) throws Error
- public bool condition_wait (IOCondition condition, Cancellable? cancellable = null) throws IOError
- public bool connect (SocketAddress address, Cancellable? cancellable = null) throws Error
- public SocketSource create_source (IOCondition condition, Cancellable? cancellable = null)
- public ssize_t get_available_bytes ()
- public bool get_blocking ()
- public bool get_broadcast ()
- public Credentials get_credentials () throws Error
- public SocketFamily get_family ()
- public int get_fd ()
- public bool get_keepalive ()
- public int get_listen_backlog ()
- public SocketAddress get_local_address () throws Error
- public bool get_multicast_loopback ()
- public uint get_multicast_ttl ()
- public bool get_option (int level, int optname, out int value) throws Error
- public SocketProtocol get_protocol ()
- public SocketAddress get_remote_address () throws Error
- public SocketType get_socket_type ()
- public uint get_timeout ()
- public uint get_ttl ()
- public bool is_closed ()
- public bool is_connected ()
- public bool join_multicast_group (InetAddress group, bool source_specific, string? iface) throws Error
- public bool join_multicast_group_ssm (InetAddress group, InetAddress? source_specific, string? iface) throws Error
- public bool leave_multicast_group (InetAddress group, bool source_specific, string? iface) throws Error
- public bool leave_multicast_group_ssm (InetAddress group, InetAddress? source_specific, string? iface) throws Error
- public bool listen () throws Error
- public ssize_t receive (uint8[] buffer, Cancellable? cancellable = null) throws Error
- public ssize_t receive_from (out SocketAddress address, uint8[] buffer, Cancellable? cancellable = null) throws Error
- public ssize_t receive_message (out SocketAddress? address, InputVector[] vectors, out SocketControlMessage[]? messages, ref int flags, Cancellable? cancellable = null) throws Error
- public int receive_messages (InputMessage[] messages, int flags, Cancellable? cancellable = null) throws Error
- public ssize_t receive_with_blocking (uint8[] buffer, bool blocking, Cancellable? cancellable = null) throws Error
- public ssize_t send (uint8[] buffer, Cancellable? cancellable = null) throws Error
- public ssize_t send_message (SocketAddress? address, OutputVector[] vectors, SocketControlMessage[]? messages, int flags, Cancellable? cancellable = null) throws Error
- public PollableReturn send_message_with_timeout (SocketAddress? address, OutputVector[] vectors, SocketControlMessage[]? messages, int flags, int64 timeout_us, out size_t bytes_written, Cancellable? cancellable = null) throws Error
- public int send_messages (OutputMessage[] messages, int flags, Cancellable? cancellable = null) throws Error
- public ssize_t send_to (SocketAddress? address, uint8[] buffer, Cancellable? cancellable = null) throws Error
- public ssize_t send_with_blocking (uint8[] buffer, bool blocking, Cancellable? cancellable = null) throws Error
- public void set_blocking (bool blocking)
- public void set_broadcast (bool broadcast)
- public void set_keepalive (bool keepalive)
- public void set_listen_backlog (int backlog)
- public void set_multicast_loopback (bool loopback)
- public void set_multicast_ttl (uint ttl)
- public bool set_option (int level, int optname, int value) throws Error
- public void set_timeout (uint timeout)
- public void set_ttl (uint ttl)
- public bool shutdown (bool shutdown_read, bool shutdown_write) throws Error
- public bool speaks_ipv4 ()
Inherited Members:
All known members inherited from class GLib.Object
All known members inherited from interface GLib.DatagramBased
All known members inherited from interface GLib.Initable