Tcp - er der en nemmere / bedre måde i vs2005?
Hej er der nogen der kan sige må om dette kan gøres bedre / nemmere i vs2005. ??-------------------- Module1.vb -----------------------
Imports System.Net.Sockets
Module Module1
Const portNo As Integer = 501
Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Parse("10.11.10.50")
'Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Parse("127.1.0.0")
Dim listener As New System.Net.Sockets.TcpListener(localAdd, portNo)
Sub Main()
listener.Start()
While True
Dim user As New CommandClient(listener.AcceptTcpClient)
End While
End Sub
End Module
--------------------------------------------------------
------------------ CommandClient.vb --------------------
Imports System.Net.Sockets
Public Class CommandClient
'---contains a list of all the clients
Public Shared AllClients As New Hashtable
'---information about the client
Private _client As TcpClient
Private _clientIP As String
Private _ClientNick As String
Private _ClientMachinename As String
Private _ClientTimestamp As String
'---used for sending/receiving data
Private data() As Byte
'---when a client is connected
Public Sub New(ByVal client As TcpClient)
_client = client
'---get the client IP address
_clientIP = client.Client.RemoteEndPoint.ToString
'---add the current client to the hash table
AllClients.Add(_clientIP, Me)
'---start reading data from the client in a separate thread
ReDim data(_client.ReceiveBufferSize - 1)
_client.GetStream.BeginRead(data, 0, _
CInt(_client.ReceiveBufferSize), _
AddressOf ReceiveMessage, Nothing)
End Sub
'---send the message to the client
Public Sub SendMessage(ByVal message As String)
Try
'---send the text
Dim ns As System.Net.Sockets.NetworkStream
SyncLock _client.GetStream
ns = _client.GetStream
Dim bytesToSend As Byte() = _
System.Text.Encoding.ASCII.GetBytes(message)
ns.Write(bytesToSend, 0, bytesToSend.Length)
ns.Flush()
End SyncLock
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
'---receiving a message from the client
Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
'---read from client---
Dim bytesRead As Integer
Try
SyncLock _client.GetStream
bytesRead = _client.GetStream.EndRead(ar)
End SyncLock
'---client has disconnected
If bytesRead < 1 Then
AllClients.Remove(_clientIP)
Broadcast("[UserDisconnected][" & _ClientNick & "]", Nothing)
'Me.BroadCastUserList()
Exit Sub
Else
'---receive the message sent
Dim messageReceived As String = _
System.Text.Encoding.ASCII.GetString(data, 0, bytesRead)
Broadcast(messageReceived ,nothing)
'---continue reading from client
SyncLock _client.GetStream
_client.GetStream.BeginRead(data, 0, _
CInt(_client.ReceiveBufferSize), _
AddressOf ReceiveMessage, Nothing)
End SyncLock
Catch ex As Exception
AllClients.Remove(_clientIP)
Broadcast("[UserDisconnected][" & _ClientNick & "]", Nothing)
End Try
End Sub
Private Sub BroadCastUserList(ByVal users() As String)
'===client is request for all users names===
' e.g. [Usrs]
'---get all the users---
Dim allUsers As String = "[UserList]["
Dim c As DictionaryEntry
For Each c In AllClients
'---get all the users' name
allUsers += _
CType(c.Value, CommandClient)._ClientNick & "," & CType(c.Value, CommandClient)._ClientMachinename & "," & CType(c.Value, CommandClient)._clientIP & "," & CType(c.Value, CommandClient)._ClientTimestamp & "|"
Next
allUsers += "]"
'e.g. [Usrs][User1,User2,etc]
Broadcast(allUsers, users)
End Sub
'---broadcast message to selected users
Public Sub Broadcast(ByVal message As String, ByVal users() As String)
If users Is Nothing Then
'---broadcasting to everyone
Dim c As DictionaryEntry
For Each c In AllClients
'---broadcast message to all users
CType(c.Value, CommandClient).SendMessage(message & vbCrLf)
Next
'---log it locally
Console.WriteLine("---> " & Left(message, 100) & "...")
Else
'---broadcasting to selected ones
Dim c As DictionaryEntry
For Each c In AllClients
Dim user As String
For Each user In users
If CType(c.Value, CommandClient)._ClientNick = user Then
'---send message to user
CType(c.Value, CommandClient).SendMessage(message & vbCrLf)
'---log it locally
Console.WriteLine("---> " & Left(message, 100) & "...")
Exit For
End If
Next
Next
End If
End Sub
End Class
-----------------------------------------------------------
... også gerne hvordan jeg kan lave det om til en winapp istedet?