Connecting Tech Pros Worldwide Forums | Help | Site Map

Extract Bytes Sent To Server via TCP?

Newbie
 
Join Date: May 2009
Posts: 13
#1: May 17 '09
Hi :D.
I have a simple client and a simple server program. I am sending a byte array to the server from the client.

I am having difficulty extracting the byte data sent to the server program.

Server:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Net.Sockets
  2. Imports System.Threading
  3. Imports System.IO
  4. Imports System.Net
  5.  
  6. Module Module1
  7.  
  8.     Dim IPA As IPAddress = IPAddress.Parse("192.168.1.65")
  9.     Dim Listener As New TcpListener(IPA, 65535)
  10.     Dim Client As New TcpClient
  11.     Dim Message As String = ""
  12.  
  13.     Sub Main()
  14.  
  15.         Listener.Start()
  16.  
  17.         While Listener.Pending = False
  18.  
  19.             Message = ""
  20.  
  21.             Client = Listener.AcceptTcpClient()
  22.  
  23.             'Create Stream to Read and Write to
  24.             Dim Stream As NetworkStream = Client.GetStream()
  25.  
  26.             Dim Reader As New StreamReader(Stream)
  27.  
  28.             'While Reader.Peek > -1
  29.             '    Message = Message + Convert.ToChar(Reader.Read()).ToString
  30.             'End While
  31.  
  32.             'Console.WriteLine(Message)
  33.  
  34.             Dim BReader As New BinaryReader(Stream)
  35.  
  36.             'BReader.ReadByte()
  37.  
  38.             Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(BReader.ReadString)
  39.  
  40.             Console.WriteLine(data)
  41.             Console.WriteLine("<<")
  42.  
  43. End Sub
  44.  
  45. End Module
  46.  
Client:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Net.Sockets
  2. Imports System.Threading
  3. Imports System.IO
  4. Imports System.Net
  5.  
  6. Class Window1
  7.  
  8.     Dim Listener As New TcpListener(65534)
  9.     Dim Client As New TcpClient
  10.     Dim Message As String = ""
  11.  
  12.     Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
  13.  
  14.         Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
  15.         ListThread.Start()
  16.  
  17.     End Sub
  18.  
  19.     Sub Listening()
  20.  
  21.         Listener.Start()
  22.  
  23.         While Listener.Pending = False
  24.  
  25.             Message = ""
  26.  
  27.             Client = Listener.AcceptTcpClient()
  28.  
  29.             'Create Stream to Read and Write to
  30.             Dim Stream As NetworkStream = Client.GetStream()
  31.  
  32.             Dim Reader As New StreamReader(Stream)
  33.  
  34.             While Reader.Peek > -1
  35.                 Message = Message + Convert.ToChar(Reader.Read()).ToString
  36.             End While
  37.  
  38.  
  39.         End While
  40.  
  41.     End Sub
  42.  
  43.     Sub SendData()
  44.  
  45.         Client = New TcpClient("192.168.1.65", 65535)
  46.  
  47.         Dim Writer As New StreamWriter(Client.GetStream())
  48.  
  49.         Dim X As Byte
  50.  
  51.         Dim XYZ() As Byte = New Byte(2) {}
  52.  
  53.         X = 25
  54.  
  55.         XYZ(0) = 1
  56.         XYZ(1) = 2
  57.         XYZ(2) = 3
  58.  
  59.         Writer.Write(XYZ)
  60.  
  61.         Writer.Flush()
  62.  
  63.     End Sub
  64.  
  65.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
  66.  
  67.         SendData()
  68.  
  69.     End Sub
  70.  
  71. End Class
  72.  
  73.  
My intent is to send packets of data to and from the Server and Client.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#2: May 17 '09

re: Extract Bytes Sent To Server via TCP?


Quote:
I am having difficulty extracting the byte data sent to the server program.
Could you be a bit less vague?
You are getting exception errors?
You are getting communication but don't know how to translate the byte data back to readable text?
You are getting one way communication?

A lot of things look a little 'off' to me, but maybe that's because VB isn't my chosen language.
Both server and client are talking to 192.168.1.65
You have a byte array of {1,2,3} that I think you are trying to turn into characters, but those are not visible characters:
ascii 1 = start of heading
ascii 2 = start of text
ascii 3 = end of text
I can't see as the byte x (set to 25) has any purpose or is being used.
Newbie
 
Join Date: May 2009
Posts: 13
#3: May 17 '09

re: Extract Bytes Sent To Server via TCP?


Ive managed to get it working with the following:

Listener
Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  2.         Listener.Start()
  3.         While Listener.Pending = False
  4.             Client = Listener.AcceptTcpClient()
  5.             Dim Stream As NetworkStream = Client.GetStream()
  6.             Dim data As Byte() = New [Byte](256) {}
  7.             Dim bytes As Int32 = Stream.Read(data, 0, data.Length)
  8.             If data(0) = 1 Then Console.WriteLine("1")
  9.         End While
  10.     End Sub
  11.  
SendData
Expand|Select|Wrap|Line Numbers
  1. Sub SendData()
  2.         Client = New TcpClient("127.0.0.1", 65535)
  3.         Dim s = Client.GetStream()
  4.         Dim XYZ As Byte() = New Byte(2) {}
  5.         XYZ(0) = 1
  6.         XYZ(1) = 2
  7.         XYZ(2) = 3
  8.         s.Write(XYZ, 0, XYZ.Length)
  9.         s.Close()
  10.         Client.Close()
  11.     End Sub
  12.  
But I have to send the data twice for it to show up in the server.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#4: May 17 '09

re: Extract Bytes Sent To Server via TCP?


Quote:
But I have to send the data twice for it to show up in the server.
Does it show up twice at the receiving end?
Are you even sure about what you are receiving? Because it looks like you only confirm the first byte.
Expand|Select|Wrap|Line Numbers
  1. If data(0) = 1 Then Console.WriteLine("1")
Perhaps you should output everything you receive to the console so you can be certain of what you receive. Then once you know what you are getting, you can start adding qualifications on it.
Newbie
 
Join Date: May 2009
Posts: 13
#5: May 17 '09

re: Extract Bytes Sent To Server via TCP?


Basically (even without the qualifications) on my first send, nothing shows up in the console at the server end. Then on my second send the expected data shows up.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: May 18 '09

re: Extract Bytes Sent To Server via TCP?


I was going to say, if you want bytes, StreamReader was the wrong object to use
Newbie
 
Join Date: May 2009
Posts: 13
#7: May 19 '09

re: Extract Bytes Sent To Server via TCP?


What would u recommend?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#8: May 21 '09

re: Extract Bytes Sent To Server via TCP?


Any Stream object should be fine, using NetworkStream (the actual object) is more then good.
Reply