Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect Failure with Winsock 1

Status
Not open for further replies.

alan232

Programmer
May 19, 2004
144
0
0
US
Hi All;

I'm completely new to this. After placing the winsock ole
on a form, the following connection code consistently returns with a state "Error" of 9 even though Winsock is running on the remote host and I can ping it without difficulty:

WITH THISFORM.oleSock
IF .state != 0
.OBJECT.CLOSE()
ENDIF
.protocol = 0
.remoteHost = "192.168.1.101"
.remotePort = 0
.localPort = 0
.OBJECT.CONNECT()

ENDWITH

Any help is appreciated,
Thanks,
Alan

 
Oh;

I'm using VFP 6.0 SP5.

Thanks,
Alan
 
Hi All;

For anyone who is interested, I believe Markus Voellmy (with help), published WinSock OCXSample (Updated: 2005.12.29 08:49:02 AM) on the internet that solves most of my winsock questions. I highly recommend it to newbies like me.

Thanks Markus!
Alan
 
It looks like you aren't connecting to a remote port:

.remotePort = 0

Whatever your host app is, you need to connect to the port it uses. For instance, to connect to telnet, you would use .remotePort = 23.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Thanks Dave;

I had hoped that setting remoteport to 0 would have the same effect as setting the localport to 0--nope, I was wrong.

A star to you sir!
(By the way, any idea on how the server, recieving a connection request can determine the name --not just ip address--of the machine making the request? All my ip's are assigned by a router, so just getting the IP address does not tell me what or where the machine is.)

Thanks,
Alan
 
Thanks for the star!

As far as I know, the computer name isn't available or isn't passed to the server when it connects. You could send it from the client when you make a connection to the server, or you could use the following code from the server to grab the name using API calls:
Code:
*************************************************
*... FUNCTION gethostbyaddrfn
* 
* Gets computer name using IP address
*
* Usage: 
*   SET PROCEDURE TO gethostbyaddrfn
*   cCompName = gethostbyaddrfn("127.0.0.1")
*************************************************
PARAMETERS szaddr

STORE IIF(PARAMETERS() > 0, .T., .F.) TO lSingleIP  &&... command line ip
DO DECL

IF NOT InitWinsock()
   RETURN
ENDIF

#DEFINE AF_INET        2  && internetwork: UDP, TCP, etc.

LOCAL lnIP, lnHOSTENTptr, lcHOSTENT

   IF At(',', szaddr) > 0
      STORE Left(szaddr, At(',', szaddr) -1) TO xaddr
   ELSE 
      STORE szaddr TO xaddr
   ENDIF 
   
   lnIP = inet_addr(xaddr)
   ? "IP : " + xaddr

   lnHOSTENTptr = gethostbyaddr(@lnIP, 4, AF_INET)
   ipaddr = xaddr

   IF lnHOSTENTptr = 0
      ?? " Not found: ", WSAGetLastError()
      HOSTNAME = 'Not found'
   ELSE
      #DEFINE HOSTENT_SIZE  16
      lcHOSTENT = GetMemBuf (lnHOSTENTptr, HOSTENT_SIZE)
      ?? " Host name:", GetMemStr(buf2dword(SUBSTR(lcHOSTENT, 1,4)))
      HOSTNAME = GetMemStr(buf2dword(SUBSTR(lcHOSTENT, 1,4)))
   ENDIF


* terminating use of the Ws2_32.dll
= WSACleanup()
RETURN HOSTNAME 

FUNCTION  InitWinsock()
   * Initializing the Winsock service for the application
   #DEFINE WSADATA_SIZE  398
   #DEFINE WS_VERSION    514  && 0x0202
   LOCAL lcWSADATAln, lnInitResult
   lcWSADATA = REPLI(CHR(0), WSADATA_SIZE)
   lnInitResult = WSAStartup (WS_VERSION, @lcWSADATA)
   RETURN  (lnInitResult = 0)

FUNCTION  GetMemBuf (lnAddr, lnBufsize)
   * returning data from a memory block
   LOCAL lcBuffer
   lcBuffer = REPLI(CHR(0), lnBufsize)
   = Heap2Str (@lcBuffer, lnAddr, lnBufsize)
   RETURN  lcBuffer

FUNCTION  GetMemStr (lnAddr)
   * returning data from a memory block
   * before the first occurence of a zero byte
   LOCAL lcBuffer
   lcBuffer = GetMemBuf (lnAddr, 250)
   RETURN  SUBSTR(lcBuffer, 1, AT(CHR(0),lcBuffer)-1)

FUNCTION  buf2dword (lcBuffer)
   RETURN ASC(SUBSTR(lcBuffer, 1,1)) + ;
      ASC(SUBSTR(lcBuffer, 2,1)) * 256 +;
      ASC(SUBSTR(lcBuffer, 3,1)) * 65536 +;
      ASC(SUBSTR(lcBuffer, 4,1)) * 16777216

PROCEDURE  DECL
   DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING @lpWSAData
   DECLARE INTEGER WSACleanup IN ws2_32
   DECLARE INTEGER WSAGetLastError IN ws2_32

   DECLARE INTEGER gethostbyaddr IN ws2_32;
      INTEGER @ADDR, INTEGER LEN, INTEGER TYPE

   DECLARE RtlMoveMemory IN kernel32 AS Heap2Str;
      STRING @DEST, INTEGER Src, INTEGER nLength

   DECLARE INTEGER inet_addr IN ws2_32 STRING cp

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Hi Dave;

Tried the code. It works great right out of the box. (Which is a good thing since I have no idea what makes it tick!)

Thanks much!
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top