Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Learning VB.NET Net.Sockets

Status
Not open for further replies.

mishbaker

Technical User
Jan 17, 2004
94
US
I put together a little app that runs a TCPListener and TCPClient class. I wanted to enable them to send little messages back and forth. I've disabled the firewall completely for testing purposes.

Server Code
Code:
Public Class ServerSide
    Private _TCPListener As New TcpListener(IPAddress.Any, 23000)
    Private WithEvents _Timer As System.Timers.Timer = New System.Timers.Timer
    Private _TCP As TcpClient
    Private _NetStream As NetworkStream
    Private _SReader As StreamReader
    Private _Message As String
    Public Event ConnectionFound()

    Public ReadOnly Property Message()
        Get
            Return _Message
        End Get
    End Property

    Public Sub Start()
        _TCPListener.Start()
    End Sub

    Private Sub Timer_Elapsed() Handles _Timer.Elapsed
        If _TCPListener.Pending Then
            _TCP = _TCPListener.AcceptTcpClient
            _NetStream = _TCP.GetStream
            _SReader = New StreamReader(_NetStream)
            _Message = _SReader.ReadToEnd

            RaiseEvent ConnectionFound()
        End If
    End Sub

End Class

The Client Side Code
Code:
Public Class ClientSide
    Private _TCP As TcpClient = Nothing
    Private _SWriter As StreamWriter = Nothing
    Private _NetStream As NetworkStream = Nothing

    Public Sub New()
        _TCP = New TcpClient
        Try
            _TCP.Connect("localhost", 23000)
        Catch ex As Exception
            _TCP.Connect("localhost", 23000)
        End Try

        _NetStream = _TCP.GetStream
    End Sub

    Public Sub SendMessage(ByVal Message As String)
        _SWriter = New StreamWriter(_NetStream)
        _SWriter.WriteLine(Message)
        If _SWriter IsNot Nothing Then
            _SWriter.Close()
        End If
    End Sub

    Public Sub Close()
        If _TCP IsNot Nothing Then
            _TCP.Close()
        End If
    End Sub

End Class

The problem is I get the 10061 Error of: No connection could be made because the target machine actively refused it. Not sure what to do about it at this point.

All lessons learned from the School of Hard Knocks........at least tuition is cheap.
 
There were so many things wrong here I can't begin to explain them all.

First. You have to actually start the Server and Timer so it can check.

Second. You have to close the StreamWriter on the Client end before the StreamReader on the Server end can finish up its job.

Lessons learned. Sorry for taking up space. Any other pointers though would be helpful.

All lessons learned from the School of Hard Knocks........at least tuition is cheap.
 

Are you calling the Start() sub somewhere in your code?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
No :( I wasn't.

It was a long night. Does that excuse fly here?

All lessons learned from the School of Hard Knocks........at least tuition is cheap.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top