First attempt with Winsock2, failed by the way

New to FreeBASIC? Post your questions here.
Post Reply
Julcar
Posts: 141
Joined: Oct 19, 2010 18:52
Contact:

First attempt with Winsock2, failed by the way

Post by Julcar »

Code: Select all

#INCLUDE "win/winsock2.bi"

DIM WinSockData AS LPWSADATA, RetVal AS INTEGER
RetVal = WSAStartup(WINSOCK_VERSION, WinSockData)
IF RetVal > 0 THEN
  PRINT RetVal
END IF

Code: Select all

fbc -m test -x .\test.exe test.bas && test
This little code snippet sends the output 10014 which of course is WSAEFAULT error code. The C/C++ examples on the net has mostly the same code to init windows sockets streams, so I don't know why this fails.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: First attempt with Winsock2, failed by the way

Post by UEZ »

Try this:

Code: Select all

#INCLUDE "win/winsock2.bi"

DIM As WSADATA WinSockData
DIM RetVal AS Integer
RetVal = WSAStartup(WINSOCK_VERSION, @WinSockData)
IF RetVal THEN
  PRINT "Error: " & RetVal
ELSE
  PRINT "No Error"
END If
WSACleanup()
SLEEP
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: First attempt with Winsock2, failed by the way

Post by caseih »

Julcar wrote:This little code snippet sends the output 10014 which of course is WSAEFAULT error code. The C/C++ examples on the net has mostly the same code to init windows sockets streams, so I don't know why this fails.
It fails because you declared a WinSockData variable as a pointer type, but didn't point it at anything. I believe FB initializes it to zero by default. So WSAStartup is getting a null pointer and returning an error. Double check the C++ example code. I suspect you'll find it's more like @UEZ's example. You allocate the structure, pass a pointer to that structure, and WSAStartup fills in the structure.
Julcar
Posts: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: First attempt with Winsock2, failed by the way

Post by Julcar »

Something is wrong here:

Code: Select all

#INCLUDE "win/winsock2.bi"

DIM WinSockData AS LPWSADATA, RetVal AS INTEGER
DIM AS LPWSADATA WinSockData2
DIM AS INTEGER RetVal2
RetVal = WSAStartup(WINSOCK_VERSION, WinSockData)
IF RetVal > 0 THEN
  PRINT "WinSockData value: " & VarPtr(WinSockData)
  PRINT RetVal
END IF
RetVal2 = WSAStartup(WINSOCK_VERSION, WinSockData2)
IF RetVal2 > 0 THEN
  PRINT "WinSockData2 value: " & VarPtr(WinSockData2)
  PRINT RetVal2
END IF

Code: Select all

WinSockData value: 1244912
 10014
WinSockData2 value: 1244904
 10014
Both variables are initialized with a value higher than zero, but WSAStartup still returns WSAEFAULT
Julcar
Posts: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: First attempt with Winsock2, failed by the way

Post by Julcar »

UEZ wrote:Try this:

Code: Select all

#INCLUDE "win/winsock2.bi"

DIM As WSADATA WinSockData
DIM RetVal AS Integer
RetVal = WSAStartup(WINSOCK_VERSION, @WinSockData)
IF RetVal THEN
  PRINT "Error: " & RetVal
ELSE
  PRINT "No Error"
END If
WSACleanup()
SLEEP
Ohhh I got it!

in winsock2.bi the WSAStartup is declared in this way

Code: Select all

declare function WSAStartup(byval wVersionRequested as WORD, byval lpWSAData as LPWSADATA) as long
That's why I declared WinSockData as LPWSADATA, but looking further in the include file here is the answer

Code: Select all

type LPWSADATA as WSADATA ptr
I removed the LP heading and now it works.

Code: Select all

#INCLUDE "win/winsock2.bi"

DIM WinSockData AS WSADATA, RetVal AS INTEGER
RetVal = WSAStartup(WINSOCK_VERSION, VarPtr(WinSockData))
IF RetVal > 0 THEN
  PRINT "WinSockData value: " & VarPtr(WinSockData)
  PRINT RetVal
END IF
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: First attempt with Winsock2, failed by the way

Post by caseih »

Yes every once in a while hungarian notation is actually useful. LP means "long pointer"
Julcar wrote:

Code: Select all

 PRINT "WinSockData value: " & VarPtr(WinSockData)
By the way, what you were printing out was a pointer to a (null) pointer.
Post Reply