Reference Guide > S Routines > SOCKET_INIT Function
  

SOCKET_INIT Function
Binds a socket to a specified port that is designated to listen for client connections.
Usage
socket = SOCKET_INIT(port)
Input Parameters
port—The port number on which the server will listen. You can call SOCKET_INIT with a port number of 0 (zero), which lets your operating system pick an available port.
Keywords
None.
Returned Value
socket—On success, returns a socket handle; on failure, returns one of the following values:
*–2—Error creating the socket.
*–3—Error binding the socket.
*–4—Error on the listen() command.
Discussion
You must call this routine in a server program to specify which port to listen on for client connections.
The port number is usually a standard, or previously agreed upon, port that clients use to contact a server. For example, by convention, most Web servers listen for connections on port 80. Most hosts have ports numbered between 1 and 65,535. Typically, ports 0 to 1024 are reserved and only available to someone with administrator or super user privileges.
The returned socket handle is used by other SOCKET_* routines to identify a particular socket connection.
It is good practice to close the socket handle when you are finished with it.
Example
In this simple server program, a socket is bound to port 1500, which is the port that the client program uses to contact the server.
PRO SERVER
   port = 1500
   socket = SOCKET_INIT(port)
   connection = SOCKET_ACCEPT(socket)
   data = BYTARR(15)
   nbytes = SOCKET_READ(connection,data)
   PRINT, 'SERVER received: ', STRING(data)
   SOCKET_CLOSE, connection
   SOCKET_CLOSE, socket 
END
See Also
SOCKET_ACCEPT, SOCKET_CLOSE, SOCKET_GETPORT
For more detailed information on using the socket routines, see the PV‑WAVE Application Developer’s Guide.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.