473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Extract Bytes Sent To Server via TCP?

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.
May 17 '09 #1
7 2678
tlhintoq
3,525 Expert 2GB
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.
May 17 '09 #2
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.
May 17 '09 #3
tlhintoq
3,525 Expert 2GB
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.
May 17 '09 #4
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.
May 17 '09 #5
Plater
7,872 Expert 4TB
I was going to say, if you want bytes, StreamReader was the wrong object to use
May 18 '09 #6
What would u recommend?
May 18 '09 #7
Plater
7,872 Expert 4TB
Any Stream object should be fine, using NetworkStream (the actual object) is more then good.
May 21 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: bastardx | last post by:
Hi I wonder if you can point me to some scripts because I don't know perl. I think that perl is best for what I want to do. I have special kind of logs which looks like following: ID - is client...
44
by: RB | last post by:
How to extract bytes from long, starting from the last byte? For example, I have a long number: 0x12345678 I need to represent it as the following bytes list: 0x78, 0x56, 0x34, 0x12 Thanks in...
1
by: | last post by:
one thing to keep in mind is that i've written multiple tcp apps in .net 2002 and 2003, and haven't seen this problem before. I have a simple socket app where a client sends some # of bytes to...
3
by: Vjay77 | last post by:
I posted this question, but I pressed 'post' and it disappeared. So once again: Problem: I need to go to lets say www.site.com/page.html Imagine that this html code is 6 mb long. I need to...
13
by: Shailesh Humbad | last post by:
Here is an advanced PHP question. Can anyone think of a way to detect the number of bytes written to output when a script is aborted? I am sending a large file to the client, and I want to record...
2
by: nishi.hirve | last post by:
Hello, I am writing 1 client-server application in which I have written my server in C on linux system and client in C#. My server is sending image bytes of that image size to the client....
1
by: William Connery | last post by:
Hi, I have a small python program with e-mail capabilities that I have pieced together from code snippets found on the internet. The program uses the smtplib module to successfully send an...
7
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and...
5
by: jaco.versfeld | last post by:
Hi There, I have a basic TCP client and TCP server in C++. The TCP client connects to the server, and after a setup phase starts to transmit a file to the TCP server using multiple packets...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.