Is there any way to detect that I am no longer receiving data on a TCP connection in C#. For example, I have the following program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace SocketTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);

            Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                host.Connect(ip);
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.ToString(),"ERROR!",MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string strTest = "help\r\n";
            host.Send(Encoding.ASCII.GetBytes(strTest));
            byte[] data = new byte[1024];
            int receivedDataLength = host.Receive(data);
            string stringData = Encoding.ASCII.GetString(data,0,receivedDataLength);
            MessageBox.Show(stringData);
            host.Shutdown(SocketShutdown.Both);
            host.Close();
        }
    }
}

However, there is more data than what stringData is showing. Is there any to keep adding to the stringData? (short of increasing the size of byte[] data because that works as well) I guess what I'm trying to ask is, is there a way to tell when I'm done receiving data? I've tried using while or do/while, but I keep ending up in infinite loops.

Thanx,
~Atrus

I don't undersand very well your question there are many ways to establish communication using socket I give u a peace of code that I used in one of my projects

using System;
using System.Net.Sockets;

//This code is used for the client side:

public class Client
{
static public void Main( string[] Args )
{
TCPClient ToServer;
try
{
//new instance
ToServer = new TCPClient("localHost", 10);
}
catch
{
Console.WriteLine(
"Connection failed to server);
return;
}
//Network stream
NetworkStream networkStream = ToServer.GetStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
try
{
string outputString;
// read the data from the host and display it
{
outputString = streamReader.ReadLine();
Console.WriteLine(outputString);
streamWriter.WriteLine("Client Message");
Console.WriteLine("Client Message");
streamWriter.Flush();
}
}
catch
{
Console.WriteLine("Something wrong from server");
}
//Close the networkstream
networkStream.Close();
}
}

//Now This is code used in the server side

public class Server
{
public static void Main()
{
TCPListener oTcpListener = new TCPListener(10);
oTcpListener.Start();
Socket socketForClient = oTcpListener.Accept();
//Tell us if the client weather the client is connected or not
if (socketForClient.Connected)
{
//Tell us if the client weather the client is connected or not
Console.WriteLine("Client connected");
//use a network stream is very helpful
NetworkStream networkStream = new NetworkStream(socketForClient);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
string oString = "Sending";
streamWriter.WriteLine(oString);
Console.WriteLine(oString);
streamWriter.Flush();
oString = streamReader.ReadLine();
Console.WriteLine(oString);
//Close the stream reader
streamReader.Close();
//Close the network stream
networkStream.Close();
//close the stream writer
streamWriter.Close();
}
socketForClient.Close();
Console.WriteLine("Exiting...");
}
}

This article has been dead for over six months. Start a new discussion instead.