Click here to Skip to main content
15,898,984 members
Home / Discussions / C#
   

C#

 
AnswerRe: Let the user write a line code Pin
Nuri Ismail20-Sep-09 22:41
Nuri Ismail20-Sep-09 22:41 
GeneralRe: Let the user write a line code Pin
bonzaiholding21-Sep-09 0:21
bonzaiholding21-Sep-09 0:21 
AnswerRe: Let the user write a line code Pin
dan!sh 20-Sep-09 22:43
professional dan!sh 20-Sep-09 22:43 
AnswerRe: Let the user write a line code Pin
Senseicads21-Sep-09 1:10
Senseicads21-Sep-09 1:10 
AnswerRe: Let the user write a line code Pin
PIEBALDconsult21-Sep-09 4:58
mvePIEBALDconsult21-Sep-09 4:58 
QuestionProblem in Connecting Excel File Pin
amaankhan20-Sep-09 21:27
amaankhan20-Sep-09 21:27 
AnswerRe: Problem in Connecting Excel File Pin
amaankhan20-Sep-09 21:33
amaankhan20-Sep-09 21:33 
QuestionAnother TCP Issue [UDATED] Pin
Harvey Saayman20-Sep-09 21:16
Harvey Saayman20-Sep-09 21:16 
Hey guys

I'm trying to communicate with an external device(proximity card reader) over TCP/IP.

At the moment im able to send the device a packet and receive a reply from the device. The problem is, that when i try to send a second packet I get an IOException at NetworkStream.Write(Buffer, 0, Buffer.Length); stating "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."

This makes no sense to me, the device does not close the connection, I am 100% sure of that.

Here is the code of my test 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;
using System.Threading;

namespace NewBadgerTest
{
    public partial class Form1 : Form
    {
        TcpClient BadgerConnection;

        public Form1()
        {
            InitializeComponent();

            try
            {
                BadgerConnection = new TcpClient();
                BadgerConnection.Connect(IPAddress.Parse("10.0.0.100"), 1111);

                this.Text += " - Connected";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error while connecting", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (BadgerConnection.Connected)
            {
                //
                // This packet commands the device to return its version number
                byte[] Buffer = new byte[]
                {
                    161,
                    15,
                    89,
                    1,
                    89,
                    3,
                    102
                };

                NetworkStream ns = BadgerConnection.GetStream();
                ns.Write(Buffer, 0, Buffer.Length);
                ns.Flush();

                string Reply = GetReply(BadgerConnection.GetStream());

                MessageBox.Show(Reply);
            }
        }

        public string GetReply(NetworkStream ns)
        {
            StringBuilder result = new StringBuilder();

            try
            {
                byte[] Buffer = new byte[1024];

                while (true)
                {
                    if (ns.CanRead)
                    {
                        int numberOfBytesRead = 0;

                        // Incoming message may be larger than the buffer size.
                        while (ns.DataAvailable)
                        {
                            numberOfBytesRead = ns.Read(Buffer, 0, Buffer.Length);

                            result.AppendFormat("{0}", Encoding.ASCII.GetString(Buffer, 0, numberOfBytesRead));
                        }

                        if (result.Length > 0)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return result.ToString();
        }
    }
}


For simplicity's sake the part where a reply is read is not in a different thread although when I do it like that the result is the same.

Can someone please shed some light on what might be the cause of this problem? Cause I'm running out of ideas and things to try.

Thanks

<Update>
I've been playing around with this more and got two new pieces of information.
As pradnya_k suggested I downloaded process explorer and realized that the connection dies after I call NetworkStream.Write() sometimes it dies even before I get my packet from the device I'm talking too. Then the exception only gets thrown when I try to call NetworkStream.Write() again whice is exactly what was said in some TCP articles I've read.

The second thing I was able to figure out was that the WSE error code that gets thrown is 10054: WSAECONNRESET

WSAECONNRESET
10054
	Connection reset by peer.

    An existing connection was forcibly closed by the remote host. This normally results if the peer 
application on the remote host is suddenly stopped, the host is rebooted, the host or remote network 
interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the 
SO_LINGER option on the remote socket). This error may also result if a connection was broken due to 
keep-alive activity detecting a failure while one or more operations are in progress. Operations that 
were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.

</Update>

Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL

you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

modified on Monday, September 21, 2009 8:32 AM

AnswerRe: Another TCP Issue Pin
pradnya_k21-Sep-09 1:19
pradnya_k21-Sep-09 1:19 
GeneralRe: Another TCP Issue Pin
pradnya_k21-Sep-09 21:15
pradnya_k21-Sep-09 21:15 
QuestionPassword field in datagridview,C# Pin
sampath_dr20-Sep-09 20:04
sampath_dr20-Sep-09 20:04 
AnswerRe: Password field in datagridview,C# Pin
Ravi Mori20-Sep-09 20:59
Ravi Mori20-Sep-09 20:59 
GeneralRe: Password field in datagridview,C# Pin
sampath_dr20-Sep-09 21:21
sampath_dr20-Sep-09 21:21 
QuestionTiming Out the User Session Using the MouseMove Event Pin
Richard Andrew x6420-Sep-09 19:23
professionalRichard Andrew x6420-Sep-09 19:23 
AnswerRe: Timing Out the User Session Using the MouseMove Event Pin
Christian Graus20-Sep-09 19:39
protectorChristian Graus20-Sep-09 19:39 
Questionreducing image file size Pin
michael@cohen20-Sep-09 19:09
michael@cohen20-Sep-09 19:09 
AnswerRe: reducing image file size Pin
Christian Graus20-Sep-09 19:41
protectorChristian Graus20-Sep-09 19:41 
AnswerRe: reducing image file size Pin
Luc Pattyn21-Sep-09 1:01
sitebuilderLuc Pattyn21-Sep-09 1:01 
Questionhow to count printed total pages [modified] Pin
scoket20-Sep-09 17:48
scoket20-Sep-09 17:48 
QuestionThread scheduling [modified] Pin
Kwonhyung Roh20-Sep-09 16:58
Kwonhyung Roh20-Sep-09 16:58 
AnswerRe: Thread scheduling Pin
Dave Kreskowiak20-Sep-09 17:02
mveDave Kreskowiak20-Sep-09 17:02 
GeneralRe: Thread scheduling Pin
Kwonhyung Roh20-Sep-09 18:15
Kwonhyung Roh20-Sep-09 18:15 
GeneralRe: Thread scheduling Pin
Dave Kreskowiak21-Sep-09 2:11
mveDave Kreskowiak21-Sep-09 2:11 
GeneralRe: Thread scheduling Pin
Kwonhyung Roh21-Sep-09 17:31
Kwonhyung Roh21-Sep-09 17:31 
GeneralRe: Thread scheduling Pin
Dave Kreskowiak22-Sep-09 4:30
mveDave Kreskowiak22-Sep-09 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.