Sample code for 30+ languages & platforms
PowerBuilder

Accept Connection on Socket

See more Socket/SSL/TLS Examples

Demonstrates how to create a TCP/IP socket, listen on a port, accept an incoming connection, and send a "Hello World" message to the client.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_ListenSocket
oleobject loo_ConnectedSocket

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

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

// Bind to a port and listen for incoming connections:
// This example will listen at port 5555 and allows for a backlog
// of 25 pending connection requests.
li_Success = loo_ListenSocket.BindAndListen(5555,25)
if li_Success = 0 then
    Write-Debug loo_ListenSocket.LastErrorText
    destroy loo_ListenSocket
    return
end if

// Get the next incoming connection
// Wait a maximum of 20 seconds (20000 millisec)
loo_ConnectedSocket = create oleobject
li_rc = loo_ConnectedSocket.ConnectToNewObject("Chilkat.Socket")

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

// Set maximum timeouts for reading an writing (in millisec)
loo_ConnectedSocket.MaxReadIdleMs = 10000
loo_ConnectedSocket.MaxSendIdleMs = 10000

// Send a "Hello World!" message to the client:
li_Success = loo_ConnectedSocket.SendString("Hello World!")
if li_Success <> 1 then
    Write-Debug loo_ConnectedSocket.LastErrorText
    destroy loo_ListenSocket
    destroy loo_ConnectedSocket
    return
end if

// Close the connection with the client.
// Wait a max of 20 seconds (20000 millsec)
li_Success = loo_ConnectedSocket.Close(20000)

Write-Debug "success!"


destroy loo_ListenSocket
destroy loo_ConnectedSocket