Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock RemoteHost problem

Status
Not open for further replies.

shawnnason

Programmer
Jun 12, 2001
26
US
I am trying to write an application that will connect to different computer at run time by specifiying, in a text ox, the name of the computerto which I want to connect. I am using Winsock for this. I know that I can use the "friendly" name or the dotted noation. I have my form load do, among other things this:

Code:
tcpClient.RemoteHost = txtConnectToName.text
[\code]

with txtConnectToName set at desing time with a default value.  I then check at run time what the value of txtConnectToName is via a msgbox, and I get the new name.  However, when I get to the next line, which is again the RemoteHost call, I get an error:

[code]
MsgBox (txtConnectToName.text)

tcpClient.RemoteHost = txtConnectToName.text
[\code]

The error is:

Run Time Error 40020:
Invalid Operation at current state

I am guessing that I need to do something to the Winsock tcpClient first, but what?

- Shawn
 
When you say you are doing a 'RemoteHost call' do you mean: tcpClient.Connect ???

 
When I say RemoteHost, I mean that I set the host that I am connecting to. My subsa look like this:

Form Load:

Code:
Private Sub Form_Load()

' The name of the Winsock control is tcpClient.
' The Winsock control is embedded in the form.
' Note: to specify a remote host, you can use
' either the IP address (ex: "121.111.1.1") or
' the computer's "friendly" name.
' Invoke the Connect method to initiate a connection.
    ' See cmdConnect_Click for usage of Connect method
' randomize numbers
' set sv_SDNUM
' display sv_SDNUM
' set sv_STATE to 0 (idle, not awaiting anything)
' set sv_STATUS to -1 (not master or slave yet)

tcpClient.RemoteHost = txtConnectToName.Text
tcpClient.RemotePort = 1001
Randomize
sv_SDNUM = (Rnd * 2 ^ 24)
txtsvTT.Text = sv_SDNUM
sv_STATE = 0
sv_STATUS = -1
tcpClient.Listen

End Sub
[\code]

Connect button:
[code]

Private Sub cmdConnect_Click()

MsgBox (txtConnectToName.Text)

***ERROR IN THIS LINE*** 
'tcpClient.RemoteHost = txtConnectToName.Text

' Invoke the Connect method to initiate a connection.

' IF this one is listening THEN stop listening
If tcpClient.State = 2 Then _
    tcpClient.Close

' see who you are being asked to connect to
' IF state is 0 (connection closed), which it should be, THEN
'   connect to the guy you want
'   disable the connect button
'   enable the disconnect/close button
' ENDIF
' clear both text windows
' update connection status

txtConTo.Text = tcpClient.RemoteHost
    
If tcpClient.State = 0 Then
    tcpClient.Connect
    cmdConnect.Enabled = False
    cmdClose.Enabled = True
End If

txtSendData.Text = ""
txtOutput.Text = ""

Status_Caption

End Sub
[\code]
 
In the form load you have tcpClient.Listen, this sets the socket to a state of listening. In this state you cannot change properties like the RemoteHost or RemoteHost. These values can only be changed when the socket is closed.

In the cmdConnect_Click() event you have the line tcpClient.RemoteHost = txtConnectToName.Text the socket will not like this because it is listening.

You might want to think about exactly what you're trying to achieve from this application (ie is it going to listen for a connection, or are you going to explicitly connect to the other machine). Either way your best bet will be to remove the tcpClient.Listen from the form load.

Madlarry


 
MadLarry -

I want to be ready to listen as startup, but if nothing happens and I want to connect, then I will close down the socket and reopen with a connect call. So, it loos like all I need to do is move the tcpClient.RemoteHost to after I close and it should work.

- Shawn
 
Have you consider to use a Winsock array ? with the index value you can have several sockets with the same name performing different operations.
 
SIRLUIS -

I've seen references to Winsock arrays, but have never really thought about doing that. Can you give me an idea of how I'd make and use such an array? I am guessing that I'd declare the array, make some kind of counter, and use that counter to reference the member of the array I want. What about RemotePort and RemoteHost - do they have to be different for each iteration of the array, or can more than one Winsock connect to the same port and/or same host? It would seem that using the same host wouldn't be a problem, but using the same port might be.

- Shawn
 
Hi Shawn!
The reason is that a connection can connect only 0nce. and unless you close the connection it remains open. so try to close the connection and open it. hope it will work out.

rgds.

Sarma
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top