连接已被主机中的软件中止

时间:2015-05-08 08:18:18

标签: c# sockets asyncsocket socketexception

您好我有一个使用C#的中间件程序。该程序向网站发送一些请求并从那里提取数据。现在我收到一个错误,我知道,因为网站的网址已被更改。

URL之前是:ip:port 但现在URL已更改为:ip:port / NugaWeb

如何更改程序以请求转到新网址。可能会向缓冲区添加内容,我不确定是否有人可以提供帮助。

错误:

System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.Socket.Receive(Byte[] buffer)
   at myRfid.ConnSocket.getResponse(Int32 protocol, String sendString) in d:\Birth_DayTestNewServer\myRfid\ConnSocket.cs:line 87}

Connection.cs

class ConnSocket
    {
        public const int OVERHIT_ADD_USER = 100;
        public const int OVERHIT_GET_AVERAGE_OF_AGE= 101;
        public const int OVERHIT_WRITE_TEST_RESULT = 102;
        public const int OVERHIT_USER_LIST = 103;
        public const int OVERHIT_USER_SEX= 105;

        public const string HEADER="FF00";
        public const string TAILER="/$/$";
        private Socket clientSocket = null;
        //public static string ip = "121.158.41.217";

        public static string ip = "165.132.221.47";

        private byte[] receiveData;

        private void connection()
        {

            try
            {
                receiveData=new byte[4096];

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





            //clientSocket.Connect(ip, 8090);
            clientSocket.Connect(ip, 8080);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        public string getResponse(int protocol, string sendString)
        {
            string sendMsg = "";
            string recMsg = "";
            byte[] send = null;
            byte[] rec = null;
            connection();
            try
            {
                Encoding korean = Encoding.GetEncoding("euc-kr");
                Encoding enc = Encoding.ASCII;
                rec=new byte[1024];
                //serverStream = clientSocket.GetStream();


                sendMsg = HEADER + "&" + protocol + "&" + sendString + TAILER;             //Step3 :sendMsg=""   
                send = new byte[korean.GetBytes(sendMsg).Length];       //Step4 : sendMsg = "FF00&105&eecb0b7e000104e0/$/$"     sendMsg becomes "FF00&105&/$/$" and then becomes "FF00&101&eecb0b7e000104e0&28/$/$"  and then "FF00&101&&28/$/$"



                send=korean.GetBytes(sendMsg);

                //i tried adding a check on sendMsg here
                if (sendMsg == "FF00&105&/$/$")
                {
                    MessageBox.Show("Card not placed !! Place the card and please try again  ");
                    return null;

                    //System.Environment.Exit(0);
                }

                clientSocket.Send(send);

                int recLen = 0;
                recLen=clientSocket.Receive(rec);

                recMsg = korean.GetString(rec, 0, recLen);



                //sw.Write(send);           
                /*
                rec = new byte[1024];
                rec=korean.GetBytes(sr.ReadToEnd());
                */
                /*
                while (!sr.EndOfStream)
                {
                    recMsg=recMsg+sr.ReadLine();
                }
                 * */



            }


           catch (Exception ex)                                          
            {
               MessageBox.Show(ex.ToString());

            }


                /*i tried this 

            catch (SocketException e)
            {
                if (e.SocketErrorCode == WouldBlock)
                {
                    m_socket.Blocking = true;
                    m_socket.Send(buffer, bufferSize, SocketFlags.None);
                    m_socket.Blocking = false;
                }
            }*/


            finally
            {


                if (clientSocket != null)
                {
                    clientSocket.Close(); clientSocket = null;
                }

            }
            return recMsg;
        }
    }

0 个答案:

没有答案