A winsock question (don't worry more will come) - whitetiger0990 - 05-21-2005
It compiles fine but I get an illegal operation when running this:
Code: option explicit
#include "win/winsock.bi"
dim wsadata as lpwsadata, iResult as integer
iResult = WSAStartup(1.1, wsadata)
print iResult
sleep
I'm probably missing something huge >_> I havn't even gotten past the initiation...
A winsock question (don't worry more will come) - v3cz0r - 05-21-2005
"wsadata" must to be the struct, not the pointer to it. Pass it by doing @wsadata, after declaring it as "wsadata as WSAData", see examples/Windows/winsocktest.bas for more details - resolving the host to an IP can be really trick with Winsock.
A winsock question (don't worry more will come) - whitetiger0990 - 05-21-2005
yay thanks
I just got one prog to connect to another. hehe
A winsock question (don't worry more will come) - shiftLynx - 05-21-2005
Code: iResult = WSAStartup(1.1, wsadata)
Did this work? Has the FB port been made to accept a floating point argument as the first parameter or something? If it has, then cool, but as far as I know, you need to use the MAKEWORD macro if you're coding in C... so I would have thought you would be doing this:
Code: iResult = WSAStartup((1 shl 8) or 1, @wsadata)
A winsock question (don't worry more will come) - whitetiger0990 - 05-21-2005
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.basCode: 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.basCode: 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.basCode: 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
A winsock question (don't worry more will come) - whitetiger0990 - 05-22-2005
nvm It wasn't accepting properly
|