RingSockets Extension¶
In this chapter we will learn about using the RingSockets extension.
TCP Server¶
Example (TCP Server Code):
load "sockets.ring"
sock = socket(AF_INET,SOCK_STREAM,0)
bind(sock,"127.0.0.1",5050)
listen(sock,5)
ns = accept(sock)
send(ns,"Hello Client")
msg = recv(ns,1024)
? "Client Say >> " + msg
close(ns)
close(sock)
? "socket connection closed"
TCP Client¶
Example (TCP Client Code):
load "sockets.ring"
sock = socket(AF_INET,SOCK_STREAM)
connect(sock,"127.0.0.1",5050)
send(sock,"Hello Server")
msg = recv(sock,1024)
? "Server Say >> " + msg
close(sock)
? "socket connection closed"
Functions¶
The next functions are provided by this extension
socket(nAddressFamily, nConnectionType) -> SocketHandle
bind(pSocketHandle, cHost , nPort)
listen(pSocketHandle, nBacklog)
accept(pSocketHandle) -> pConnectionHandle
send(pConnectionHandle|pSocketHandle,cMessage)
sendto(pSocketHandle,cMessage)
recv(pConnectionHandle|pSocketHandle,nBuffer) -> cData
recvfrom(pSocketHandle, nBuffer) -> cData
connect(pSocketHandle, cHost , nPort) -> return <0> if successful
close(pSocketHandle|pConnectionHandle)
gethostbyname(cHostName) -> cIPAddress
gethostbyaddr(cIPAddress) -> aListOfHostInfo
gethostname() -> cHostName
getservbyname(cName) -> nPort
getservbyport(nPort) -> cName
Example:
load "sockets.ring"
host = gethostbyname("google.com")
? host
line()
? gethostbyaddr(host)
line()
? gethostname()
line()
? getservbyname("ftp")
line()
? getservbyport(21)
func line ? copy("=",30)
Constants¶
Address Family:
AF_INET # mean use IPV4
AF_INET6 # ........ IPV6
Connection type:
SOCK_STREAM # mean use TCP Protocol
SOCK_DGRAM # ........ UDP ........