Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Nodda4you on May 18, 2005, 03:31 PM

Title: baisc help
Post by: Nodda4you on May 18, 2005, 03:31 PM
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...
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


For the client
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


I run it and click connect, and try sending, but it says
QuoteWrong protocal or connection state for the requested transaction or request
Title: Re: baisc help
Post by: Hdx on May 18, 2005, 03:39 PM
Make sure to close the winsock before tryinhg to connect.
~-~(HDX)~-~
Title: Re: baisc help
Post by: Nodda4you on May 18, 2005, 03:55 PM
On which? Client or server?
I have it for the server..

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


and if i need it for the client, where do i put it? and would it still be

Winsock1.close
?
Title: Re: baisc help
Post by: Hdx on May 18, 2005, 03:59 PM

Private Sub cmdConnect_Click()
                            <~~~~poke
Winsock1.Connect
End Sub

~-~(HDX)~-~
Title: Re: baisc help
Post by: OnlyMeat on May 18, 2005, 04:01 PM
Im assuming it's because you are not selecting the protocol either udp or tcp/ip. This can be done on the form load.
Title: Re: baisc help
Post by: Hdx on May 18, 2005, 04:05 PM
Winsock in VB defults to TCP\IP upon adding the object. But that could be the problem
~-~(HDX)~-~
Title: Re: baisc help
Post by: Nodda4you on May 18, 2005, 04:13 PM

Private Sub cmdConnect_Click()
Winsock1.Close
Winsock1.Connect
End Sub
Title: Re: baisc help
Post by: Tontow on May 18, 2005, 07:40 PM
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.


Title: Re: baisc help
Post by: Hdx on May 18, 2005, 07:52 PM
First it's listening, so it has to stop listening, then accept the connection.
~-~(HDX)~-~
Title: Re: baisc help
Post by: LivedKrad on May 18, 2005, 07:57 PM
Why not set a conditional to check the connection state before sending? That way you can see if you're even connected at all.
Title: Re: baisc help
Post by: Nodda4you on May 18, 2005, 08:07 PM
How do I do that?
Title: Re: baisc help
Post by: Hdx on May 18, 2005, 08:10 PM
winsock.State
sckConnected
~-~(HDX)~-~
Title: Re: baisc help
Post by: 111787 on May 18, 2005, 08:12 PM
you are probably going to want to use an array of sockets.  Otherwise you can only have one connection to the server.
Title: Re: baisc help
Post by: Nodda4you on May 18, 2005, 08:22 PM
Where would I put

winsock.State
sckConnected
Title: Re: baisc help
Post by: Tontow on May 18, 2005, 08:23 PM
Quote from: Nodda4you on May 18, 2005, 08:07 PM
How do I do that?

put this in a timer


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

Title: Re: baisc help
Post by: Nodda4you on May 18, 2005, 08:35 PM
How would I get

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
to work?
Title: Re: baisc help
Post by: Hdx on May 18, 2005, 08:39 PM
you exicute it. Replace Socket with your winsock control. All that does is change the forms caption to what state it is in.
~-~(HDX)~-~
Title: Re: baisc help
Post by: Tontow on May 18, 2005, 08:49 PM
Quote from: Nodda4you on May 18, 2005, 08:35 PM
How would I get

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
to work?

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)
Title: Re: baisc help
Post by: Spilled on May 25, 2005, 03:16 PM
Quote from: Nodda4you on May 18, 2005, 03:31 PM
For the client
Private Sub cmdConnect_Click()
Winsock1.Connect
End Sub

Also you need to provide it with a server, port to connect to
Ex:

Private sub cmdConnect_Click()
Socket.Close: Socket.Connect SERVERHERE, PORTHERE
end sub


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.