Click here to Skip to main content
15,446,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a Windows Forms Application that has a TCPlistener and such and can accept one connection, but I want it to accept more than one. I tried the code from http://www.devx.com/dotnet/Article/28083#codeitemarea[^], except I put the server class' code in my current project, running on the Form1_Load event instead of Sub Main(), as it's Winforms instead of console-based.

So now I have this:

VB
Dim TCPListener As New TcpListener(IncomingPort)
    Private Sub Form1_Load()
        TCPListener.Start()
        Me.Text = 0
        While True
            Dim client As New ClientConnection(TCPListener.AcceptTcpClient)
            Me.Text = CInt(Me.Text) + 1
        End While
End Sub


ClientConnection is this class:

VB
Imports System.Net.Sockets
Public Class ClientConnection
    Public Shared AllClients As New Hashtable
    Private _client As TcpClient
    Private _clientIP As String
    Private _ClientNick As String
    Private data() As Byte
    Private ReceiveNick As Boolean = True
    Public Sub New(ByVal Client As TcpClient)
        _client = client
        _clientIP = client.Client.RemoteEndPoint.ToString
        AllClients.Add(_clientIP, Me)
        ReDim data(_client.ReceiveBufferSize)
        _client.GetStream.BeginRead(data, 0, CInt(_client.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
    End Sub
    Public Sub SendMessage(ByVal message As String)
        Try
            Dim ns As System.Net.Sockets.NetworkStream
            SyncLock _client.GetStream
                ns = _client.GetStream
            End SyncLock
            Dim bytesToSend As Byte() = System.Text.Encoding.ASCII.GetBytes(message)
            ns.Write(bytesToSend, 0, bytesToSend.Length)
            ns.Flush()
        Catch ex As Exception
            MsgBox("Error sending message: " & vbNewLine & vbNewLine & ex.Message)
        End Try
    End Sub
    Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
        Dim bytesRead As Integer
        Try
            SyncLock _client.GetStream
                bytesRead = _client.GetStream.EndRead(ar)
            End SyncLock
            If bytesRead < 1 Then
                AllClients.Remove(_clientIP)
                Broadcast(_ClientNick & " has left the chat.")
                Exit Sub
            Else
                Dim messageReceived As String = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead)
                If ReceiveNick Then
                    _ClientNick = messageReceived
                    Broadcast(_ClientNick & " has joined the chat.")
                    ReceiveNick = False
                Else
                    Broadcast(_ClientNick & ">" & messageReceived)
                End If
            End If
            SyncLock _client.GetStream
                _client.GetStream.BeginRead(data, 0, CInt(_client.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
            End SyncLock
        Catch ex As Exception
            AllClients.Remove(_clientIP)
            Broadcast(_ClientNick & " has left the chat.")
        End Try
    End Sub
    Public Sub Broadcast(ByVal message As String)
        MsgBox("Received: " & message)
        Dim c As DictionaryEntry
        For Each c In AllClients
            CType(c.Value, ClientConnection).SendMessage(message & vbnewline)
        Next
    End Sub
End Class


When this is in the Sub Main() of the console application, there are no errors. But in WinForms, there is one that keeps saying "Only one usage of each socket address (protocol/network address/port) is normally permitted". Upon research I found that Sub Main() and Load() are the same so it's not like Load is being called continuously and creating loads of receivers... so any pointers please?

-Rixterz
Posted
Updated 9-Jun-14 5:32am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jun-14 12:40pm     CRLF
This is totally unrelated to Windows.Forms or something like that. Any number of client sockets can be connected to a server-side socket. What happens here is a different thing: you probably listen to the same port in more than one place by mistake; please check it up with the debugger. —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900