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() 3 2925
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()
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()
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() This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |