Sockets

Post Reply
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Sockets

Post 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.
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Sockets

Post 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
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Post 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.
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Post 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
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Post 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.
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Post 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?
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
Post Reply