Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote a console application. i used socket programming . i get error in below cod. transfer speed of data is 10ms . after a period of time i get a error on " while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)" line .

C#
while (true)
{
    TcpClient tcpClient = tcpListener.AcceptTcpClient();
    NetworkStream stream = tcpClient.GetStream();

    String data = null;
    int i;
    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
    {
        // Translate data bytes to a ASCII string.
        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        Console.WriteLine("Received: {0}", data);
       ...
    }


error:
HTML
An unhandled exception of type 'System.IO.IOException' occurred in System.dll Additional information: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Posted
Comments
Richard MacCutchan 13-Apr-14 4:20am    
The message tells you everything you need to know. The device at the other end of the connection is not responding.
[no name] 13-Apr-14 4:33am    
The real question is where and why. Looks like you are trying to reconnect after connection established - is that what you want? Time to roll out the debugger.
neda1367 13-Apr-14 5:07am    
I get a lot of data in a few minute. but then I receive error.

below is all my cod:
namespace consolprogramming
{
class Program
{
static void Main(string[] args)
{
//byte[] msg = Encoding.UTF8.GetBytes("thanks ...");
byte[] bytes = new byte[512];
try
{

XmlDocument DOC = new XmlDocument();

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\k.xml";

//DOC.Load(path);

// XmlNode root;

DataSet ds = new DataSet("kDs");
IPHostEntry ipHost = Dns.GetHostEntry("");
// Gets first IP address associated with a localhost
IPAddress add = ipHost.AddressList[3];


TcpListener tcpListener = new TcpListener(add, 6000);
tcpListener.Start();

Console.WriteLine("Waiting for a connection...");


ds.Namespace = "sample";
DataTable stdTable = new DataTable("azmon");
DataColumn col1 = new DataColumn("data");
stdTable.Columns.Add(col1);
ds.Tables.Add(stdTable);

//Add azmon Data to the table




TcpClient tcpClient = tcpListener.AcceptTcpClient();


NetworkStream stream = tcpClient.GetStream();
DataRow newRow;
String data = null;
while (true)
{
int i;
//try
//{
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);

// Process the data sent by the client.
//data = data.ToUpper();

byte[] msg = System.Text.Encoding.ASCII.GetBytes("thanks");

// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", "thanks");

newRow = stdTable.NewRow();
newRow["data"] = data;

stdTable.Rows.Add(newRow);

ds.AcceptChanges();
ds.WriteXml(path);
}
//}
//catch (IOException ioe)
//{

//}

// Shutdown and end connection
tcpClient.Close();
}

}
catch (SocketException e)
{
Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
Console.ReadKey();
}
}

}
}
1) I have a adhoc network with 2 system. one is a hardware that sends data in every 10 ms. I dont want to have limitation in sending data. 2) every data is 512 byte. i receive a lot of number of this every 512 byte but after period of time i do not receive rest of them. and get error. 3)where should i close my connection?

4) I Check the line number where the exception occurred. It failing on the Read()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900