Sample code for 30+ languages & platforms
PowerBuilder

Move a Connection into Another Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeConnection, which moves the active connection from one Socket into another. The receiving object first releases any connection it previously held.

Background. Unlike TakeSocket, TakeConnection does not add a member to a socket set. After success, the source socket holds no connection. This is useful for handing an accepted connection to a dedicated handler object.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_ListenSock
oleobject loo_ConnectedSock
oleobject loo_HandlerSock

li_Success = 0

loo_ListenSock = create oleobject
li_rc = loo_ListenSock.ConnectToNewObject("Chilkat.Socket")
if li_rc < 0 then
    destroy loo_ListenSock
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_ListenSock.BindAndListen(5000,25)
if li_Success = 0 then
    Write-Debug loo_ListenSock.LastErrorText
    destroy loo_ListenSock
    return
end if

loo_ConnectedSock = create oleobject
li_rc = loo_ConnectedSock.ConnectToNewObject("Chilkat.Socket")

li_Success = loo_ListenSock.AcceptNext(20000,loo_ConnectedSock)
if li_Success = 0 then
    Write-Debug loo_ListenSock.LastErrorText
    destroy loo_ListenSock
    destroy loo_ConnectedSock
    return
end if

//  Move the active connection out of connectedSock and into another Socket object.  Unlike
//  TakeSocket, this does not add a member to a socket set.  After success, connectedSock holds no
//  connection.
loo_HandlerSock = create oleobject
li_rc = loo_HandlerSock.ConnectToNewObject("Chilkat.Socket")

li_Success = loo_HandlerSock.TakeConnection(loo_ConnectedSock)
if li_Success = 0 then
    Write-Debug loo_HandlerSock.LastErrorText
    destroy loo_ListenSock
    destroy loo_ConnectedSock
    destroy loo_HandlerSock
    return
end if

Write-Debug "Connection moved to the handler socket."


destroy loo_ListenSock
destroy loo_ConnectedSock
destroy loo_HandlerSock