Winsock is a socket component which is used for making a tcp connection as well as udp connection.
Let's Start
Create a New Project and add a winsock component, command button, textbox.
w.Connect "localhost", 111 'This will connect on localhost at port 111
cmdConnect.Enabled = False
End Sub
Private Sub cmdSend_Click()
w.SendData txtSend.Text 'Send the content of txtSend to connected socket.
End Sub
Private Sub Form_Load()
Form2.Visible = True 'Load the form 2
cmdSend.Enabled = False 'Disable the cmdSend
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
w.Close 'Closing the socket before form close. Its very important
End Sub
Private Sub w_Close()
w.Close 'Close the socket if connection close from other side
MsgBox "Connection closed from one side..."
End
End Sub
Private Sub w_Connect()
cmdSend.Enabled = True
w.SendData "Hi... This is a test"
End Sub
Private Sub w_DataArrival(ByVal bytesTotal As Long)
Dim buff As String
w.GetData buff, vbString 'Getting other side data in buff variable.
lbl.Caption = buff
End Sub
Private Sub cmdListen_Click()
x.LocalPort = 111 'Assign the localport 111 to winsock to listen on it
x.Listen 'Start listning for connection on assigned port.
cmdListen.Enabled = False
End Sub
Private Sub cmdSend_Click()
x.SendData txtSend.Text
txtSend.Text = ""
End Sub
Private Sub Form_Load()
cmdSend.Enabled = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
x.Close
End Sub
Private Sub x_ConnectionRequest(ByVal requestID As Long)
x.Close 'Close listning for connection
x.Accept requestID 'Accept the incoming request
cmdSend.Enabled = True
End Sub
Private Sub x_DataArrival(ByVal bytesTotal As Long)
Dim buff As String
x.GetData buff, vbString
lbl.Caption = buff
End Sub
Private Sub x_Close()
x.Close
MsgBox "Connection closed from one side..."
End
End Sub
Now run the project.
This socket is used in many purpose....
- Create a Standard EXE Project.
- Goto Project Menu -> Components
- Scroll Down and Select Microsoft Winsock Control and Click OK
- Now you see the Winsock(two computer icon) icon on the toolbar.
- Place a winsock to the form and rename it to "w"
- Place a Command Button to the form and rename it to "cmdConnect"
- Place a Text Box to the form and rename it to "txtSend"
- Place a Label and name it to lbl
- Goto Code by double clicking on form and remove the old code.
- Now add this code
w.Connect "localhost", 111 'This will connect on localhost at port 111
cmdConnect.Enabled = False
End Sub
Private Sub cmdSend_Click()
w.SendData txtSend.Text 'Send the content of txtSend to connected socket.
End Sub
Private Sub Form_Load()
Form2.Visible = True 'Load the form 2
cmdSend.Enabled = False 'Disable the cmdSend
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
w.Close 'Closing the socket before form close. Its very important
End Sub
Private Sub w_Close()
w.Close 'Close the socket if connection close from other side
MsgBox "Connection closed from one side..."
End
End Sub
Private Sub w_Connect()
cmdSend.Enabled = True
w.SendData "Hi... This is a test"
End Sub
Private Sub w_DataArrival(ByVal bytesTotal As Long)
Dim buff As String
w.GetData buff, vbString 'Getting other side data in buff variable.
lbl.Caption = buff
End Sub
Create a new form and add the controls
- Create a new form (Project -> Add Form)
- Place a winsock to the form and rename it to "x"
- Place a Command Button to the form and rename it to "cmdListen"
- Place a Text Box to the form and rename it to "txtSend"
- Place a Label and name it to lbl.
- Place a Command Button and rename to "cmdSend"
Private Sub cmdListen_Click()
x.LocalPort = 111 'Assign the localport 111 to winsock to listen on it
x.Listen 'Start listning for connection on assigned port.
cmdListen.Enabled = False
End Sub
Private Sub cmdSend_Click()
x.SendData txtSend.Text
txtSend.Text = ""
End Sub
Private Sub Form_Load()
cmdSend.Enabled = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
x.Close
End Sub
Private Sub x_ConnectionRequest(ByVal requestID As Long)
x.Close 'Close listning for connection
x.Accept requestID 'Accept the incoming request
cmdSend.Enabled = True
End Sub
Private Sub x_DataArrival(ByVal bytesTotal As Long)
Dim buff As String
x.GetData buff, vbString
lbl.Caption = buff
End Sub
Private Sub x_Close()
x.Close
MsgBox "Connection closed from one side..."
End
End Sub
Now run the project.
- Run the project. Both form will shown.
- First click on Listen Button to strat listning.
- Then click on connect button to connect with the listning socket.
- Now write text in text box and send in any of form.
No comments:
Post a Comment