Radford
asked on
Opening Socket Connection in VB
How do you open a socket connection in VB? Is there a socket object or something?
Thanks, and any acceptable answer with sample code with get an A.
Thanks, and any acceptable answer with sample code with get an A.
By the way, here is a chat server written with the winsock control...
Cheers!
-------------------------- ---------- ---------- ---------- ----
1) create a project
2) add the winsock control to your project
3) add a winsock control to your form an set it's INDEX property to 0
4) add a listbox to your form and set the SORTED Property to TRUE
5) add a command button to your form
6) Paste the following code into the DECLARATION section of the form and run it. Anyone can then telnet into this server on port 1001 and chat.
Private Sub Command1_Click()
If List1.ListIndex <> -1 Then Winsock1_Close CInt(Mid$(List1.List(List1 .ListIndex ), 12, 4))
End Sub
Private Sub Form_Load()
Command1.Caption = "Disconnect"
Winsock1(0).LocalPort = 1001
Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
Dim iVal As Long
For iVal = 1 To Winsock1.UBound
If Winsock1(iVal).State = sckConnected Then
Winsock1(iVal).SendData Winsock1(Index).RemoteHost IP + " DISCONNECTED" + vbCrLf
DoEvents
End If
Next iVal
Winsock1(Index).Close
For iVal = 0 To List1.ListCount - 1
If InStr(1, List1.List(iVal), "Connection " + Format$(Index, "0000")) <> 0 Then
List1.RemoveItem iVal
Exit For
End If
Next iVal
If Index = Winsock1.UBound Then
Unload Winsock1(Index)
End If
End Sub
Private Sub Winsock1_ConnectionRequest (Index As Integer, ByVal requestID As Long)
Dim iVal As Long
Dim TotalConnect As Long
Dim Acceptor As Long
Acceptor = 0
TotalConnect = 1
For iVal = 1 To Winsock1.UBound
Select Case Winsock1(iVal).State
Case sckConnected
TotalConnect = TotalConnect + 1
Case sckClosed
Acceptor = iVal
End Select
Next iVal
If Acceptor = 0 Then Acceptor = Winsock1.Count
If Acceptor > Winsock1.UBound Then Load Winsock1(Acceptor)
With Winsock1(Acceptor)
.Accept requestID
List1.AddItem "Connection " + Format$(Acceptor, "0000") + " - " + .RemoteHostIP
.SendData "Welcome to Telnet Chat Server" + vbCrLf + "Server hosted on " + _
Winsock1(0).LocalIP + vbCrLf + "Total sessions connected: " + CStr(TotalConnect) + vbCrLf + vbCrLf
DoEvents
End With
'ANNOUNCE THE NEW CONNECTION TO EVERYONE
For iVal = 1 To Winsock1.UBound
If Winsock1(iVal).State = sckConnected And iVal <> Acceptor Then
Winsock1(iVal).SendData "From SERVER: " + Winsock1(Acceptor).RemoteH ostIP + " has joined session." + vbCrLf
DoEvents
End If
Next iVal
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As String
Dim iVal As Long
Winsock1(Index).GetData strData, vbString
Winsock1(Index).Tag = Winsock1(Index).Tag + strData
If InStr(1, Winsock1(Index).Tag, vbCr) Then
Winsock1(Index).SendData vbCrLf
DoEvents
'SEND THE BUFFER TO EVERYONE
For iVal = 1 To Winsock1.UBound
If Winsock1(iVal).State = sckConnected And iVal <> Index Then
Winsock1(iVal).SendData "From " + Winsock1(Index).RemoteHost IP + ": " + Winsock1(Index).Tag
DoEvents
End If
Next iVal
Winsock1(Index).Tag = ""
Else
'ECHO THE TEXT BACK TO THE LOCAL TERMINAL
Winsock1(Index).SendData strData
End If
End Sub
Cheers!
--------------------------
1) create a project
2) add the winsock control to your project
3) add a winsock control to your form an set it's INDEX property to 0
4) add a listbox to your form and set the SORTED Property to TRUE
5) add a command button to your form
6) Paste the following code into the DECLARATION section of the form and run it. Anyone can then telnet into this server on port 1001 and chat.
Private Sub Command1_Click()
If List1.ListIndex <> -1 Then Winsock1_Close CInt(Mid$(List1.List(List1
End Sub
Private Sub Form_Load()
Command1.Caption = "Disconnect"
Winsock1(0).LocalPort = 1001
Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
Dim iVal As Long
For iVal = 1 To Winsock1.UBound
If Winsock1(iVal).State = sckConnected Then
Winsock1(iVal).SendData Winsock1(Index).RemoteHost
DoEvents
End If
Next iVal
Winsock1(Index).Close
For iVal = 0 To List1.ListCount - 1
If InStr(1, List1.List(iVal), "Connection " + Format$(Index, "0000")) <> 0 Then
List1.RemoveItem iVal
Exit For
End If
Next iVal
If Index = Winsock1.UBound Then
Unload Winsock1(Index)
End If
End Sub
Private Sub Winsock1_ConnectionRequest
Dim iVal As Long
Dim TotalConnect As Long
Dim Acceptor As Long
Acceptor = 0
TotalConnect = 1
For iVal = 1 To Winsock1.UBound
Select Case Winsock1(iVal).State
Case sckConnected
TotalConnect = TotalConnect + 1
Case sckClosed
Acceptor = iVal
End Select
Next iVal
If Acceptor = 0 Then Acceptor = Winsock1.Count
If Acceptor > Winsock1.UBound Then Load Winsock1(Acceptor)
With Winsock1(Acceptor)
.Accept requestID
List1.AddItem "Connection " + Format$(Acceptor, "0000") + " - " + .RemoteHostIP
.SendData "Welcome to Telnet Chat Server" + vbCrLf + "Server hosted on " + _
Winsock1(0).LocalIP + vbCrLf + "Total sessions connected: " + CStr(TotalConnect) + vbCrLf + vbCrLf
DoEvents
End With
'ANNOUNCE THE NEW CONNECTION TO EVERYONE
For iVal = 1 To Winsock1.UBound
If Winsock1(iVal).State = sckConnected And iVal <> Acceptor Then
Winsock1(iVal).SendData "From SERVER: " + Winsock1(Acceptor).RemoteH
DoEvents
End If
Next iVal
End Sub
Private Sub Winsock1_DataArrival(Index
Dim strData As String
Dim iVal As Long
Winsock1(Index).GetData strData, vbString
Winsock1(Index).Tag = Winsock1(Index).Tag + strData
If InStr(1, Winsock1(Index).Tag, vbCr) Then
Winsock1(Index).SendData vbCrLf
DoEvents
'SEND THE BUFFER TO EVERYONE
For iVal = 1 To Winsock1.UBound
If Winsock1(iVal).State = sckConnected And iVal <> Index Then
Winsock1(iVal).SendData "From " + Winsock1(Index).RemoteHost
DoEvents
End If
Next iVal
Winsock1(Index).Tag = ""
Else
'ECHO THE TEXT BACK TO THE LOCAL TERMINAL
Winsock1(Index).SendData strData
End If
End Sub
ASKER
Thanks for the help, but what if I am not using a form? I tried:
Dim MySock As New Winsock
and
Dim MySock As Winsock
Set MySock = CreateObject("Winsock")
Neither of them worked.
Dim MySock As New Winsock
and
Dim MySock As Winsock
Set MySock = CreateObject("Winsock")
Neither of them worked.
I don't know, I've never used the winsock control without a form....
However, you can add a form to your project with the winsock control on it, and just not show the form...
Other than that, I think you'll have to use APIs
Cheers!
However, you can add a form to your project with the winsock control on it, and just not show the form...
Other than that, I think you'll have to use APIs
Cheers!
ASKER
Ok thanks, but I'm going to wait and see if I can get an answer on how to use Winsocket without a form. If I don't get a better answer in a few days, please post again and I'll give you the points.
You can call the winsock.dll file directly using a couple of API's. Since I don't know what you want to do I can just point you to a "server-client" chat program using winsock.dll and NOT that ocx file.
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3976
There's some additional codes using winsock.dll at www.planet-source-code.com, it has a search feature that works occasionally. ;)
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3976
There's some additional codes using winsock.dll at www.planet-source-code.com, it has a search feature that works occasionally. ;)
ASKER
I am writing an Active-X DLL that will open a socket connection to a server. All I need to do is send a few bytes in and I'll be closing the connection. I do not need to have a form, but I can't figure out how to instantiate a winsock object without a form.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Right-click the toolbar and select "Components..." then select "Microsoft Winsock Control" from the list...
Add the control to a form and then press F1... It will give you help on the control.
Cheers!