FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Sockets
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Sockets
Posted: Fri Aug 25, 2006 01:55 PM
Hi.

I'm trying to connect to a socket where I'm expected to send some data but failing to connect. Here is the code I'm using:

*-------------------------------------------------------------------------------------------------------------------------------
METHOD SetupIpSocket() CLASS TEXPORTER

	::oSocket := TSocket():New( ::nSocket )
	::oSocket:bConnect = { || ::isConnected := .t. }
	::oSocket:Connect( ::cIp )

RETURN ::isConnected


::isconnected is initialized as .f. After executing these lines, it stays as .f., thus no connection is happening.

I tested the ip and port using telnet (telnet ip port) and I noticed connection, therefore I presume that the port is up and accepting connection at that given ip. Is there any other tool that I can use to troubleshoot this issue? Why is it not connecting using my source?

thank you,


Reinaldo.
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Sockets
Posted: Fri Aug 25, 2006 03:15 PM
Did you already tried my sample?

#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL oWnd

    DEFINE WINDOW oWnd

    @ 1, 2 BUTTON "Send";
           SIZE 100, 50;
           ACTION SENDDATA( 2000, "127.0.0.1", "This is a test." )

    ACTIVATE WINDOW oWnd

    RETURN NIL


STATIC FUNCTION SENDDATA( nPort, cIP, cMsg )

    LOCAL oSocket := TSocket():New( nPort )

    oSocket:bConnect := { || oSocket:SendData( "MSG " + cMsg ),;
                             oSocket:End() }

    oSocket:Connect( cIP )

    RETURN NIL


EMG
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Sockets
Posted: Fri Aug 25, 2006 04:24 PM

enrico,

no, i did not. sorry. you are right, i will as soon as i get back to the office. i'll keep you posted on the results.

thank you.

rc.

Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Sockets
Posted: Fri Aug 25, 2006 09:33 PM
Enrico;

I made some minor modifications to your code:
#include "Fivewin.ch"
static isconnected := .f.

FUNCTION MAIN()

    LOCAL oWnd

    DEFINE WINDOW oWnd

    @ 1, 2 BUTTON "Send";
           SIZE 100, 50;
           ACTION ( SENDDATA( 15000, "98.19.1.4", "This is a test" ), ;
			MsgInfo( isconnected ), ;
			isconnected := .f. )

    ACTIVATE WINDOW oWnd

    RETURN NIL


STATIC FUNCTION SENDDATA( nPort, cIP, cMsg )

    LOCAL oSocket := TSocket():New( nPort )

    oSocket:bConnect := { || oSocket:SendData( "MSG " + cMsg ),;
					isconnected := .t. ,;
                             oSocket:End() }

    oSocket:Connect( cIP )

    RETURN NIL


Please test on your system. Unless you actually have a computer with ip 98.19.1.4 answering on port 15000, you are supposed to get .f. everytime --right?

The first time you click on the send button, you get .f. on the msg window. But if you wait 30 seconds before you click on it again, you'll get .t.. And if you click every 30 seconds or more you will always get .t..

Test it. Unless I'm missing something obvious, something is not working right with the tsocket class.

Reinaldo
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Sockets
Posted: Fri Aug 25, 2006 10:11 PM

SendData() method has a timeout and then the execution continues with the next statement. Therefore, after the timeout, isconnected is alway set to true.

EMG

Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Sockets
Posted: Fri Aug 25, 2006 10:29 PM

But isn't bconnect supposed to execute if and only if a socket connection is succesful?

At any rate, I'm testing this code on a network where I do have a server with that IP and answering on that port. I've tested from a command prompt using <telnet ip port> and I see connection. Yet with this very same code I get .f. on the msginfo window.

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Sockets
Posted: Fri Aug 25, 2006 11:04 PM

This is normal. The execution immediately exit from SENDDATA() function and this is why you get isconnected set to false. Later, bConnect is evaluated and isconnected is set to true but you have already printed it (too soon).

EMG

Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Sockets
Posted: Fri Aug 25, 2006 11:15 PM

Ok, so what you are saying is that I just might be getting connected after all. How can I know if indeed I have a succesful connection?

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Sockets
Posted: Sat Aug 26, 2006 08:16 AM

I don't know, sorry. You may check the timeout. If bConnnect is evaluated after the timeout is expired then the connection is not valid.

Connect() method return value seems to be always -1 (SOCKET_ERROR), I don't know why.

EMG

Continue the discussion