TheDude
09-16-2007, 02:24 PM
Hi,
you can use the System.net.sockets namespace to do client server
programming. The following link has some information.
You can convert the above sample into a server that communicates with
multiple clients using the following code
Main Module
===========
Imports System.Net.Sockets
Imports System.Text
Class TCPSrv
Shared Sub Main()
' Must listen on correct port- must be same as port client wants to
connect on.
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
Dim ct As Integer
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return 'a
TcpClient initialized for communication.
ct = 0
While (True)
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
Dim tmp As New WorkerThread(tcpClient, ct)
ct += 1
Dim thrd As New System.Threading.Thread(AddressOf
tmp.workerMethod)
thrd.Start()
End While
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End Sub
End Class
The WorkerThread Class
======================
Imports System.Net.Sockets
Imports System.Text
Public Class WorkerThread
Private tcpClient As tcpClient
Private clientnumber As Integer
Sub New(ByRef tcpCli As tcpClient, ByVal ct As Integer)
tcpClient = tcpCli
clientnumber = ct
End Sub
Sub workerMethod()
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString + "Client
number" + clientnumber.ToString))
'Any communication with the remote client using the TcpClient can
go here.
'Close TcpListener and TcpClient.
tcpClient.Close()
Console.WriteLine("exit")
End Sub
End Class
you can use the System.net.sockets namespace to do client server
programming. The following link has some information.
You can convert the above sample into a server that communicates with
multiple clients using the following code
Main Module
===========
Imports System.Net.Sockets
Imports System.Text
Class TCPSrv
Shared Sub Main()
' Must listen on correct port- must be same as port client wants to
connect on.
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
Dim ct As Integer
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return 'a
TcpClient initialized for communication.
ct = 0
While (True)
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
Dim tmp As New WorkerThread(tcpClient, ct)
ct += 1
Dim thrd As New System.Threading.Thread(AddressOf
tmp.workerMethod)
thrd.Start()
End While
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End Sub
End Class
The WorkerThread Class
======================
Imports System.Net.Sockets
Imports System.Text
Public Class WorkerThread
Private tcpClient As tcpClient
Private clientnumber As Integer
Sub New(ByRef tcpCli As tcpClient, ByVal ct As Integer)
tcpClient = tcpCli
clientnumber = ct
End Sub
Sub workerMethod()
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString + "Client
number" + clientnumber.ToString))
'Any communication with the remote client using the TcpClient can
go here.
'Close TcpListener and TcpClient.
tcpClient.Close()
Console.WriteLine("exit")
End Sub
End Class