By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,057 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,057 IT Pros & Developers. It's quick & easy.

Winsock Problem

100+
P: 108
Hi,

Below is my simple code regarding socket connection and currently I'm encountering problem (I cannot connect to the remote pc). I have created a similar program which I created in VB.Net which I run successfully. I'm trying to figure out what is the error. Hope you can help me guys.

Expand|Select|Wrap|Line Numbers
  1. Private Sub SendText(ByVal aIpAdd As String, ByVal aMes As String)
  2.  
  3.     'Dim iport As String
  4.  
  5.     'Set port number to 4003
  6.     'iport = 4003
  7.  
  8.     'Start socket connection
  9.     If MySock.State = sckClosed Then
  10.  
  11.         'set server address
  12.         MySock.RemoteHost = aIpAdd
  13.  
  14.         'set server port
  15.         MySock.RemotePort = 4003
  16.  
  17.         MySock.Protocol = sckTCPProtocol
  18.  
  19.         'try to connect
  20.         MySock.Connect
  21.         If MySock.State = sckConnected Then 
  22.             'send data
  23.             MySock.SendData aMes
  24.             MsgBox (aMes)
  25.             'Sleep 50
  26.             MySock.Close
  27.         End If
  28.  
  29.  
  30.     End If
  31.  
  32. End Sub
Nov 29 '07 #1
Share this Question
Share on Google+
4 Replies


QVeen72
Expert 100+
P: 1,445
Hi,

After looking into your code,
There is a first "IF" condition which does the action(sendmsg) only if the WinSocket is closed..
may be you can check this code"

Expand|Select|Wrap|Line Numbers
  1. Private Sub SendText(ByVal aIpAdd As String, ByVal aMes As String)
  2.     If MySock.State = sckClosed Then
  3.         MySock.RemoteHost = aIpAdd
  4.         MySock.RemotePort = 4003
  5.         MySock.Protocol = sckTCPProtocol
  6.         MySock.Connect
  7.    End If
  8.    If MySock.State = sckConnected Then 
  9.        MySock.SendData aMes
  10.        MsgBox (aMes)
  11.        'Sleep 50
  12.        MySock.Close
  13.    Else
  14.       'Write Code To Re-Connect and send...
  15.    End If   
  16. End Sub
  17.  
Use Code Tags, and Indent your Code properly to avoid any Confusion for Branch/loop conditions..


Regards
Veena
Nov 29 '07 #2

100+
P: 108
Hi,

After looking into your code,
There is a first "IF" condition which does the action(sendmsg) only if the WinSocket is closed..
may be you can check this code"

Expand|Select|Wrap|Line Numbers
  1. Private Sub SendText(ByVal aIpAdd As String, ByVal aMes As String)
  2.     If MySock.State = sckClosed Then
  3.         MySock.RemoteHost = aIpAdd
  4.         MySock.RemotePort = 4003
  5.         MySock.Protocol = sckTCPProtocol
  6.         MySock.Connect
  7.    End If
  8.    If MySock.State = sckConnected Then 
  9.        MySock.SendData aMes
  10.        MsgBox (aMes)
  11.        'Sleep 50
  12.        MySock.Close
  13.    Else
  14.       'Write Code To Re-Connect and send...
  15.    End If   
  16. End Sub
  17.  
Use Code Tags, and Indent your Code properly to avoid any Confusion for Branch/loop conditions..


Regards
Veena

Hi,

Sorry about that. I added reconnect portion and I got an error and after checking on my log, it says there that I was connected. But MySock.State return 6 and I dont know what is that value. Beside, I'm confuse with RemotePort and RemoteHost, what is the correct datatype for this two? I'm currently passing integer for RemotePort and string IP Address to RemoteHost. Aside from that there is RemoteHostIP and what is the difference between RemoteHost?
Nov 29 '07 #3

QVeen72
Expert 100+
P: 1,445
But MySock.State return 6 and I dont know what is that value. Beside, I'm confuse with RemotePort and RemoteHost, what is the correct datatype for this two? I'm currently passing integer for RemotePort and string IP Address to RemoteHost. Aside from that there is RemoteHostIP and what is the difference between RemoteHost?
State =6 is Still Connection , write "DoEvents" for this
RemoteHost is either ServerName Or TCPIP
RemotePort Read This

Regards
Veena
Nov 29 '07 #4

100+
P: 108
State =6 is Still Connection , write "DoEvents" for this
RemoteHost is either ServerName Or TCPIP
RemotePort Read This

Regards
Veena

[HTML]Private Sub MyClose()
MySock.Close
End Sub

Private Sub TryToConnect(ByVal aIpAdd)

MySock.RemoteHost = aIpAdd
MySock.RemotePort = 4003
MySock.Protocol = sckTCPProtocol
MySock.Connect

End Sub
Private Sub MySend(ByVal aIpAdd, ByVal aMes As String)

Call TryToConnect(aIpAdd)
Do
DoEvents
If MySock.State = 7 Then
MySock.SendData aMes
End If

If MySock.State = 9 Then
MsgBox ("No Connection")
End If

Loop Until MySock.State = 7 Or MySock.State = 9

Call MyClose

End Sub
[/HTML]


I added DoEvents and try to debug it but the state is always 6 until it terminated. About the RemoteHost, is it correct that I'm passing the IP Address in string format? Is there a way where in I can convert the STring IP Address to ServerName.
Nov 29 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.