473,903 Members | 4,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IPAd dress = System.Net.IPAd dress.Any
Dim listener As New TcpListener(loc alAdd, portNo)
listener.Start( )

Dim tcpClient As TcpClient

tcpClient = listener.Accept TcpClient()

tcpClient.Recei veBufferSize = 400000

Dim lingerOption As New LingerOption(Tr ue, 10)
tcpClient.Linge rState = lingerOption

Dim NWStream As NetworkStream = tcpClient.GetSt ream

Dim bytesToRead(200 000) As Byte
Dim numBytesRead As Integer = 0

'---read incoming stream
If NWStream.CanRea d Then
Do
numBytesRead = numBytesRead + NWStream.Read(b ytesToRead,
numBytesRead, tcpClient.Recei veBufferSize)
Loop While NWStream.DataAv ailable
NWStream.Close( )
End If

Dim FileName = DateTimeForFile () & ".jpg"
Dim fs As System.IO.FileS tream
fs = New System.IO.FileS tream(FileName,
System.IO.FileM ode.Create, _
System.IO.FileA ccess.Write)
fs.Write(bytesT oRead, 0, numBytesRead)
fs.Close()

MsgBox(numBytes Read)

tcpClient.Close ()

listener.Stop()
****** Client Code:
Dim FileName = DateTimeForFile () & ".jpg"
< Code to save the screenshot on the client>
Dim tcpClient As New System.Net.Sock ets.TcpClient
tcpClient.Conne ct(Me.TextBox_S erver.Text, portNo)
Dim NWStream As NetworkStream = tcpClient.GetSt ream
Dim fs As FileStream
fs = New FileStream(File Name, FileMode.Open, FileAccess.Read )
Dim bytesToSend(fs. Length) As Byte
Dim numBytesRead As Integer = fs.Read(bytesTo Send, 0,
bytesToSend.Len gth)
fs.Close()
NWStream.Write( bytesToSend, 0, numBytesRead)
tcpClient.Close ()
Aug 17 '05 #1
3 3060
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.D ataAvailable 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.m icrosoft.com> schrieb im Newsbeitrag
news:94******** *************** ***********@mic rosoft.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.IPAd dress = System.Net.IPAd dress.Any
Dim listener As New TcpListener(loc alAdd, portNo)
listener.Start( )

Dim tcpClient As TcpClient

tcpClient = listener.Accept TcpClient()

tcpClient.Recei veBufferSize = 400000

Dim lingerOption As New LingerOption(Tr ue, 10)
tcpClient.Linge rState = lingerOption

Dim NWStream As NetworkStream = tcpClient.GetSt ream

Dim bytesToRead(200 000) As Byte
Dim numBytesRead As Integer = 0

'---read incoming stream
If NWStream.CanRea d Then
Do
numBytesRead = numBytesRead +
NWStream.Read(b ytesToRead,
numBytesRead, tcpClient.Recei veBufferSize)
Loop While NWStream.DataAv ailable
NWStream.Close( )
End If

Dim FileName = DateTimeForFile () & ".jpg"
Dim fs As System.IO.FileS tream
fs = New System.IO.FileS tream(FileName,
System.IO.FileM ode.Create, _
System.IO.FileA ccess.Write)
fs.Write(bytesT oRead, 0, numBytesRead)
fs.Close()

MsgBox(numBytes Read)

tcpClient.Close ()

listener.Stop()
****** Client Code:
Dim FileName = DateTimeForFile () & ".jpg"
< Code to save the screenshot on the client>
Dim tcpClient As New System.Net.Sock ets.TcpClient
tcpClient.Conne ct(Me.TextBox_S erver.Text, portNo)
Dim NWStream As NetworkStream = tcpClient.GetSt ream
Dim fs As FileStream
fs = New FileStream(File Name, FileMode.Open, FileAccess.Read )
Dim bytesToSend(fs. Length) As Byte
Dim numBytesRead As Integer = fs.Read(bytesTo Send, 0,
bytesToSend.Len gth)
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.D ataAvailable 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.m icrosoft.com> schrieb im Newsbeitrag
news:94******** *************** ***********@mic rosoft.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.IPAd dress = System.Net.IPAd dress.Any
Dim listener As New TcpListener(loc alAdd, portNo)
listener.Start( )

Dim tcpClient As TcpClient

tcpClient = listener.Accept TcpClient()

tcpClient.Recei veBufferSize = 400000

Dim lingerOption As New LingerOption(Tr ue, 10)
tcpClient.Linge rState = lingerOption

Dim NWStream As NetworkStream = tcpClient.GetSt ream

Dim bytesToRead(200 000) As Byte
Dim numBytesRead As Integer = 0

'---read incoming stream
If NWStream.CanRea d Then
Do
numBytesRead = numBytesRead +
NWStream.Read(b ytesToRead,
numBytesRead, tcpClient.Recei veBufferSize)
Loop While NWStream.DataAv ailable
NWStream.Close( )
End If

Dim FileName = DateTimeForFile () & ".jpg"
Dim fs As System.IO.FileS tream
fs = New System.IO.FileS tream(FileName,
System.IO.FileM ode.Create, _
System.IO.FileA ccess.Write)
fs.Write(bytesT oRead, 0, numBytesRead)
fs.Close()

MsgBox(numBytes Read)

tcpClient.Close ()

listener.Stop()
****** Client Code:
Dim FileName = DateTimeForFile () & ".jpg"
< Code to save the screenshot on the client>
Dim tcpClient As New System.Net.Sock ets.TcpClient
tcpClient.Conne ct(Me.TextBox_S erver.Text, portNo)
Dim NWStream As NetworkStream = tcpClient.GetSt ream
Dim fs As FileStream
fs = New FileStream(File Name, FileMode.Open, FileAccess.Read )
Dim bytesToSend(fs. Length) As Byte
Dim numBytesRead As Integer = fs.Read(bytesTo Send, 0,
bytesToSend.Len gth)
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.Binar yWriter
and System.IO.Binar yReader. Do something like the following (C# Syntax):

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

....
byte[] imageBytes;

using (BinaryReader imageReader = new BinaryReader(in putStream))
{
uint magicNumber = imageReader.Rea dUInt32();
uint protocolVersion = imageReader.Rea dUInt32();

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

int imageSize = imageReader.Rea dInt32();
imageBytes = imageReader.Rea dBytes(imageSiz e); // The full
data block will be read here.
}

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

- Markus
"?????" <@discussions.m icrosoft.com> schrieb im Newsbeitrag
news:EA******** *************** ***********@mic rosoft.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.D ataAvailable 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.m icrosoft.com> schrieb im Newsbeitrag
news:94******** *************** ***********@mic rosoft.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.IPAd dress = System.Net.IPAd dress.Any
> Dim listener As New TcpListener(loc alAdd, portNo)
> listener.Start( )
>
> Dim tcpClient As TcpClient
>
> tcpClient = listener.Accept TcpClient()
>
> tcpClient.Recei veBufferSize = 400000
>
> Dim lingerOption As New LingerOption(Tr ue, 10)
> tcpClient.Linge rState = lingerOption
>
> Dim NWStream As NetworkStream = tcpClient.GetSt ream
>
> Dim bytesToRead(200 000) As Byte
> Dim numBytesRead As Integer = 0
>
> '---read incoming stream
> If NWStream.CanRea d Then
> Do
> numBytesRead = numBytesRead +
> NWStream.Read(b ytesToRead,
> numBytesRead, tcpClient.Recei veBufferSize)
> Loop While NWStream.DataAv ailable
> NWStream.Close( )
> End If
>
> Dim FileName = DateTimeForFile () & ".jpg"
> Dim fs As System.IO.FileS tream
> fs = New System.IO.FileS tream(FileName,
> System.IO.FileM ode.Create, _
> System.IO.FileA ccess.Write)
> fs.Write(bytesT oRead, 0, numBytesRead)
> fs.Close()
>
> MsgBox(numBytes Read)
>
> tcpClient.Close ()
>
> listener.Stop()
>
>
> ****** Client Code:
> Dim FileName = DateTimeForFile () & ".jpg"
> < Code to save the screenshot on the client>
> Dim tcpClient As New System.Net.Sock ets.TcpClient
> tcpClient.Conne ct(Me.TextBox_S erver.Text, portNo)
> Dim NWStream As NetworkStream = tcpClient.GetSt ream
> Dim fs As FileStream
> fs = New FileStream(File Name, FileMode.Open, FileAccess.Read )
> Dim bytesToSend(fs. Length) As Byte
> Dim numBytesRead As Integer = fs.Read(bytesTo Send, 0,
> bytesToSend.Len gth)
> 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
9133
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 the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
2
7971
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 listener but it fails to send a third packet. I don't get any error messages. The client sends the packet successfully but it never arrives to the listener. This only happens when I send the packets one straight after the other. If I place a delay of 2...
3
28667
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 slowly, about 4 seconds between send and receive. In our production environment with hundreds of transactions it was truly costly. a while ago i changed de TcpClient object. Now i am using a Socket (System.Net.Sockets.Socket) object and it...
3
4704
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 My code is as below, but I never get a response. I'm assuming my transmission is ending at the null character (the debug statement certainly only outputs up until the null character). Any ideas?
0
1984
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 server really sends everything I expect it to send. I am actually sending XML messages and this limit is a pain in the ... Here is the code: -------------------------------------------------------------------------- Dim tcpclient As New...
1
4544
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 closes the connection so I can exit my listener code. Here is what I have tried. First I asked MSDN and they said..
3
343
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 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...
0
1482
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 NetworkStream. I have everything working pretty well except images won't load to their location. ------------------------------------------------------------------------------------------------------------------...
5
7348
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
10006
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11294
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10882
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4734
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.