When a client disconnects I normally unload the Winsock control that is associated with that clientVB Code:
Private Sub Winsock_Close(Index As Integer) Unload Winsock(Index) End Sub
Printable View
When a client disconnects I normally unload the Winsock control that is associated with that clientVB Code:
Private Sub Winsock_Close(Index As Integer) Unload Winsock(Index) End Sub
Isn't it better if you leave it as it is, but just close the connection ? That way you can use it later on, for a new connection... (that's what I always do)
Also... I think it's weird to have a gap in the control array...
So do I, hence my previous bit of code.Quote:
Also... I think it's weird to have a gap in the control array...
Poor Pino, we're ruining his nice thread.
I mean like this:
This code is from the server I made for work, wich we use for more than a year now...
VB Code:
Private Sub SckTCP_Close(Index As Integer) CClients = CClients - 1 SckTCP(Index).Close ClearUser Index If Index = 0 Then ConnectAllSockets End If End Sub Private Sub SckTCP_ConnectionRequest(Index As Integer, ByVal requestID As Long) Dim K As Integer, SHA As New clsSHA256, StrToSend As String For K = 1 To SckTCP.UBound If SckTCP(K).State = sckClosed Then Exit For Next K If K = SckTCP.UBound + 1 Then Load SckTCP(SckTCP.UBound + 1) Load tmrSendData(SckTCP.UBound) ReDim Preserve Clients(SckTCP.UBound) K = SckTCP.UBound End If Randomize Clients(K).State = 0 Clients(K).CLogIn.RndINIT = UCase(SHA.SHA256(CStr((2 ^ 30) * Rnd))) SckTCP(K).Accept requestID StrToSend = "RndINIT|" & Clients(K).CLogIn.RndINIT SckTCP(K).SendData Len(StrToSend) & ":" & StrToSend CClients = CClients + 1 End Sub
Quote:
Originally Posted by Pino
This is a very good tutorial but you forgot to include some stuff (which the most BEGINNER programmers wouldnt know how to do)
in the parts where you say stuff like
"ok so now your server is listening we must handle connection requests, this is when the client try’s to connect to us!
Private Sub Winsock_ConnectionRequest(byVal RequestID as long)"
you didnt include the part that tells HOW to get the above statement (the sub_winsock_connectionrequest) event...so that may be a tad bit confusing (of course I kno how to get it cuz this is my 2nd year of programming)...you also didnt mention that you have to double click the form and select the LOAD property to enter the first bit of code you give
sorry for the negative feedback...just think of it as constructive criticism :)
thanks for the tutorial...hopefully i wont have any questions but if i do i will let you know...and maybe we can write another tutorial together :)
Thanks for your comments,
I think if somone is beginning to learn winsock then they should know a decent level of programming, and stuff like getting the correct subs well i'd assume they would know that.
Thanks for the feedback though, either way the guide has helped several people get to grips wihth winsock thats all it was here for not supposed to be a full explination :)
your welcome for the comments, again I am sorry they sounded so negative....
i attempted to use it to learn winsock...but its just wayyy over my head...
normally when you learn a new command u learn the generic use of it correct? such as the command button....when I learned it all we first learned to do was make a pic visible..so thats simple....winsock is just....CONFUSING!
thanks for taking the time out to write the guide regardless...and i hope you have fun in the future with whatever helpful projects you make work on :)
Well in all honesty this is the simplest format of how winsock works, if this above you I feel you are not ready. Also this is old now move to .NET :-D
Thanks
Because this codes helped me very much.
Raghib Khesal
KGCS
raghib.netfirms.com
Alright i dont know if this will help anyone but this connectionrequest code is from my chat server which can handle as many winsock controls that vb can open. If this has been posted or something similar to it i apologize but it works friggin great seeing as it checks for any unused sockets that are still open before making new ones so it recycles the old ones and keeps reusing them saves huge on memory
VB Code:
Private Sub sckData_ConnectionRequest(Index As Integer, ByVal requestID As Long) Dim t1, t2 Dim i As Long, num As Long If IsServerBanned(sckData(Index).RemoteHostIP) = "True" Then Exit Sub CheckSockets: For i = 1 To sckData.UBound If sckData(i).State = 0 Or sckData(0).State = 8 Then sckData(i).Close sckData(i).Tag = "" sckData(i).Accept requestID WriteToLog sckData(i).RemoteHostIP & " is attempting to connect", RGB(0, 0, 0) sckData(i).SendData "gi" & Chr(10) Exit Sub End If DoEvents Next i DoEvents num = sckData.UBound + 1 Load sckData(num) sckData(num).Accept requestID WriteToLog sckData(i).RemoteHostIP & " is attempting to connect", RGB(0, 0, 0) sckData(num).SendData "gi" & Chr(10) End Sub
Just ignore the senddata stuff chat protocols and stuff
That's basically the same idea of the code I posted in post #44
I'd like to thank y'all for making my life alot easier. I've used code based on this thread for a couple of years for several meteorological servers where I work. It works great!
Does anyone have any idea how to do this stuff in VB2005? My research into this indicates you need about 10 times the amount of code to maybe achieve the same results. :confused:
I dont have word, but i need the tut. :(
Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
newClient = wskClients.Count + 1
Load wskClients(newClient)
wskClients(newClient).Accept requestID 'accept the connection with our new Winsock
End Sub
Doesent work to me it say method member not found And sory i didtn place it in the VB code box becouse it sux and i dont know how to use it becouse im a idiot...
instead of Local machine whether i can connect to Remote machine.
i just created a combobox and inside that i have "localhost" and remote machine eg:"a1".
if selected is localhost, it will connect. but if the selected item is a1(remote machine) it's not connecting.
this the code i'm using....
SEND
------
Private Sub frmSend_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
comboBoxIP.Items.Add("localhost")
comboBoxIP.Items.Add("a1")
End Sub
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
wsSend.Close()
wsSend.Connect(comboBoxIP.Text, 1223) End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
wsSend.SendData(txtSendWhat.Text)
End Sub
RECEIVE
--------
Private Sub frmSend_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim frmReceive As New frmReceive
frmReceive.Show()
wsReceive.Close()
wsReceive.LocalPort = 1223
'wsReceive.RemotePort = 1223 wsReceive.Listen()
End Sub
Private Sub wsReceive_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles wsReceive.ConnectionRequest
If wsReceive.CtlState <> MSWinsockLib.StateConstants.sckClosed Then
wsReceive.Close()
End If
wsReceive.Accept(e.requestID)
If wsReceive.CtlState = MSWinsockLib.StateConstants.sckConnected Then
Call wsReceiveConnected()
End If
End Sub
Private Sub wsReceive_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles wsReceive.DataArrival
Dim str As String
wsReceive.GetData(str, vbString)
txtMessageReceived.Text = str
End Sub
if i need to connect to a remote machine, what is the change i need to do in this code. i'm not getting anyidea.... if you know how to do that please let me know .
thanks in advance and thanks for your post and link.. its very usefull for me...
I'm trying to do this at home but I cant find the winsock control? i even tried browsing for it and running a search but could not find it. I'm running Vista, is this why? how would i go about this project if so?
This is very old code Visual Baisc 6, what version are you using?Quote:
Originally Posted by paralyzedcitizen
I'm using VB6. I know it's old and I should be using .net but my mate gave it to me and that's why i've learnt vb6. I have started to learn .net with 2005 but i prefer vb6 to be honest.
*Bump*
paralyzedcitizen, why don't you start a new thread where you ask for your problem instead of "hijacking" a year old thread ?
Make sure your thread title is about your problem, something like "Winsock on Vista"
?Quote:
paralyzedcitizen, why don't you start a new thread where you ask for your problem instead of "hijacking" a year old thread ?
My query is directly related to this thread, I'm trying to make the chat application from the first post. Surely this thread couldn't be more relavent? And if I created a new thread I would most likely just get linked back here.
yay men....its not working if you try to connect it using other computer to other computer...but do nice..