Sockets

Sockets

Postby reinaldocrespo » Fri Aug 25, 2006 1: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:

Code: Select all  Expand view
*-------------------------------------------------------------------------------------------------------------------------------
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
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Sockets

Postby Enrico Maria Giordano » Fri Aug 25, 2006 3:15 pm

Did you already tried my sample?

Code: Select all  Expand view
#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
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby reinaldocrespo » Fri Aug 25, 2006 4: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.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Postby reinaldocrespo » Fri Aug 25, 2006 9:33 pm

Enrico;

I made some minor modifications to your code:
Code: Select all  Expand view
#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
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

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

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

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

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

Postby Enrico Maria Giordano » Sat Aug 26, 2006 8: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
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 84 guests