First attempt with Winsock2, failed by the way

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

First attempt with Winsock2, failed by the way

Postby Julcar » Apr 07, 2021 8:40

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: 708
Joined: May 05, 2017 19:59
Location: Germany

Re: First attempt with Winsock2, failed by the way

Postby UEZ » Apr 07, 2021 11:10

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: 1649
Joined: Feb 26, 2007 5:32

Re: First attempt with Winsock2, failed by the way

Postby caseih » Apr 07, 2021 15:11

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: 90
Joined: Oct 19, 2010 18:52
Contact:

Re: First attempt with Winsock2, failed by the way

Postby Julcar » Apr 07, 2021 20:27

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: 90
Joined: Oct 19, 2010 18:52
Contact:

Re: First attempt with Winsock2, failed by the way

Postby Julcar » Apr 07, 2021 20:34

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: 1649
Joined: Feb 26, 2007 5:32

Re: First attempt with Winsock2, failed by the way

Postby caseih » Apr 08, 2021 0:10

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.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 8 guests