TCP/IP Network Programming

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

Network programming is based around the Winsock control. To use the Winsock Control, add it to the ToolBox by selecting the "Components" menu item and select "Microsoft Winsock Control 6.0" from the list. The Winsock control is not visible to the user when the application is run and provides access to Transfer Control Protocol (TCP) and User Datagram Protocol (UDP) Network Services.

Transfer Control Protocol (TCP)

The Transfer Control Protocol (TCP) is used to create and maintain a connection to a remote computer. To set the Winsock control to TCP, set Winsock's Protocol Property to "sckTCPProtocol". The computers are able to share information through the connection. The Client must know the Server's name or Internet Protocol IP address, set in the RemoteHost Property of the Winsock control. The Client must also know the Port number on which the Server will be listening, set in the RemotePort Property of the Winsock control.

If tcpClient.RemoteHost = "" Then
     tcpClient.RemoteHost = "localhost"
End If
tcpClient.RemotePort = 3456
tcpClient.Connect

 

The LocalPort Property must be set on the Server to specify which Port it is listening to.

tcpServer.LocalPort = 3456
tcpServer.Listen

 

When the Client requests a connection, the ConnectionRequest Event occurs. The Accept method is used within the ConnectionRequest event to complete the connection.

When connected, information is sent and received using the SendData and GetData methods. When data is received, the DataArrival Event occurs.

SIMPLE TCP CHAT PROGRAM

The following describes how to create a simple TCP based chat program. The program consists of two parts, a server and a client.

TCP CHAT SERVER

The Server Application has a Winsock control named tcpServer using the Protocol sckTCPProtocol, and two TextBoxes, named txtOutput and txtSend. txtOutput's MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpServer.LocalPort = 3456
     tcpServer.Listen
End Sub
Private Sub Form_Unload(Cancel As Integer)
     tcpServer.Close
End Sub
Private Sub tcpServer_Close()
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpServer.Close
     txtOutput.Text = txtOutput.Text & "Client closed connection" & vbCrLf & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
     tcpServer.Listen
End Sub
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
     If tcpServer.State <> sckClosed Then
          tcpServer.Close
     End If
     txtSend.Enabled = True
     txtOutput.Enabled = True
     tcpServer.Accept requestID
     txtOutput.Text = tcpServer.RemoteHostIP & ":" & _
     tcpServer.RemotePort & _
     " connected" & vbCrLf & vbCrLf
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     tcpServer.GetData msg
     txtOutput.Text = txtOutput.Text & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpServer_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)
     MsgBox Source & ": " & Description, vbExclamation, "TCP/IP Error"
End Sub
Private Sub txtOutput_GotFocus()
     If txtOutput.Enabled = True Then
          txtSend.SetFocus
     End If
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          tcpServer.SendData "SERVER: " & txtSend.Text
          txtOutput.Text = txtOutput.Text & "SERVER: " & txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

 

 

TCP CHAT CLIENT:

The Client Application has a Winsock control named tcpClient using the Protocol sckTCPProtocol, and two TextBoxes named txtOutput and txtSend. txtOutput's MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     Dim strPrompt As String

     strPrompt = "Enter the IP address of the server"
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpClient.RemoteHost = InputBox(strPrompt, "IP Address", "127.0.0.1")
     If tcpClient.RemoteHost = "" Then
          tcpClient.RemoteHost = "localhost"
     End If
     tcpClient.RemotePort = 3456
     tcpClient.Connect
End Sub
Private Sub Form_Unload(Cancel As Integer)
     tcpClient.Close
End Sub
Private Sub tcpClient_Close()
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpClient.Close
     txtOutput.Text = txtOutput.Text & "Server closed connection" & vbCrLf & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Connect()
     txtSend.Enabled = True
     txtOutput.Enabled = True
     txtOutput.Text = tcpClient.RemoteHostIP & _
     ":" & tcpClient.RemotePort & _
     " connected" & vbCrLf & vbCrLf
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     tcpClient.GetData msg
     txtOutput.Text = txtOutput.Text & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_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)
     MsgBox Source & ": " & Description, vbExclamation, "TCP/IP Error"
End Sub
Private Sub txtOutput_GotFocus()
     If txtOutput.Enabled = True Then
          txtSend.SetFocus
     End If
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          tcpClient.SendData "CLIENT: " & txtSend.Text
          txtOutput.Text = txtOutput.Text & "CLIENT: " & txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

 

TCO/IP APPLICATION:

USER DATAGRAM PROTOCOL (UDP)

The User Datagram Protocol (UDP) is a Connectionless protocol and unlike TCP does not establish a connection. To set the Winsock control to UDP, set Winsock's Protocol Property to sckUDPProtocol. The computers are able to share information through the connection. Data is sent by setting the Client's LocalPort property and the Server's RemoteHost and RemptePort Properties to the Internet address and LocalPort Property of the Client.

Data is sent and received using the SendData and GetData methods. When data is received, the DataArrival Event occurs.

SIMPLE UDP CHAT PROGRAM:

The following describes how to create a simple UDP based chat program. Like the TCP chat program, this example is consists of a server and a client.

UDP CHAT SERVER:

The Server Application has a Winsock control named udpServer using the Protocol sckUDPProtocol, and two TextBoxes named txtOutput and txtSend. txtOutput's MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     udpServer.Bind 3456
End Sub
Private Sub Form_Unload(Cancel As Integer)
     udpServer.Close
End Sub
Private Sub udpServer_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     udpServer.GetData msg
     txtOutput.Text = txtOutput.Text & udpServer.RemoteHostIP
     txtOutput.Text = txtOutput.Text & ":" & udpServer.RemotePort
     txtOutput.Text = txtOutput.Text & " " & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub txtOutput_GotFocus()
     txtSend.SetFocus
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          udpServer.SendData txtSend.Text
          txtOutput.Text = txtOutput & "Sent: " & txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

 

UDP CHAT CLIENT:

The Client Application has a Winsock control named udpClient using the Protocol sckUDPProtocol, and two TextBoxes named txtOutput and txtSend. txtOutput's MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     Dim msg As String
     msg = "Enter the IP address of the server"
     udpClient.RemoteHost = InputBox(msg, "IP Address", "localhost")
     If udpClient.RemoteHost = "" Then
          udpClient.RemoteHost = "localhost"
     End If
     udpClient.RemotePort = 3456
End Sub
Private Sub Form_Unload(Cancel As Integer)
     udpClient.Close
End Sub
Private Sub udpClient_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     udpClient.GetData msg
     txtOutput.Text = txtOutput.Text & udpClient.RemoteHostIP
     txtOutput.Text = txtOutput.Text & ":" & udpClient.RemotePort
     txtOutput.Text = txtOutput.Text & " " & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub txtOutput_GotFocus()
     txtSend.SetFocus
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          udpClient.SendData txtSend.Text
          txtOutput.Text = txtOutput.Text & "Sent: " & txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641