.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Web Links

how to know how much TCPClient.sendbufferSize to use

Posted By:      Posted Date: October 14, 2010    Points: 0   Category :.NET Framework
I have a simple based File transfer between a Server and a Client using TCP, the default buffer that is set is 8192 bytes. I was able to set it to 300k and a 10mb file was transfered successfully through Localhost in less than 2 seconds. My question is how can I ever determine what is the best buffer rate for non-local activities? If the server is stationed across the world and the client the opposite side with a 5MB Cable Internet how much can that handle? Would the data get damaged? would everything eventually get through even though the buffer rate is set to 300k or 1MB? What is the best way to figure out the fastest rate?

View Complete Post

More Related Resource Links

.net4 TcpClient fails when NetworkStream.ReadTimeout is set

Hi, I have a problem implementing a Read() with timeout. I have a TcpClient obtained via listener: let listener = new TcpListener(IPAddress.Any, 6667) listener.Start() let client = listener.AcceptTcpClient() and I tried this code to have a Read with timeout: let stream = client.GetStream() stream.ReadTimeout <- 5000 try Some(stream.ReadByte()) with _ -> None which works fine. The problem is that, when the timeout happens, client.Connected becomes false !! And that's false because the socket is still alive and I'm still able to Write/Read data through the stream. Also, because client.Connected is false, I'm not able anymore to invoke the GetStream() method. I think this is a big bug, isn't it? Marco --------------------- Edit: I forgot to mention that it happens the same setting either  client.ReceiveTimeout  and/or  stream.ReadTimeout...

TCPClient on Windows Service

I wrote code to comunicate two pc. For testing I made it with windows forms, in app A I have a server wish is waiting for a request from app B. When I tested them in windows forms all worked fine but I converted them into windows service app and the client (app B) can´t comunicate with the server. If I use the windows forms server and windows service client all works fine, so, I think it could be a problem with security or permissions when I use the windows service server.

I tried to send messages to de event viewer in the client with microsoft enterprice library but I can´t get anything. I don´t have idea what is happening. Could anyone help me???

I´m using TCPListener in my server in the port 13015 and I'm checking for pending request with a system.threading.timer...

TryConnect, TcpClient.Connect that doesn't throws exception, it returns bool

using System;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

namespace TryConnect
  class Program
    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern int WSAConnect(
                     [In] IntPtr socketHandle,
                     [In] byte[] socketAddress,
                     [In] int socketAddressSize,
                     [In] IntPtr inBuffer,
                     [In] IntPtr outBuffer,
                     [In] IntPtr sQOS,
                     [In] IntPtr gQOS

    [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern IntPtr WSASocket(
                        [In] AddressFamily addressFamily,
                        [In] SocketType socketType,
                        [In] ProtocolType protocolType,
                        [In] IntPtr protocolInfo,
                        [In] uint group,
                        [In] int flags

    [DllImport("ws2_32.dll", ExactSpelling = true, SetLastError = true)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    internal static extern int closesocket(
                       [In] IntPtr socketHandle


TCPClient and Read not returning complete data unless I use thread.sleep


Hello experts, here is my code that I have a question on:

 Dim receivedbyte(1024) As Byte
        Dim myread As Integer = 0
        Dim myresponsesb As New StringBuilder()
            myread = myns.Read(receivedbyte, 0, receivedbyte.Length)
            myresponsesb.Append(System.Text.Encoding.UTF8.GetString(receivedbyte, 0, myread))

        Loop While (myns.DataAvailable)

In the above, myns is a networkstream that is initialized with a tcpclient that is communicating with a XMPP server. basically, iam getting back xml messages.

The issue with the above is that if I dont use thread.sleep(500), the complete data is not returned. Why is that? I have tried to use a streamreader instead of read and using that method i can get complete data from the stream. What am I doing wrong with the Read method of networkstream?

SECOND: The XML being returned from the server is not well formed - the closing tag for one of the tags is missing and I am unable to use XDocument to parse this XML because of this. What could be the issue here? Is it my code or something on server end?

Also, what technique should i use to make the returned XML well formed so that I can use XDOCUMENT and LINQ to parse this?


Storing TcpClient object in session



I am experimenting with an asp.net application wherein I do the following things (simplified for the purpose of this discussion):

1. Take user's gmail username/password (don't fret, I am not doing any nasty stuff Wink).

2. Create TcpClient object and connect it to the Gmail's POP email server using the provided username/password.

3. I am going to use this TcpClient object to download mails for the user.

Following is the problem I face:

1. Say I have presented the user with the subject lines of all his emails.

2. Now he clicks one subject line to download the message. (This will create the post-back to the aspx page)

3. When I receive the post back, will I be able to re-use the same TcpClient object which I connected to the POP server, if I store that object as a Session variable?

I doubt the streams associated with the object will not remain valid.

Is this even the right method to achieve what I am trying to achieve?



C#: How to determine if a TCPClient has been disconnected.

I've had a TCPClient disconnect but still have the connected property == true. So it seams the only way to tell is if a Read from the network stream returns 0 bytes. So right now I'm assuming it is disconnected if it returns 0 bytes. I was wondering if there is a better way?

TcpClient with SSLStream via Proxy is this possible?


Hi, please help me!

I spent hours with google and msdn, but no result.


I communicate with a server with tcpclient and sslStream, and it works fine, but there is a new problem.

I have to do this with static IP adress, but cause of my service this can't possible.


So I have a server with static IP and I installed a squid proxy on it.


Can I communicate with TCPclient with SSLStream through PROXY?

How to add proxy host and port to a tcpclient with sslstream connection?

It would be very important...


Thank you very much!

TCPClient Winsock Error 10045


While working with the TCPClient/Listener, I ran into the error "A connect request was made on an already connected socket". Upon further inspection I have found that it is the Winsock Error 10045, Operation Not Supported.

I have looked upon many different Posts in the MSDN forums and elsewhere and have been unable to find out what i am doing wrong. The TCPListener is working Perfectly (As far as i have been able to see, due to being unable to connect).

My code for the TCPClient is as follows:

   IPEndPoint LPoint = new IPEndPoint(IPAddress.Parse(""), 8001);

   TcpClient LClient = new TcpClient("", 8001);

   LClient.Connect(LPoint); ;

   if (LClient.Connected)


    listBox1.Items.Add("Connected on");

    using (Stream stream = LClient.GetStream())


     string str = "Test";

     ASCIIEncoding asen = new ASCIIEncoding();


Looking 4 a simple TCPClient.BeginConnect sample...


Networking code, makes my head hurt.

I am trying to create a class that implements a form of connectionless messaging. I have my DoSendMessage method  that is using TCPClient.Connect but the problem is that if the server is not availible the Connect method hangs for about 5 secs while it tries to connect. Meaning my application also hangs. Very bad....

What I am looking for is a COMPLETE and WORKING sample that is simple and uses the asynronous method for connecting using TCPClient.BeginConnect. But I have not been able to track down any easy to fallow sample code.

Does anybody know of any tutorials or links that can help me out. Thanks in advance!

My code is as fallows ...

  Private Sub DoSendMessage(ByVal Server As IPAddress, ByVal Port As Integer, ByVal Message As String, ByVal Raiseevents As Boolean)
        If Server Is Nothing Then Exit Sub
        ' If Server = String.Empty Or Message = String.Empty Then Exit Sub
        ' If Server.Contains(":") Then Server = Server.Substring(0, Server.IndexOf(":"))

            ' Create a TcpClient.
            ' Note, for this client to work you need to have a TcpServer
            ' connected to the same address as specified by the server, port
            ' combination.
            Dim client As New TcpClient
            client.ExclusiveAddressUse = False
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend