locked
Getting Error: An existing connection was forcibly closed by the remote host c# RRS feed

  • Question

  • User-1417180453 posted

    I am trying to read data sent by the GPS device to m server. I have a VPS server with static IP address. I am getting error (An existing connection was forcibly closed by the remote host c#) to read data.

    My GPS device sending data to static IP provided by the hosting and port 80.

    With the below code, I am doing both sending and reading the data.

    For the time being, I want to read data on click of "button" on the web page.

    Here is the code:

    protected void button1_Click(object sender, EventArgs e)
    {        
        string message = "Thisistestmessage";
        Int32 port = Convert.ToInt32(textbox.Text);
        //string server = "127.0.0.1";
        string server = "45.58.143.201";
        string content = "";
        byte[] dataa = System.Text.Encoding.ASCII.GetBytes(message);
        IPAddress ipaddress = IPAddress.Parse(server);
        IPEndPoint ip = new IPEndPoint(ipaddress, port);
        Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        s.Connect(ip);
        Response.Write("Socket Connected");
        s.Send(dataa);
        Response.Write("Message Sent");
        byte[] data = new byte[2048];
        int i = s.Receive(data);
        Response.Write("Data received");
        Response.Write(i.ToString());
        //Response.ContentEncoding(i.ToString());
        //Response.Write(Encoding.ASCII.GetString(data));
        s.Close();
    }
    }
    Thursday, January 31, 2019 3:27 AM

All replies

  • User753101303 posted

    Hi,

    Looks to be a firewall issue (or the GPS doesn't listen ?).  Also you likely need to send a message the device could understand. On my side, I would rather expect this kind of device to push data to the server (rather than the server coming to read data out of the device).

    IMO you should first try the vendor support site for the device you are using to see how it works exactly.

    For example according to https://en.wikipedia.org/wiki/GPS_tracking_unit#Types you have "Data pushers" and "Data pullers" (the later being less used than the former)

    Thursday, January 31, 2019 12:11 PM
  • User36583972 posted

    Hi kaushalarwal

    I am trying to read data sent by the GPS device to m server. I have a VPS server with static IP address. I am getting error (An existing connection was forcibly closed by the remote host c#) to read data.

    1: First make sure that there is no problem with server remote access.

    2: Try the following code to receive and sent data.

            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    byte[] bytes = new byte[1024];
                    string message = "Thisistestmessage";
                    Int32 port = 1234;
                    string server = "your address";
                    byte[] dataa = System.Text.Encoding.ASCII.GetBytes(message);
                    IPAddress ipaddress = IPAddress.Parse(server);
                    IPEndPoint ip = new IPEndPoint(ipaddress, port);
                    Socket sendersket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 
                    try
                    {
                        sendersket.Connect(ip);                   
                        byte[] msg = Encoding.ASCII.GetBytes(TextBox3.Text + "<EOF>");
                        int bytesSent = sendersket.Send(msg);
                        Response.Write("Message Sent" + TextBox3.Text+ "\r\n");
                        int bytesRec = sendersket.Receive(bytes);
                        Response.Write("Server Sent" + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                        // Release the socket.  
                        sendersket.Shutdown(SocketShutdown.Both);
                        sendersket.Close();
                    }
                    catch (ArgumentNullException ane)
                    {
                      
                    }
                    catch (SocketException se)
                    {
                        
                    }
                    catch (Exception es)
                    {
                        
                    }
                }
                catch (Exception es)
                {
                   
                }
            }


    Besides, If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.


    Best Regards,

    Yong Lu

    Friday, February 1, 2019 6:10 AM