# zPocketScript Class Reference: Socket

## Prototype: File

## Library: libjs_net

The Socket object provides encapsulation of the socket's API.
eg.
`var connection=new Socket(Socket.PF_INET,Socket.SOCK_STREAM);`

As the prototype chain for Socket objects includes the File object, you can Socket's inherit all methods from the File object. As with a file, a Socket object is closed when you delete it.

eg.
`// resolve the address of Craig's website
var serverAddress=new Host("angela1.data-uncertain.co.uk");
if(serverAddress)
{
// got the address - let's create a socket
s=new Socket(Socket.PF_INET,Socket.SOCK_STREAM);
// now, let's connect the socket to port 80 on Craig's server...
connected=s.connect(serverAddress,80);
if(connected)
{
// success - let's make an HTTP request
// - note that writeln() and readString() are inherited from the File object
s.writeln("GET / HTTP/1.0");
s.writeln("");
response=s.readString(10000);
}else{
// failure :(
}
// all done, delete the socket, so it get's closed
delete s;
}
delete serverAddress;
`

NOTE:The Socket object is a seperate plugin library, not part of the core library.

## Methods

- Socket(int domain, int type) - construct a new socket.
  Parameters:
  - domain - socket domain. Currently one of:
    - Socket.PF_LOCAL - local/unix domain socket
    - Socket.PF_INET - internet domain socket
  - type - socket type. Currently one of:
    - Socket.SOCK_STREAM - TCP socket
    - Socket.SOCK_DGRAM - UDP socket

- connect(Host host,int port) - connect a socket to a remote(?) host.
  Parameters:
  - host - a Host object for the machine you wish to connect to.
  - port - port number to connect to on the remote machine.
  Returns:
  - true or 1 : success (connection established to host:port)
  - false or 0 : fail (connection failled)
  eg.
  `var s=new Socket(Socket.PF_INET,Socket.SOCK_STREAM);
  var serverAddress=new Host("angela1.data-uncertain.co.uk",80);
  var connected=s.connect(serverAddress);
  `

## Properties

## Constants

- PF_LOCAL
- PF_INET
- SOCK_STREAM
- SOCK_DGRAM