-
2 Attachment(s)
More Winsock :P
Well i started programing a simple server and client just for the fun and experiance. anyway i basically want the server to display a list of the ip's connected to it. This i have managed to do but now im having problems displaying the data in the allocated text box, i had it perfect at first but after changing a few things (i cannot remember what i changed) it decided to stop working properly, heres the part of the code i know has something wrong with it. Well it doesnt have anything wrong but its very buggy and it a bad way to do what i want done, anyone know how i could imporve from what i have?
Code:
Edited Post - Read Below
Edit:
Ok i have been messing about trying different things and i have managed to get it working again but it still doesnt work 100%, below is the latest screenshot and i have uploaded the exe files of my client + server so you can see what i mean when i say its not working 100%
aslo my new bit of code is below:
Code:
Private Sub Timer1_Timer()
If Winsock1.State = sckConnected And Winsock2.State = sckConnected Then
Text1.Text = Winsock1.RemoteHostIP & par.Text & Winsock1.LocalPort & newl.Text & Winsock2.RemoteHostIP & par.Text & Winsock2.LocalPort
End If
If Winsock2.State = sckConnected And Winsock1.State = sckClosed Then
Text1.Text = Winsock2.RemoteHostIP & par.Text & Winsock2.LocalPort
End If
If Winsock1.State = sckConnected And Winsock2.State = sckClosed Then
Text1.Text = Winsock1.RemoteHostIP & par.Text & Winsock1.LocalPort
End If
If Winsock1.State = sckClosed And Winsock2.State = sckClosed Then
Text1.Text = ""
End If
End Sub
Thanks in Advance
Server & Client ScreenShot:
-
It might help if you explain whats wrong and show us what its supposed to be
-
Tell us whats wrong, also you might want to look into indexs with 1 winsock instead of making 2 controls.. if your accepting more then 2 connections that is. :P
-
well i dont know how to lol, and the thing thats wrong is if i click 1 of the connections 1 ip doesnt show up, it only shows the ips if i click on both of the connections.. also can you explain a bit more about the indexs? thanks in advance
-
Perfect article:
http://www.winsockvb.com/article.php?article_id=18
If you have problems let me know.. basicly you are loading another instance of the winsock control and accepting the connection to that newly loaded control. You will have a timer that does a loop throught these controls to see if they are connected(using a boolean varible which you will set) and if not just disconnect them.
Private var which goes on very top- Public for modules:
Code:
Private Connected(1 to 500) As Boolean
So new connection:
Code:
for i = 1 to ubound(connected)
if connected(i) = false then
Winsock1(0).Close 'close the listen
Load Winsock1(i) 'load the new object
connected(i) = true
Winsock1(i).Accept requestID 'accept it
Winsock1(0).Listen 'listen again so we can get more connections
end if
next i
Timer1(set to like 3000 miliseconds i would say) which will go throught to see if anyone disconnected:
Code:
For i = 1 To UBound(Connected)
If Connected(i) = True Then
If Not Winsock1(i).State = 7 Then
Connected(i) = False
Unload Winsock1(i)
End If
End If
Next i
On data arrival:
Code:
'we now have indexs to play around with, everytime data is received we know who sent it.. 'for example
Dim Data As string
Winsock1(index).GetData Data
Msgbox Data
'to send
Winsock1(index).senddata "hello" 'this sends data right back to the guy whom we receieved data from
other sending(to send out of the on data receive you will need to store the index of the person who send in an integer so make a function.. if you need to:
Code:
'a loop to send to everyone connected
for i = 1 to Ubound(connected)
if winsock1(i).state = 7 then 'if connected continue
winsock1(i).senddata "this is a test"
end if
next i
function incase your retarted:
Code:
Private Function SendPack(Data as string, who as integer)
Winsock1(who).senddata Data
End Function
example use(on data receive):
Code:
Dim data as string
winsock1(index).getdata data
if data = "dothis" then
SendPack "done",index
end if
hope this helps.. I changed most the code without testing so exscuse any errors.
-
thanks for this, my client/server is a heck of a lot more efficient now, 1 thing though, if i want it to display a list of the ips connected would i have to use seperate text boxes or could i just use 1? - making a dynamic list looks easy but its harder than its made out to be lol.
-
Just use a listbox control?
Heres a simple system:
You add their index and ip to the listbox on connect:
Code:
for i = 1 to ubound(connected)
if connected(i) = false then
Winsock1(0).Close 'close the listen
Load Winsock1(i) 'load the new object
connected(i) = true
Winsock1(i).Accept requestID 'accept it
listbox1.additem i & ":" & Winsock1(i).remotehost
Winsock1(0).Listen 'listen again so we can get more connections
end if
next i
^--if .remotehost isn't correct i'm sorry, I'm typing this all from memory.
say if some one disconnects and you want to remove them from the list(timer1 code):
Code:
dim things() as string
dim j as integer
For i = 1 To UBound(Connected)
If Connected(i) = True Then
If Not Winsock1(i).State = 7 Then
Connected(i) = False
Unload Winsock1(i)
For j = 0 to listbox1.listcount - 1
things = split(listbox1.item(j), ":")
if int(things(0)) = j then 'this is the correct user who disconnected
listbox1.removeitem(j) 'remove them
End If
Next j
End If
End If
Next i
once again if theres any errors in the code tell me.. this is all from memory.