WinSockets

Visitors counter:

 

Function reference:

Function name

Library

Description

CloseSocket

wsock32.dll

Close opened socket

WSASturtup

wsock32.dll

Initialize socket mechanizm

WSACleanup

wsock32.dll

free resources bused by windows socket mechanizm

inet_ntoa

wsock32.dll

Convert IP address into readable text

Connect

wsock32.dll

Connect to the socket

Send

wsock32.dll

Send data via socket

Recv

wsock32.dll

Read data from socket

Socket

wsock32.dll

Create socket

htons

wsock32.dll

converts host byte order into network byte order

CopyMemory

kernel32.dll

Function used to copy memory from pointer to structure

GetHostByName

wsock32.dll

Get information about komputer

WSAGetLastError

wsock32.dll

Get last error that raise during using socket mechanizm

 

 

 

CloseSocket
//close socket opened previously by socket function
Function long Closesocket(Long s ) Library"wsock32.dll"

WSAStartup
//initialize Windows Socket windows mechanizm
Function long WSAStartup(Integer wVersionRequested, ref WSADATA lpWSAData ) Library "wsock32.dll"

type WSADATA from structure
integer wversion
integer whighversion
character szdescription[257]
character szsystemstatus[129]
long imaxsockets
long imaxudpdg
long lpvendorinfo
end type
 


WSACleanup
//free the resources used by Windows Socket mechanizm
Function long WSACleanup() Library "wsock32.dll"

Inet_Ntoa
//converts IP address into readable text
Function long inet_ntoa(long inaddr) Library "wsock32.dll"

Connect
//establishes connection via socket
Function long connectA(long s , sockaddr name , long namelen ) Library "wsock32.dll" alias for "connect"

type SOCKADDR from structure
integer sin_family
integer sin_port
long sin_addr
character sin_zero[8]
end type

Send
//send data via opened socket
Function long send(Long ByVal, string buf, long length, long flags ) Library "wsock32.dll"

Recv
//received data from socket
Function long recv(long s, ref String buf, long length, long flags) Library "wsock32.dll"

Socket
//create socket
Function long socket(long af , long prototype , long protocol) Library "wsock32.dll"

Htons
//converts host byte order into network byte order
Function integer htons(integer hostshort ) Library "wsock32.dll"

CopyMemory
//use function to copy data from pointer to structure
Subroutine CopyMemory(ref HOSTENT Destination , long Source , long Length) Library "kernel32.dll" Alias for "RtlMoveMemory"
Subroutine CopyMemory(ref long destination, long Source , long Length) Library "kernel32.dll" Alias for "RtlMoveMemory"

type HOSTENT from structure
long h_name
long h_aliases
integer h_addrtype
integer h_length
long h_addr_list
end type

GetHostByName
//get information about komputer
Function long gethostbyname(string name ) Library "wsock32.dll"

WSAGetLastError
//.get last error
Function long WSAGetLastError() Library "wsock32.dll"

GetHostName
//get host name
Function long GetHostName(ref String name , ref long namelen ) Library "wsock32.dll"

Htonl
//converts value from host byte order to network byte order
Function long htonl(long hostlong) Library "wsock32.dll"

Inet_Addr
//converts an IP string to IP address in network byte order
Function long inet_addr(String cp) Library "wsock32.dll"

IoctlSocket
//manipulate input/output mode of the socket
Function long ioctlsocket(Long s , long cmd , long argp ) Library "wsock32.dll"

 

Example (connecting to web server):

long ll_ret, ll_socket
wsadata lstr_sockinfo
sockaddr lstr_sockaddr
HOSTENT  lstr_hostent

//Begin a Winsock session
ll_ret = WSAStartup(514, lstr_sockinfo)
If ll_ret <> 0 Then
MessageBox("","Unable to initialize Winsock! --")
return
End If

long ll_hostinfo
ll_hostinfo = GetHostByName("www.onet.pl")
if ll_hostinfo = 0 then
MessageBox("","invalid host")
return
end if

CopyMemory( lstr_hostent,  ll_hostinfo, 16 )


//open socket
Constant long INVALID_SOCKET = 4294967295

ll_socket = socket(2, 0, 0)
if ll_socket = INVALID_SOCKET then
MessageBox("", "Error in opening socket")
end if

//connect to socket
// Use Internet Protocol (IP)
lstr_sockaddr.sin_family = 2
//Connect to port 80.
lstr_sockaddr.sin_port = htons(80)

long pipaddress, ipAddress, tmp
tmp = lstr_hostent.h_addr_list
//Get the server's IP address out of the structure.
CopyMemory( pIPAddress, tmp, 4 )
CopyMemory( ipAddress, pIPAddress, 4)

//Connect to this IP address.
lstr_sockaddr.sin_addr = ipAddress
//Padding characters.
lstr_sockaddr.sin_zero = space(8)

ll_ret = connectA(ll_socket, lstr_sockaddr, 16)
If ll_ret <> 0 Then
MessageBox("","cannot connect "+ String(WSAGetLastError()))
End If

// Send an HTTP/GET request for the /index.html file.
string buffer
buffer = "GET / HTTP/1.1 Host: www.onet.pl User-Agent: HTTP-Test-Program"
ll_ret = send(ll_socket, buffer, Len(buffer), 0)

//read from socket
buffer = Space(1024)
do
ll_ret = recv(ll_socket, buffer, Len(buffer), 0)
loop Until ll_ret = 0

//closesocket( ll_socket)
//WSACleanup()
 


[Home] [Performance] [PB Tricks] [Resume] [Galery] [Contact] [Links]