PDA

Click to See Complete Forum and Search --> : Problem In Winsock Connection


ishrar
Mar 18th, 2007, 01:36 AM
Hello friends I have wrote some code for connecting through winsock to send data, but there is problem in connecting, whenever i connect it is connected for 1 second after that its state is clossing. I dont how this problem occures.
So please try to help me




Private Sub cmdConn_Click()
If Not txtPort = "" Or Not txtIp.Text = "" Then

wskSend.Close
DoEvents
wskSend.RemoteHost = txtIp.Text
wskSend.RemotePort = Val(txtPort.Text)
'MsgBox wskSend.State
wskSend.Connect
'MsgBox wskSend.State
End If
End Sub

Private Sub cmdSend_Click()
On Error GoTo sol
Label4.Caption = wskSend.State
If Not wskSend.State = 7 Then
wskSend.Connect
End If
wskSend.SendData txtMsg

Exit Sub
sol:
MsgBox Err.Description
End Sub

'Close connection button
Private Sub Command1_Click()
wskSend.Close
End Sub

Private Sub Form_Load()
wskSend.Protocol = sckTCPProtocol
End Sub

Private Sub Timer1_Timer()
If wskSend.State = 6 Then
Label4.Caption = "Connecting"
ElseIf wskSend.State = 7 Then
Label4.Caption = "Connected"
MsgBox "connected"
ElseIf wskSend.State = 0 Then
Label4.Caption = "Closed"
ElseIf wskSend.State = 8 Then
Label4.Caption = "Clossing"
'MsgBox "closing"
'wskSend.Close
ElseIf wskSend.State = 9 Then
Label4.Caption = "Error"
ElseIf wskSend.State = 2 Then
Label4.Caption = "Listening"
ElseIf wskSend.State = 1 Then
Label4.Caption = "Open"
ElseIf wskSend.State = 7 Then
Label4.Caption = "Connected"
End If
End Sub

Private Sub wskSend_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Error in connection" & Number & Description
End Sub

Private Sub wskSend_SendComplete()
Label4.Caption = "data sent"
End Sub

Private Sub wskSend_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
Label4.Caption = "sending"
End Sub

opus
Mar 18th, 2007, 05:39 AM
I don't see a problem in the client code, show us your server.

BTW doesn't the .Connect method require the RemoteHost and RemotePort?

ishrar
Mar 19th, 2007, 02:34 AM
Yes,
Let me know have you checked it by runing it or not.

opus
Mar 19th, 2007, 02:39 AM
Without your servercode?
By the look at your client code I found no problems, and therefore I'm asking to see your server code!

ishrar
Mar 19th, 2007, 03:05 AM
Private Sub cmdListen_Click()
wskReceive.Close
Label2 = wskReceive.State
wskReceive.LocalPort = Val(txtPortNo)
wskReceive.Listen
If wskReceive.State = 1 Then
cmdListen.Caption = "Listening"
txtMsg.Text = "Listening" & vbNewLine
Else
'cmdListen.Caption = "Connecting"
End If
End Sub

'close connection......
Private Sub Command1_Click()
wskReceive.Close
End Sub

Private Sub Timer1_Timer()
If wskReceive.State = 6 Then
Label2.Caption = "Connecting"
ElseIf wskReceive.State = 7 Then
Label2.Caption = "Connected"
ElseIf wskReceive.State = 0 Then
Label2.Caption = "Closed"
ElseIf wskReceive.State = 8 Then
Label2.Caption = "Clossing"
wskReceive.Connect
ElseIf wskReceive.State = 9 Then
Label2.Caption = "Error"
ElseIf wskReceive.State = 2 Then
Label2.Caption = "Listening"
ElseIf wskReceive.State = 1 Then
Label2.Caption = "Open"
ElseIf wskReceive.State = 7 Then
Label2.Caption = "Connected"
End If
End Sub

Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
'MsgBox "Request received from client"
If Not wskReceive.State = 2 Then wskReceive.Accept requestID Else Label2.Caption = "Listening"


End Sub

Private Sub wskReceive_DataArrival(ByVal bytesTotal As Long)
wskReceive.GetData txtMsg.Text, vbString
End Sub

ishrar
Mar 19th, 2007, 03:06 AM
Now say what is wrong>>>>>>>>>

opus
Mar 19th, 2007, 08:40 AM
In your cmdConn_Click (Client) I changed:

wskSend.Connect

to:

wskSend.Connect wskSend.Remotehost, wsksend.RemotePort

(this one might not be neccessary)
In your wskReceive_ConnectionRequest (Server) I changed:

Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
'MsgBox "Request received from client"
If wskReceive.State <> sckClosed Then wskReceive.Close
wskReceive.Accept requestID
Label2.Caption = "Listening"
End Sub

In your wskReceive_DataArrival (Server) I changed:

Private Sub wskReceive_DataArrival(ByVal bytesTotal As Long)
Dim Stream as String
wskReceive.GetData Stream
txtMsg.Text= Stream
End Sub

You should be able to send from clinet to server using those changes.

ishrar
Mar 19th, 2007, 02:31 PM
fantastic man.....
Very good exlnt.
It is working properly, but i m eager to know what was mstk in my prog.
while line was wrong...
I want to know where i ws wrong?
Plz tell
thnk you very much:wave:

opus
Mar 20th, 2007, 01:17 AM
You had:

Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
'MsgBox "Request received from client"
If Not wskReceive.State = 2 Then wskReceive.Accept requestID Else Label2.Caption = "Listening"
End Sub

Your code did the .Accept only when the state of wskReceive was NOT listening (=2), but the connection shouldbe listening to get the ConnectionRequest!

When this Event fires wskReceive is in the .Listening State (=2). In order to use this Winsock, I would reset the connection (using .Close). For applications with more connections, you should revert to a another Winsock to do the actual connect and keep the "listening" connection in the listening-state.
First, .State=2 is the Listening-State.

The reason for the change in the DataArrival Sub isn't obvious to me either, but this way it works!

BTW: Ratings are welcome!

ishrar
Mar 21st, 2007, 05:13 AM
You had:

Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
'MsgBox "Request received from client"
If Not wskReceive.State = 2 Then wskReceive.Accept requestID Else Label2.Caption = "Listening"
End Sub

Your code did the .Accept only when the state of wskReceive was NOT listening (=2), but the connection shouldbe listening to get the ConnectionRequest!

When this Event fires wskReceive is in the .Listening State (=2). In order to use this Winsock, I would reset the connection (using .Close). For applications with more connections, you should revert to a another Winsock to do the actual connect and keep the "listening" connection in the listening-state.
First, .State=2 is the Listening-State.

The reason for the change in the DataArrival Sub isn't obvious to me either, but this way it works!

BTW: Ratings are welcome!
Thank you very muchh

Al42
Mar 21st, 2007, 12:44 PM
First, .State=2 is the Listening-State.Which is why it's better to use constants. It makes the code much easier to debug.

sckClosed 0
sckOpen 1
sckListening 2
sckConnectionPending 3
sckResolvingHost 4
sckHostResolved 5
sckConnecting 6
sckConnected 7
sckClosing 8
sckError 9