Valhalla Legends Forums Archive | Visual Basic Programming | baisc help

AuthorMessageTime
Nodda4you
im making a basic chat program. well, not even chat. just to understand winsock.
here's my code. It's not working

For the server...
[code]Private Sub cmdConnect_Click()
Winsock1.LocalPort = 1234
Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim TempData As String
Winsock1.GetData TempData
MsgBox TempData
End Sub
[/code]

For the client
[code]Private Sub cmdConnect_Click()
Winsock1.Connect
End Sub

Private Sub cmdSend_Click()
Dim data As String
data = "Testing 123"
Winsock1.SendData data
End Sub

Private Sub Form_Load()
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = 1234
End Sub
[/code]

I run it and click connect, and try sending, but it says [QUOTE]Wrong protocal or connection state for the requested transaction or request[/Quote]
May 18, 2005, 8:31 PM
HdxBmx27
Make sure to close the winsock before tryinhg to connect.
~-~(HDX)~-~
May 18, 2005, 8:39 PM
Nodda4you
On which? Client or server?
I have it for the server..
[code]
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
[/code]

and if i need it for the client, where do i put it? and would it still be
[code]
Winsock1.close
[/code]?
May 18, 2005, 8:55 PM
HdxBmx27
[code]
Private Sub cmdConnect_Click()
                            <~~~~poke
Winsock1.Connect
End Sub[/code]
~-~(HDX)~-~
May 18, 2005, 8:59 PM
OnlyMeat
Im assuming it's because you are not selecting the protocol either udp or tcp/ip. This can be done on the form load.
May 18, 2005, 9:01 PM
HdxBmx27
Winsock in VB defults to TCP\IP upon adding the object. But that could be the problem
~-~(HDX)~-~
May 18, 2005, 9:05 PM
Nodda4you
[code]
Private Sub cmdConnect_Click()
Winsock1.Close
Winsock1.Connect
End Sub
[/code]
May 18, 2005, 9:13 PM
Tontow
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub


why is your server closeing the connection befor accepting it?????  Thats like hangeing up the phone befor saying hello.


May 19, 2005, 12:40 AM
HdxBmx27
First it's listening, so it has to stop listening, then accept the connection.
~-~(HDX)~-~
May 19, 2005, 12:52 AM
LivedKrad
Why not set a conditional to check the connection state before sending? That way you can see if you're even connected at all.
May 19, 2005, 12:57 AM
Nodda4you
How do I do that?
May 19, 2005, 1:07 AM
HdxBmx27
winsock.State
sckConnected
~-~(HDX)~-~
May 19, 2005, 1:10 AM
111787
you are probably going to want to use an array of sockets.  Otherwise you can only have one connection to the server.
May 19, 2005, 1:12 AM
Nodda4you
Where would I put
[code]
winsock.State
sckConnected
[/code]
May 19, 2005, 1:22 AM
Tontow
[quote author=Nodda4you link=topic=11628.msg112935#msg112935 date=1116464857]
How do I do that?
[/quote]

put this in a timer

[code]
Select Case socket.State
   Case 0
       form.Caption = "Closed"
   Case 1
       form.Caption = "Open"
   Case 2
       form.Caption = "Listening"
   Case 3
       form.Caption = "Connection pending"
   Case 4
       form.Caption = "Resolving host"
   Case 5
       form.Caption = "Host resolved"
   Case 6
       form.Caption = "Connecting"
   Case 7
       form.Caption = "Connected"
   Case 8
       form.Caption = "Peer is closing the connection"
   Case 9
       form.Caption = "Error"
End Select
[/code]
May 19, 2005, 1:23 AM
Nodda4you
How would I get
[code]
Select Case socket.State
  Case 0
      form.Caption = "Closed"
  Case 1
      form.Caption = "Open"
  Case 2
      form.Caption = "Listening"
  Case 3
      form.Caption = "Connection pending"
  Case 4
      form.Caption = "Resolving host"
  Case 5
      form.Caption = "Host resolved"
  Case 6
      form.Caption = "Connecting"
  Case 7
      form.Caption = "Connected"
  Case 8
      form.Caption = "Peer is closing the connection"
  Case 9
      form.Caption = "Error"
End Select
[/code] to work?
May 19, 2005, 1:35 AM
HdxBmx27
you exicute it. Replace Socket with your winsock control. All that does is change the forms caption to what state it is in.
~-~(HDX)~-~
May 19, 2005, 1:39 AM
Tontow
[quote author=Nodda4you link=topic=11628.msg112950#msg112950 date=1116466549]
How would I get
[code]
Select Case socket.State
   Case 0
       form.Caption = "Closed"
   Case 1
       form.Caption = "Open"
   Case 2
       form.Caption = "Listening"
   Case 3
       form.Caption = "Connection pending"
   Case 4
       form.Caption = "Resolving host"
   Case 5
       form.Caption = "Host resolved"
   Case 6
       form.Caption = "Connecting"
   Case 7
       form.Caption = "Connected"
   Case 8
       form.Caption = "Peer is closing the connection"
   Case 9
       form.Caption = "Error"
End Select
[/code] to work?
[/quote]

add a timer to your form
double click the timer and add that code
replace form with the name of your form and replace socket with the name of your winsock.

also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/VBRef98/html/vbmscLROverview.asp should be of some help (look to the menu at the left)
May 19, 2005, 1:49 AM
Spilled[DW]
[quote author=Nodda4you link=topic=11628.msg112907#msg112907 date=1116448292]
For the client
[code]Private Sub cmdConnect_Click()
Winsock1.Connect
End Sub
[/code]
[/quote]
Also you need to provide it with a server, port to connect to
Ex:
[code]
Private sub cmdConnect_Click()
Socket.Close: Socket.Connect SERVERHERE, PORTHERE
end sub
[/code]

The Socket.Close just makes sure the winsock closes before you try to connect again. Without it if you hit connect twice your program would error. Goodluck.
May 25, 2005, 8:16 PM

Search