Well I have another problem. I seem to be able to have a client and server connect but I have it so that the moment it connects it sends data from the server to the client (i've tried both ways). It doesn't work. any suggestions?
winsockutil.bas
Code:
option escape
'$include: "win/winsock.bi"
declare function wsConnect (s as socket, host as string, port as integer)
declare function wsResolveHost ( hostname as string ) as integer
declare function wsGetData ( s as socket ) as string
declare function wsSendData ( s as socket, sendbuffer as string)
declare function wsInit (s as socket)
declare function wsKill (s as socket)
declare function wsListen (s as socket, port as integer)
declare function wsAccept (s as socket)
const RECVBUFFLEN = 8192
const NEWLINE = chr$(13)+chr$(10)'"\r\n"
function wsAccept(s as socket)
dim acceptsocket as socket
AcceptSocket = SOCKET_ERROR
while ( AcceptSocket = SOCKET_ERROR )
AcceptSocket = accept( s, 0, 0)
wend
end function
function wsListen (s as socket, port as integer)
dim sa as sockaddr_in
sa.sin_port = htons(port)
sa.sin_family = AF_INET
sa.sin_addr.S_addr = inet_addr( "127.0.0.1" )
bind( s, @sa, sizeof(sa) )
listen( s, 1 )
end function
function wsKill(s as socket)
shutdown(s, 2)
closesocket(s)
WSACleanup
end function
function wsInit(s as socket)
dim wsaData as WSAData
WSAStartup( MAKEWORD( 1, 1 ), @wsaData )
s = opensocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
end function
function wsSendData(s as socket, sendbuffer as string)
send(s, strptr(sendBuffer), len(sendBuffer),0)
end function
function wsGetData(s as socket) as string
dim recvbuffer as zstring * RECVBUFFLEN+1
dim bytes as integer
bytes = recv(s, strptr(recvBuffer), RECVBUFFLEN, 0)
recvbuffer[bytes] = 0
if recvbuffer = "" then
wsgetdata = trim$(str$(bytes))
else
wsgetData = recvbuffer
end if
end function
function wsGetAllData(s as socket) as string
dim recvbuffer as zstring * RECVBUFFLEN+1
dim bytes as integer
while (bytes <> socket_error)
bytes = recv(s, strptr(recvBuffer), RECVBUFFLEN, 0)
if ( bytes = 0 or bytes = WSAECONNRESET ) then
exit while
end if
wend
recvbuffer[bytes] = 0
if recvbuffer = "" then
wsgetalldata = trim$(str$(bytes))
else
wsgetalldata = recvBuffer
end if
end function
function wsResolveHost( hostname as string ) as integer
dim ia as in_addr
dim hostentry as hostent ptr
ia.S_addr = inet_addr( hostname )
if ( ia.S_addr = INADDR_NONE ) then
hostentry = gethostbyname( hostname )
if (hostentry = 0) then exit function
wsresolveHost = **hostentry->h_addr_list
else
wsresolveHost = ia.S_addr
end if
end function
function wsConnect(s as socket, host as string, port as integer)
dim connection as sockaddr_in
connection.sin_port = htons(port)
connection.sin_family = AF_INET
connection.sin_addr.S_addr = wsresolveHost(host)
connect(s, @connection, len(connection))
end function
winsockclient.bas
Code:
option explicit
option escape
#include "winsockutil.bas"
cls
dim s as socket
print "Initializing..."
wsInit(s)
print "Connecting..."
wsConnect(s,"127.0.0.1", 9090)
print "Recieving..."
dim moo as string
moo = wsGetallData(s)
print moo
print "Killing..."
wsKill(s)
sleep
winsockserver.bas
Code:
option explicit
option escape
#include "winsockutil.bas"
cls
dim s as socket
print "Initializing..."
wsInit(s)
print "Listening..."
wsListen (s, 9090)
print "Accepting..."
wsAccept (s)
print "Sending..."
wsSendData (s, "moo23wd")
print "Killing..."
wsKill(s)
sleep