Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A winsock question (don't worry more will come)
#1
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...
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#2
"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.
Reply
#3
yay thanks

I just got one prog to connect to another. hehe
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#4
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)
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#5
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
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#6
nvm It wasn't accepting properly
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)