ServerSocket

(Engine-Level Function)

Description: Returns a server WinSock socket stream given a handle returned by a SocketServerStart function or an integer error code.
Returns: Stream
Usage: Script Only.
Function Groups: Network,  Stream and Socket
Related to: ClientSocket | CloseStream | SocketAttribs | SocketServerEnd | SocketServerStart | SocketWait | SRead | SWrite | LargeSocketWrite
Format: ServerSocket(Handle)
Parameters:  
Handle
Required. Any numeric expression for the handle returned by the SocketServerStart function.
Comments: This function is used as part of a WinSocket-compliant server application. First, start a socket server using SocketServerStart. Then, use the SocketWait function as the trigger for an action script. Use this function in the script to connect a socket to the client application that triggered the SocketWait function. If the socket connection is lost (client shutdown) the stream is closed and set to a value of 0 (no error code returned).
If you experience difficulty with TCP/IP, a useful troubleshooting tool is the Windows™ diagnostic "NetStat.exe", used to display information about the network. Also of use is the Windows™ "Ping.exe" diagnostic which can be used to test the hardware connection. These files are normally found in the Windows™ directory. Consult the Windows™ documentation on their usage.

Example:

Init [
  If 1 Wait;
  [
    sHandle = SocketServerStart(0 { TCP/IP },
    20000{ Port number offered },
    1024 { Transmit buff length },
    1024 { Receiver buff length },
    1 { No transmit delay });
  ]
]
Wait [
  If SocketWait(sHandle) Main { Wait for client to connect };
  [
    server = ServerSocket(sHandle);
  ]
]
Main [
  If GetStreamLength(server) > 0 || MatchKeys(2, "r");
  [
    SRead(server, Concat("%", Concat(GetStreamLength(server),
    "c")), Data);
    SWrite(server, "%s", Data);
  ]
  { Always close socket when complete !!! }
  If WindowClose(Self());
  [
    CloseStream(server);
    SocketServerEnd(sHandle);
    Slay(Self(), 1);
  ]
  { Display data from client and server status }
  ZText(0, 50, Data, 0, 0);
  ZText(0, 100, Cond(ValueType(server) == 8 ,
  "Connected",
  "Not Connected"),
  10, 0);
]