VB Problems(Winsock Related)

dcpsoguy

Diamond Member
Nov 5, 2000
3,252
0
0
What is wrong with this code. I am trying to make a simple client-server. Heres the code for the client,and then I will show you the code for the server.


Client:

Private Sub Form_Load()
Dim server As String
Dim Port As String
server = "67.161.136.134"
Port = "5000"
Winsock1.Close
Winsock1.Connect server, Port
End Sub
Private Sub Text1_Change()
Text1.SelStart = Len(Text1.Text) 'scrolls down the text1 box when it needs to
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Form1.Winsock1.SendData Text1.Text & vbCrLf
Text1.Text = Text1.Text & vbNewLine & "You said> " & Text2.Text 'displays what you typed
Text2.Text = "" 'clears the textbox you type in
KeyAscii = 0
End If
End Sub
Private Sub Form_Unload()
Winsock1.Close
End Sub

Server:

Private Sub Command1_Click()
Dim Server As String
Dim Port As String
Winsock1.RemoteHost = IPtxt.Text
Winsock1.RemotePort = Porttxt.Text
Winsock1.Listen
End Sub

Private Sub Command2_Click()
Winsock1.Close
MsgBox ("You have stopped the server.")
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
'this sends data to the client trying to connect
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID 'accepts the client connecting
Winsock1.SendData "Connected" 'sends data to the client telling him/her its ok to connect
Label1.Caption = "Status: Connected"
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State = 0 Then End
Winsock1.Close
End Sub

And when I try to run it,it connects in everything,but when I try to type something in Text2,and press enter,I get the error Wrong Protocol or connection state for the requested transaction or request. Please help!
 

splice

Golden Member
Jun 6, 2001
1,275
0
0
what do you have in the ConnectionRequest event on the server side?

you need something like so:

Winsock1.Accept(requestID)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Create another winsock control, and instead of Winsock1.Accept requestID, make it yournewwinsockcontrol.Accept requestID
 

dcpsoguy

Diamond Member
Nov 5, 2000
3,252
0
0
Doesnt work,here is the updated server.


Private Sub Command1_Click()
Dim Server As String
Dim Port As String
Winsock1.RemoteHost = IPtxt.Text
Winsock1.RemotePort = Porttxt.Text
Winsock1.Listen
End Sub

Private Sub Command2_Click()
Winsock1.Close
MsgBox ("You have stopped the server.")
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State = 0 Then End
Winsock1.Close
End Sub
Private Sub Winsock2_ConnectionRequest(ByVal requestID As Long)
'this sends data to the client trying to connect
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID 'accepts the client connecting
Winsock1.SendData "Connected" 'sends data to the client telling him/her its ok to connect
Label1.Caption = "Status: Connected"
End Sub
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
You're listening with Winsock1 yet you're trying to accept a request through Winsock2. Winsock2 will never be notificed of the request, and Winsock1 is already bound to your port. So, listen with Winsock1, use the ConnectionRequest even of Winsock1 to Accept with Winsock2. Also, you're setting the RemoteHost and RemotePort properties of Winsock1. This is a server, the remote port and remote host aren't defined until someone connects. Set the Local host and port...
 

dcpsoguy

Diamond Member
Nov 5, 2000
3,252
0
0
Still doesnt work. Here is the updated code.

Private Sub Command1_Click()
Dim Server As String
Dim Port As String
Winsock1.LocalHostName = IPtxt.Text
Winsock1.LocalPort = Porttxt.Text
Winsock1.Listen
End Sub

Private Sub Command2_Click()
Winsock1.Close
MsgBox ("You have stopped the server.")
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State = 0 Then End
Winsock1.Close
If Winsock2.State = 0 Then End
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
'this sends data to the client trying to connect
If Winsock2.State <> sckClosed Then Winsock2.Close
Winsock2.Accept requestID 'accepts the client connecting
Winsock2.SendData "Connected" 'sends data to the client telling him/her its ok to connect
Label1.Caption = "Status: Connected"
End Sub