| Previous | Home | Next |
A Socket is an End-Point for communication link between two programs (Server Program and Client Program ) running on the same network .Socket is bidirectional. We have to write two programs for implementing a socket application in VB.Net.
Server Socket Program ( Server ) Client Socket Program ( Client ) .
In VB.Net network programming can be done by using namespaces like System.Net and System.Net.Sockets . The classes and methods of these namespaces can communicate across the network. The communication can be either connection oriented or connectionless. They can also be either stream oriented or data-gram based.
Server Socket Program
Server program running on a computer has a socket that bound to a Port Number on the same computer and listening to the client's incoming requests.
Client Socket Program
Client program have to know the IP Address ( Hostname ) of the computer that the VB.Net Server Socket Program resides and the Port Number assign for listening for client's request .
Code for Server Socket Program
Module Module1
Sub Main()
Dim serverSocket As New TcpListener(8888)
Dim requestCount As Integer = 0
Dim clientSocket As TcpClient = Nothing
serverSocket.Start()
Console.WriteLine(" >> Server Started")
clientSocket = serverSocket.AcceptTcpClient()
Console.WriteLine(" >> Accept connection from client")
requestCount = 0
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
Dim bytesFrom As Byte() = New Byte(10024) {}
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
Console.WriteLine(" >> Data from client - " & dataFromClient)
Dim serverResponse As String = "Server response " & Convert.ToString(requestCount)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
Console.WriteLine(" >> " & serverResponse)
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End While
clientSocket.Close()
serverSocket.[Stop]()
Console.WriteLine(" >> exit")
Console.ReadLine()
End Sub
End Module
Code for Client Socket Program
For creating Client design a Client using windows application. Server is console application but client is windows application. Server will give response when client request to sending data or connecting with server.
Namespace New_Client
Public Partial Class Form1
Inherits Form
Private clientSocket As New System.Net.Sockets.TcpClient()
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs)
msg("Client Started")
clientSocket.Connect("127.0.0.1", 8888)
End Sub
Public Sub msg(mesg As String)
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " & mesg
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() = System.Text.Encoding.ASCII
.GetBytes("Message from Client$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim inStream As Byte() = New Byte(10024) {}
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " & returndata)
End Sub
End Class
End Namespace
First execute the Server Socket Program and then Client Socket Program as shown in above pictures-
On Click to button "Send data to server" the data will send to server.
| Previous | Home | Next |