473,385 Members | 1,848 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,385 software developers and data experts.

TcpClient and images

Hi,

I'm writing a client/server application in which the client send a series of
screenshots to the server to be saved using the tcpclient.
in most cases the first screenshot is transmitted ok and arrives at the
server but from after that i only a couple of KB from the start of the file
which cases the picture to display only the very top (of the screen).
All the pictures are saved at the client side before sent to the server and
they are always good and "full", so the problems seem to be from the
transmition part or after that.

please help ...
****** Server Code:
Const portNo As Integer = 8080
Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Any
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()

Dim tcpClient As TcpClient

tcpClient = listener.AcceptTcpClient()

tcpClient.ReceiveBufferSize = 400000

Dim lingerOption As New LingerOption(True, 10)
tcpClient.LingerState = lingerOption

Dim NWStream As NetworkStream = tcpClient.GetStream

Dim bytesToRead(200000) As Byte
Dim numBytesRead As Integer = 0

'---read incoming stream
If NWStream.CanRead Then
Do
numBytesRead = numBytesRead + NWStream.Read(bytesToRead,
numBytesRead, tcpClient.ReceiveBufferSize)
Loop While NWStream.DataAvailable
NWStream.Close()
End If

Dim FileName = DateTimeForFile() & ".jpg"
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(FileName,
System.IO.FileMode.Create, _
System.IO.FileAccess.Write)
fs.Write(bytesToRead, 0, numBytesRead)
fs.Close()

MsgBox(numBytesRead)

tcpClient.Close()

listener.Stop()
****** Client Code:
Dim FileName = DateTimeForFile() & ".jpg"
< Code to save the screenshot on the client>
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(Me.TextBox_Server.Text, portNo)
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim fs As FileStream
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim bytesToSend(fs.Length) As Byte
Dim numBytesRead As Integer = fs.Read(bytesToSend, 0,
bytesToSend.Length)
fs.Close()
NWStream.Write(bytesToSend, 0, numBytesRead)
tcpClient.Close()
Aug 17 '05 #1
3 3001
Hi!

There are three things i found in your code which could be related to your
problem.

1. On the client side you try to read the whole file content in one call to
the Stream.Read method. It is not guaranteed, that the whole data block is
read at once (I think that's even true for FileStream). You should examine
the number of bytes actually read and retry reading until the whole file has
been read.

2. If you read the data comming from the network faster then it arrives,
then I think that NetworkStream.DataAvailable can return false even if
further data will be received. Have you thought about sending some sort of
header with each image, containing the actual image size?

3. In applications that maintain multiple open NetworkStreams I observed
that setting receive buffers above a specific size resulted in blocking
streams. No data could be received at all. The threshold I found was
somewhere at 80KB (as far as I remember) for the sum of all NetworkStream
receive buffers. I'm not sure whether (and in fact I don't think that) this
also is an issue with a single connection but nevertheless you could try to
reduce your receive-bfufer size.

Cheers
- Markus

"?????" <@discussions.microsoft.com> schrieb im Newsbeitrag
news:94**********************************@microsof t.com...
Hi,

I'm writing a client/server application in which the client send a series
of
screenshots to the server to be saved using the tcpclient.
in most cases the first screenshot is transmitted ok and arrives at the
server but from after that i only a couple of KB from the start of the
file
which cases the picture to display only the very top (of the screen).
All the pictures are saved at the client side before sent to the server
and
they are always good and "full", so the problems seem to be from the
transmition part or after that.

please help ...
****** Server Code:
Const portNo As Integer = 8080
Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Any
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()

Dim tcpClient As TcpClient

tcpClient = listener.AcceptTcpClient()

tcpClient.ReceiveBufferSize = 400000

Dim lingerOption As New LingerOption(True, 10)
tcpClient.LingerState = lingerOption

Dim NWStream As NetworkStream = tcpClient.GetStream

Dim bytesToRead(200000) As Byte
Dim numBytesRead As Integer = 0

'---read incoming stream
If NWStream.CanRead Then
Do
numBytesRead = numBytesRead +
NWStream.Read(bytesToRead,
numBytesRead, tcpClient.ReceiveBufferSize)
Loop While NWStream.DataAvailable
NWStream.Close()
End If

Dim FileName = DateTimeForFile() & ".jpg"
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(FileName,
System.IO.FileMode.Create, _
System.IO.FileAccess.Write)
fs.Write(bytesToRead, 0, numBytesRead)
fs.Close()

MsgBox(numBytesRead)

tcpClient.Close()

listener.Stop()
****** Client Code:
Dim FileName = DateTimeForFile() & ".jpg"
< Code to save the screenshot on the client>
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(Me.TextBox_Server.Text, portNo)
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim fs As FileStream
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim bytesToSend(fs.Length) As Byte
Dim numBytesRead As Integer = fs.Read(bytesToSend, 0,
bytesToSend.Length)
fs.Close()
NWStream.Write(bytesToSend, 0, numBytesRead)
tcpClient.Close()

Aug 17 '05 #2
1. I've checked the reading of the file and saw that always to read command
reads the whole file in one call.
2. I tried to limit the reading from the stream based on the file size and
not by the dataavailble property and that fixes the problem - but i don't
know how to send the file size from the client to the server ?

"Markus Minichmayr" wrote:
Hi!

There are three things i found in your code which could be related to your
problem.

1. On the client side you try to read the whole file content in one call to
the Stream.Read method. It is not guaranteed, that the whole data block is
read at once (I think that's even true for FileStream). You should examine
the number of bytes actually read and retry reading until the whole file has
been read.

2. If you read the data comming from the network faster then it arrives,
then I think that NetworkStream.DataAvailable can return false even if
further data will be received. Have you thought about sending some sort of
header with each image, containing the actual image size?

3. In applications that maintain multiple open NetworkStreams I observed
that setting receive buffers above a specific size resulted in blocking
streams. No data could be received at all. The threshold I found was
somewhere at 80KB (as far as I remember) for the sum of all NetworkStream
receive buffers. I'm not sure whether (and in fact I don't think that) this
also is an issue with a single connection but nevertheless you could try to
reduce your receive-bfufer size.

Cheers
- Markus

"?????" <@discussions.microsoft.com> schrieb im Newsbeitrag
news:94**********************************@microsof t.com...
Hi,

I'm writing a client/server application in which the client send a series
of
screenshots to the server to be saved using the tcpclient.
in most cases the first screenshot is transmitted ok and arrives at the
server but from after that i only a couple of KB from the start of the
file
which cases the picture to display only the very top (of the screen).
All the pictures are saved at the client side before sent to the server
and
they are always good and "full", so the problems seem to be from the
transmition part or after that.

please help ...
****** Server Code:
Const portNo As Integer = 8080
Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Any
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()

Dim tcpClient As TcpClient

tcpClient = listener.AcceptTcpClient()

tcpClient.ReceiveBufferSize = 400000

Dim lingerOption As New LingerOption(True, 10)
tcpClient.LingerState = lingerOption

Dim NWStream As NetworkStream = tcpClient.GetStream

Dim bytesToRead(200000) As Byte
Dim numBytesRead As Integer = 0

'---read incoming stream
If NWStream.CanRead Then
Do
numBytesRead = numBytesRead +
NWStream.Read(bytesToRead,
numBytesRead, tcpClient.ReceiveBufferSize)
Loop While NWStream.DataAvailable
NWStream.Close()
End If

Dim FileName = DateTimeForFile() & ".jpg"
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(FileName,
System.IO.FileMode.Create, _
System.IO.FileAccess.Write)
fs.Write(bytesToRead, 0, numBytesRead)
fs.Close()

MsgBox(numBytesRead)

tcpClient.Close()

listener.Stop()
****** Client Code:
Dim FileName = DateTimeForFile() & ".jpg"
< Code to save the screenshot on the client>
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(Me.TextBox_Server.Text, portNo)
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim fs As FileStream
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim bytesToSend(fs.Length) As Byte
Dim numBytesRead As Integer = fs.Read(bytesToSend, 0,
bytesToSend.Length)
fs.Close()
NWStream.Write(bytesToSend, 0, numBytesRead)
tcpClient.Close()


Aug 19 '05 #3
In your scenario the easiest way to do this is to use System.IO.BinaryWriter
and System.IO.BinaryReader. Do something like the following (C# Syntax):

Sender:
....
using (BinaryWriter imageWriter = new BinaryWriter(outputStream))
{
imageWriter.Write((uint) MagicNumber); // You should use a magic
number to detect communication errors
imageWriter.Write((uint) ProtocolVersion); // You might want to
include a version number of your protocol
imageWriter.Write((int) imageBytes.Length); // Write the image size
imageWriter.Write(imageBytes); // Write the actual
image
}
....
Receiver:

....
byte[] imageBytes;

using (BinaryReader imageReader = new BinaryReader(inputStream))
{
uint magicNumber = imageReader.ReadUInt32();
uint protocolVersion = imageReader.ReadUInt32();

... check magic number and protocol version and do error handling ...

int imageSize = imageReader.ReadInt32();
imageBytes = imageReader.ReadBytes(imageSize); // The full
data block will be read here.
}

.... do whatever you want ...
Don't forget to handle communication errors like timeouts!

- Markus
"?????" <@discussions.microsoft.com> schrieb im Newsbeitrag
news:EA**********************************@microsof t.com...
1. I've checked the reading of the file and saw that always to read
command
reads the whole file in one call.
2. I tried to limit the reading from the stream based on the file size and
not by the dataavailble property and that fixes the problem - but i don't
know how to send the file size from the client to the server ?

"Markus Minichmayr" wrote:
Hi!

There are three things i found in your code which could be related to
your
problem.

1. On the client side you try to read the whole file content in one call
to
the Stream.Read method. It is not guaranteed, that the whole data block
is
read at once (I think that's even true for FileStream). You should
examine
the number of bytes actually read and retry reading until the whole file
has
been read.

2. If you read the data comming from the network faster then it arrives,
then I think that NetworkStream.DataAvailable can return false even if
further data will be received. Have you thought about sending some sort
of
header with each image, containing the actual image size?

3. In applications that maintain multiple open NetworkStreams I observed
that setting receive buffers above a specific size resulted in blocking
streams. No data could be received at all. The threshold I found was
somewhere at 80KB (as far as I remember) for the sum of all NetworkStream
receive buffers. I'm not sure whether (and in fact I don't think that)
this
also is an issue with a single connection but nevertheless you could try
to
reduce your receive-bfufer size.

Cheers
- Markus

"?????" <@discussions.microsoft.com> schrieb im Newsbeitrag
news:94**********************************@microsof t.com...
> Hi,
>
> I'm writing a client/server application in which the client send a
> series
> of
> screenshots to the server to be saved using the tcpclient.
> in most cases the first screenshot is transmitted ok and arrives at the
> server but from after that i only a couple of KB from the start of the
> file
> which cases the picture to display only the very top (of the screen).
> All the pictures are saved at the client side before sent to the server
> and
> they are always good and "full", so the problems seem to be from the
> transmition part or after that.
>
> please help ...
>
>
> ****** Server Code:
> Const portNo As Integer = 8080
> Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Any
> Dim listener As New TcpListener(localAdd, portNo)
> listener.Start()
>
> Dim tcpClient As TcpClient
>
> tcpClient = listener.AcceptTcpClient()
>
> tcpClient.ReceiveBufferSize = 400000
>
> Dim lingerOption As New LingerOption(True, 10)
> tcpClient.LingerState = lingerOption
>
> Dim NWStream As NetworkStream = tcpClient.GetStream
>
> Dim bytesToRead(200000) As Byte
> Dim numBytesRead As Integer = 0
>
> '---read incoming stream
> If NWStream.CanRead Then
> Do
> numBytesRead = numBytesRead +
> NWStream.Read(bytesToRead,
> numBytesRead, tcpClient.ReceiveBufferSize)
> Loop While NWStream.DataAvailable
> NWStream.Close()
> End If
>
> Dim FileName = DateTimeForFile() & ".jpg"
> Dim fs As System.IO.FileStream
> fs = New System.IO.FileStream(FileName,
> System.IO.FileMode.Create, _
> System.IO.FileAccess.Write)
> fs.Write(bytesToRead, 0, numBytesRead)
> fs.Close()
>
> MsgBox(numBytesRead)
>
> tcpClient.Close()
>
> listener.Stop()
>
>
> ****** Client Code:
> Dim FileName = DateTimeForFile() & ".jpg"
> < Code to save the screenshot on the client>
> Dim tcpClient As New System.Net.Sockets.TcpClient
> tcpClient.Connect(Me.TextBox_Server.Text, portNo)
> Dim NWStream As NetworkStream = tcpClient.GetStream
> Dim fs As FileStream
> fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
> Dim bytesToSend(fs.Length) As Byte
> Dim numBytesRead As Integer = fs.Read(bytesToSend, 0,
> bytesToSend.Length)
> fs.Close()
> NWStream.Write(bytesToSend, 0, numBytesRead)
> tcpClient.Close()


Aug 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
2
by: Theo | last post by:
Hi, I am using a tcpclient and a tcplistener to send and receive packets. The client sends a packet and the listener replies with another one. I can send 2 packets and get an answer from the...
3
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
3
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
0
by: Torsten Brasch | last post by:
Hi All and Happy New Year ;) I have a very strange problem with System.Net.Sockets.TcpClient(). For some reason, the number of bytes I can receive is limited to 5460 bytes. I made sure that the...
1
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
3
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
0
by: etnaelk | last post by:
Hi all, I have a real bugger of a problem that I just haven't been able to figure out. I am working on writing my own proxy server in C# using TcpListener, TcpClient, HttpWebRequest/Response and...
5
by: puzzlecracker | last post by:
It looks like i need to get IPEndPoint first, but I cannot figure out from msdn the eventual obtainment of machine name and port number. Please suggest a solution. Thanks
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.