Jump to content
  • Advertisement
Sign in to follow this  
ed209

[.net] Trying to use StreamReader and StreamWriter

This topic is 3798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to use streamreader and streamwriter to get the data sent and received. For some reason the data isn't sending or receiving and I was hoping someone could show me what I'm doing wrong.. Server
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO
Imports System.Net
Module Module1

    Dim tcpListener As New TcpListener(6996)
    Dim tcpclient As TcpClient
    Dim networkStream As NetworkStream
    Dim connection As Socket
    Dim writer As BinaryWriter
    Dim reader As BinaryReader
    dim connected as boolean=true

    Sub Main()

        tcpListener.Start()
        Console.WriteLine("Waiting for connection...")
        
         Try
         
            tcpclient = tcpListener.AcceptTcpClient()
           
            Console.WriteLine("Connection accepted.")
     
            connection = tcpListener.AcceptSocket
            networkStream = New NetworkStream(connection)

            writer = New BinaryWriter(networkStream)
            reader = New BinaryReader(networkStream)


            writer.Write("Hello, Washington!!!")

        Catch e As Exception
            Console.WriteLine(e.ToString())

        End Try


    End Sub


    Sub read()
        While connected = True

            Dim clientdata As String

            clientdata = reader.ReadString()

            Console.WriteLine((clientdata))

        End While
    End Sub
End Module
Client
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.IO

Module Module1

    Sub Main()

        Using client As New TcpClient()
            Console.WriteLine("Attempting to connect to the server ", "on port 8000.")

            client.Connect(IPAddress.Parse("68.248.3.155"), 6996)

            Using networkStream As NetworkStream = client.GetStream()
                Using writer As New BinaryWriter(networkStream)
                    writer.Write("info")

                    Using reader As New BinaryReader(networkStream)
                        Console.WriteLine(reader.ReadString())
                    End Using
                End Using
            End Using
        End Using


    End Sub

End Module
Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Not sure about the reading (make sure you are actually getting sent a string in the format that ReadString() expects), but for writing you just need to call:
writer.Flush()
which will make sure the data gets sent immediately.

Share this post


Link to post
Share on other sites
You're trying to accept a single incoming connection twice (AcceptTcpClient then AcceptSocket).

This works here:

    ' Snipped Main() method for the server.

Sub Main()

tcpListener.Start()
Console.WriteLine("Waiting for connection...")

Try

connection = tcpListener.AcceptSocket
Console.WriteLine("Connection accepted.")

networkStream = New NetworkStream(connection)

writer = New BinaryWriter(networkStream)
reader = New BinaryReader(networkStream)


writer.Write("Hello, Washington!!!")

Catch e As Exception
Console.WriteLine(e.ToString())

End Try


End Sub

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!