Real Software Forums
http://forums.realsoftware.com/

EasyTCPsocket console application
http://forums.realsoftware.com/viewtopic.php?f=2&t=46336
Page 1 of 1

Author:  p0wn3d [ Mon Dec 24, 2012 7:24 am ]
Post subject:  EasyTCPsocket console application

I have a client/server desktop application using EasyTCPsocket client side.

I need to code a console version of the client but cannot find any examples. My desktop client currently has an super EasyTCPsocket class named mysocket and button on the main form.

My button connect code is:

mysocket.Address = "server.mydomain"
mysocket.Port = 22
mysocket.Connect


Mysockets connected event is

Connector.SendMessage( Val( "51" ), data )



My question is how would this be done as a console application the client also needs to handle receivedMessage events etc....

Any help is gratefully appreciated

Oh and Image everyone.....

Author:  timhare [ Mon Dec 24, 2012 1:01 pm ]
Post subject:  Re: EasyTCPsocket console application

In a ConsoleApplication, you have to provide your own event loop. This is the intended use of app.DoEvents. In the Run event of the ConsoleApplication, connect your socket and then loop:
mysocket.Address = "server.mydomain"
mysocket.Port = 22
mysocket.Connect

while True
app.DoEvents(50)
wend

Author:  p0wn3d [ Wed Dec 26, 2012 10:19 am ]
Post subject:  Re: EasyTCPsocket console application

:)

Tim thanks for you advice...

I still think I am doing something incorrect

What I have done is created a new console app project. In the project tab of the IDE I have added a class (type EasyTCPsocket) and named it mysocket.

Then in my APP run event I have added the code:

mysocket.Address = "server.mydomain"
mysocket.Port = 22
mysocket.Connect

while True
app.DoEvents(50)
wend


When I compile and run the project I get the error "This item does not exist mysocket.Address="server.mydomain"

I can only presume that i need to do things at runtime like maybe

dim mysocket as new EasyTCPSocket


:?:

Author:  timhare [ Wed Dec 26, 2012 1:53 pm ]
Post subject:  Re: EasyTCPsocket console application

Adding a class creates an object definition. Adding a property creates an object reference. You must create an object reference and instantiate the object before you can use it.

Add a class if you want to put code in its events, which you probably do.

Add a property of your class type to create a globally available reference. (Since this is a console app, DIM'ing a variable in the Run event is almost the same thing. So, your choice which way you go.)

Create a new object in the Run event before you try to use it.

Author:  msssltd [ Sat Dec 29, 2012 4:56 am ]
Post subject:  Re: EasyTCPsocket console application

You are pretty close to the solution.

Dim MySocket as new EasyTCPSocket
Creates an instance of an EasyTCPSocket class.
You can not populate the event handlers of a built in object in a console application, so it's pretty useless.

What you want to do is subclass the EasyTCPSocket Class, populate the events and then create an instance of this new class.

For example...
Add a new Class to the project in the project explorer.
Set the Class name to MyEasyTCPSocket
Set the super to EasyTCPSocket
Double click MyEasyTCPSocket
Populate the event handlers as follows

MyEasyTCPSocket.Connected
Print "Socket connected to " + me.Address
me.SendMessage( Val( "51" ), data )
End sub

MyEasyTCPSocket.Error
if me.LastErrorCode = 102 then
Print "Socket lost connection"
else
Print "Socket error " + cstr(me.LastErrorCode)
end if
end sub

MyEasyTCPSocket.DataAvailable
Print "Socket has " + cstr(me.BytesAvailable) + " bytes waiting"
end sub

Application.Run
Dim MySocket1 as new MyEasyTCPSocket
MySocket1.Address = "host.domain"
MySocket1.Port = 22

MySocket1.Connect
Do
MySocket1.Poll
Loop until MySocket1.IsConnected

While MySocket1.IsConnected
App.DoEvents
Wend
End sub


When writing console applications you need to think carefully about object persistence and scope.
For example...
MyEasyTCPSocket.Connected
//Scope error!
// Connector is not a part of the MyEasyTCPSocket subclass
Connector.SendMessage( Val( "51" ), data )
End sub

Author:  p0wn3d [ Mon Dec 31, 2012 10:44 am ]
Post subject:  Re: EasyTCPsocket console application

:) All working now thanks to you msssltd

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/