Page 1 of 1

Sockets

Posted: Fri Aug 25, 2006 1:55 pm
by reinaldocrespo
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:

Code: Select all

*-------------------------------------------------------------------------------------------------------------------------------
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.

Re: Sockets

Posted: Fri Aug 25, 2006 3:15 pm
by Enrico Maria Giordano
Did you already tried my sample?

Code: Select all

#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

Posted: Fri Aug 25, 2006 4:24 pm
by reinaldocrespo
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.

Posted: Fri Aug 25, 2006 9:33 pm
by reinaldocrespo
Enrico;

I made some minor modifications to your code:

Code: Select all

#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

Posted: Fri Aug 25, 2006 10:11 pm
by Enrico Maria Giordano
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

Posted: Fri Aug 25, 2006 10:29 pm
by reinaldocrespo
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.

Posted: Fri Aug 25, 2006 11:04 pm
by Enrico Maria Giordano
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

Posted: Fri Aug 25, 2006 11:15 pm
by reinaldocrespo
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?

Posted: Sat Aug 26, 2006 8:16 am
by Enrico Maria Giordano
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