ServerSocket

Description:              This function returns a server WinSock socket stream given a handle returned by a SocketServerStart function or an integer error code.

Returns:                    Stream

Usage:                   Script

Format:                      ServerSocket(Handle)

Parameters:             Handle    { numeric }  { required }  { no default: }

                                                Any numeric expression for the handle returned by the SocketServerStart function.

Comments:               This function is used to implement 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 in implementing 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, 15, 0);

  ZText(0, 100, Cond(ValueType(server) == 8 ,

  "Connected", 

  "Not Connected"), 

  10, 0); 

]

See Also:

ClientSocket | CloseStream | SocketAttribs | SocketServerEnd | SocketServerStart | SocketWait | SRead | SWrite | TCPIPReset