Question Socket connectivity issue in Release Mode

soumya.essl

Member
Joined
Jun 11, 2018
Messages
7
Programming Experience
5-10
[FONT=&quot]Hi,[/FONT]
[FONT=&quot]I am working on socket connection , send and receive data c# windows form application[/FONT]
[FONT=&quot]Socket connection is written on third party dll. [/FONT]
[FONT=&quot]Connecting one UHF Reader to identify surrounding UHF cards.[/FONT]
[FONT=&quot]My problem is am able to connect and get data using DLL functions using thread. But socket will be unable listen and get data in RELEASE Mode.[/FONT]
[FONT=&quot]But in DEBUG mode it works fine.[/FONT]
[FONT=&quot]Please help me in this.[/FONT]
[FONT=&quot]Regards,[/FONT]
[FONT=&quot]Soumya,[/FONT]
[FONT=&quot]Bangalore

[/FONT]
 
You're going to have to provide more detail. The question is just to vague as it is. Please provide the relevant code (and ONLY the relevant code) and explain EXACTLY what happens when you run it.
 
Hi,

I have two windows forms 1 Form1 and Form2

Form1.cs

C#:
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.IO;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using Rfid245ApiLib;


namespace _2._4G
{
    public partial class Form1 : Form
    {
        hComSocket CS;                  //???????(connect handle struct)


        int hCom = -1;                    //????(comm handle)


        Socket sockClient = null;            //????(TCP/IP handle)


        int ConnectFlag = 0;            //?????(connect falg)
        int Language = 0;
        int ResetFlag = 0;


        RFID_StandardProtocol rfid_sp = new RFID_StandardProtocol();




        int DisplayCnt = 0;
        int Displayindex = 0;


        /// <summary>
        /// memcmp API
        /// </summary>
        /// <param name="b1">????1</param>
        /// <param name="b2">????2</param>
        /// <returns>???????????0?????1????2?????0???????1????2?????0???</returns>
        [DllImport("msvcrt.dll")]
        unsafe private static extern int memcmp(void* b1, void* b2, int count);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void* memcpy(void* dest, void* src, int count);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void* malloc(int size);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void* calloc(int num, int size);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void free(void* memblock);






        public Form1()
        {
            InitializeComponent();
        }


        private void btn_TestConnection_Click(object sender, EventArgs e)
        {
            int status = 0;


            status = ConnectReader();
            if (0 == status)
            {
                CS.hCom = -1;
                CS.sockClient = sockClient;


                //CONNECT_button.Enabled = false;
                //DISCONNECT_button.Enabled = true;


                ConnectFlag = 1;        //?????(connect flag)
                MessageBox.Show("success");
                //GetVersion(255);
                //timer1.Enabled = true;
                //GetSerialNum(255);


                //timer1.Enabled = true;
                //Thread GSNT = new Thread(new ThreadStart(GetSerialNumThread));
                //Thread GVT = new Thread(new ThreadStart(GetVersionThread));
                //GSNT.Start();
                //GVT.Start();
            }


            DisconnectHandler();
        }




        private int ConnectReader()
        {
            int status = 0;
            string TempStr = "";
            string TempStrEnglish = "";


            status = SocketConnect();


            switch (status)
            {
                case 0:
                    TempStr = "????!";
                    TempStrEnglish = "Connect success!";
                    //CONNECT_button.Enabled = false;
                    //DISCONNECT_button.Enabled = true;
                    break;
                case -1:
                    TempStr = "????!";
                    TempStrEnglish = "Connect fail!";
                    //CONNECT_button.Enabled = true;
                    //DISCONNECT_button.Enabled = false;
                    break;
                case -2:
                    TempStr = "???????!";
                    TempStrEnglish = "Connect parameter incomplete!";
                    //CONNECT_button.Enabled = true;
                    //DISCONNECT_button.Enabled = false;
                    break;
                case -3:
                    TempStr = "??IP????!";
                    TempStrEnglish = "Input IP Invalid!";
                    //CONNECT_button.Enabled = true;
                    //DISCONNECT_button.Enabled = false;
                    break;
            }
            //if (0 == Language)
            //    ListBoxAdd(TempStr);
            //else
            //    ListBoxAdd(TempStrEnglish);
            //if (TempStr != "????!" || TempStrEnglish != "Connect success!")
            //    return -1;
            return 0;
        }


        private int SocketConnect()
        {
            IPAddress ipaddr;
            if (txt_IpAddress.Text == "" || txt_Port.Text == "")
            {
                return -2;            //???????(input info incomplete)
            }
            else if (!IPAddress.TryParse(txt_IpAddress.Text, out ipaddr))
            {
                return -3;
            }
            else
            {


                if (rfid_sp.Socket_ConnectSocket(ref sockClient,
                                                 txt_IpAddress.Text,
                                                 Convert.ToInt32(txt_Port.Text))
                     == RFID_StandardProtocol.SUCCESS)
                    return 0;        //????(connect success)
                else
                    return -1;        //????(connect fail)
            }
        }


        private void DisconnectHandler()
        {
            int status = 0;
            status = DisConnectReader();


            if (0 == status)
            {
                //CONNECT_button.Enabled = true;
                //DISCONNECT_button.Enabled = false;
                ConnectFlag = 0;        //?????(connect falg)
            }
        }


        private int DisConnectReader()
        {
            int status = 0;
            string TempStr = "";
            string TempStrEnglish = "";


            status = SocketDisConnect();


            switch (status)
            {
                case 0:
                    TempStr = "????!";
                    TempStrEnglish = "Close success!";
                    break;
                case -1:
                    TempStr = "????!";
                    TempStrEnglish = "Close fail!";
                    break;
                case -2:
                    TempStr = "??????!";
                    TempStrEnglish = "Handle unavailable!";
                    break;
            }
            //if (0 == Language)
            //    ListBoxAdd(TempStr);
            //else
            //    ListBoxAdd(TempStrEnglish);
            //if (TempStr != "????!" || TempStrEnglish != "Close success!")
            //    return -1;
            return 0;
        }


        private int SocketDisConnect()
        {
            if (sockClient != null)
            {
                if (rfid_sp.Socket_CloseSocket(sockClient) == RFID_StandardProtocol.SUCCESS)
                {
                    CS.sockClient = null;
                    return 0;        //????(close success)
                }
                else
                    return -1;        //????(close fail)
            }
            else
            {
                return -2;            //????(handle unavailable)
            }
        }


        private void btn_EthernetSettings_Click(object sender, EventArgs e)
        {
            Form2 frmForms2 = new Form2();
            frmForms2.ShowDialog();
        }




    }
}



Form2

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using Rfid245ApiLib;
using System.Windows.Forms;


namespace _2._4G
{
    public partial class Form2 : Form
    {
        hComSocket CS;                  //???????(connect handle struct)


        int hCom = -1;                    //????(comm handle)


        Socket sockClient = null;            //????(TCP/IP handle)


        int ConnectFlag = 0;            //?????(connect falg)
        int Language = 0;
        int ResetFlag = 0;


        RFID_StandardProtocol rfid_sp = new RFID_StandardProtocol();




        int DisplayCnt = 0;
        int Displayindex = 0;


        /// <summary>
        /// memcmp API
        /// </summary>
        /// <param name="b1">????1</param>
        /// <param name="b2">????2</param>
        /// <returns>???????????0?????1????2?????0???????1????2?????0???</returns>
        [DllImport("msvcrt.dll")]
        unsafe private static extern int memcmp(void* b1, void* b2, int count);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void* memcpy(void* dest, void* src, int count);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void* malloc(int size);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void* calloc(int num, int size);


        [DllImport("msvcrt.dll")]
        unsafe private static extern void free(void* memblock);




        public Form2()
        {
            InitializeComponent();
        }


        private void QUERYINTERNET_button_Click(object sender, EventArgs e)
        {
            QueryInternet(255);
        }


        private void ByteToDecimalstr(ref string ToStr, byte[] Buf)
        {
            for (int i = 0; i < 4; i++)
            {
                ToStr = ToStr + IntToInt_Str((int)Buf[i]);
                if (3 != i)
                    ToStr = ToStr + ".";
            }
        }


        private string IntToInt_Str(int value)
        {
            string str = string.Format("{0:D}", value);
            return str;
        }


        private int QueryInternet(int ReaderNum)
        {
            string TempStr = "";
            string TempStrEnglish = "";
            string IPAddr = "";
            string MaskCode = "";
            string GateWay = "";
            int InternetPort = 0;
            byte[] IPAddrbuf = new byte[4];
            byte[] MaskCodebuf = new byte[4];
            byte[] GateWaybuf = new byte[4];
            byte[] InternetPortbuf = new byte[2];
            byte ReaderAddr = (byte)(ReaderNum & 0xFF);
            if (0x00 == rfid_sp.Config_GetInternetAccess(CS, IPAddrbuf, MaskCodebuf, GateWaybuf, InternetPortbuf, ReaderAddr))
            {
                TempStr = "???????????!";
                TempStrEnglish = "Reader ethernet query success!";
                ByteToDecimalstr(ref IPAddr, IPAddrbuf);
                ByteToDecimalstr(ref MaskCode, MaskCodebuf);
                ByteToDecimalstr(ref GateWay, GateWaybuf);
                InternetPort = (InternetPortbuf[1] << 8) + (int)InternetPortbuf[0];
            }
            else
            {
                TempStr = "???????????!";
                TempStrEnglish = "Reader ethernet query fail!";
            }


            ADDRESS_textBox.Text = IPAddr;
            MASK_textBox.Text = MaskCode;
            GATEWAY_textBox.Text = GateWay;
            PORT_textBox.Text = Convert.ToString(InternetPort);


            //if (0 == Language)
            //    ListBoxAdd(TempStr);
            //else
            //    ListBoxAdd(TempStrEnglish);
            return 0;
        }






        private void SETINTERNET_button_Click(object sender, EventArgs e)
        {


        }


        private void Form2_Load(object sender, EventArgs e)
        {
            int status = 0;


            status = ConnectReader();
            if (0 == status)
            {
                CS.hCom = -1;
                CS.sockClient = sockClient;


                //CONNECT_button.Enabled = false;
                //DISCONNECT_button.Enabled = true;


                ConnectFlag = 1;        //?????(connect flag)
                //MessageBox.Show("success");


                QueryInternet(255);
                //GetVersion(255);
                //timer1.Enabled = true;
                //GetSerialNum(255);


                //timer1.Enabled = true;
                //Thread GSNT = new Thread(new ThreadStart(GetSerialNumThread));
                //Thread GVT = new Thread(new ThreadStart(GetVersionThread));
                //GSNT.Start();
                //GVT.Start();
            }




        }




        private int ConnectReader()
        {
            int status = 0;
            string TempStr = "";
            string TempStrEnglish = "";


            status = SocketConnect();


            switch (status)
            {
                case 0:
                    TempStr = "????!";
                    TempStrEnglish = "Connect success!";
                    //CONNECT_button.Enabled = false;
                    //DISCONNECT_button.Enabled = true;
                    break;
                case -1:
                    TempStr = "????!";
                    TempStrEnglish = "Connect fail!";
                    //CONNECT_button.Enabled = true;
                    //DISCONNECT_button.Enabled = false;
                    break;
                case -2:
                    TempStr = "???????!";
                    TempStrEnglish = "Connect parameter incomplete!";
                    //CONNECT_button.Enabled = true;
                    //DISCONNECT_button.Enabled = false;
                    break;
                case -3:
                    TempStr = "??IP????!";
                    TempStrEnglish = "Input IP Invalid!";
                    //CONNECT_button.Enabled = true;
                    //DISCONNECT_button.Enabled = false;
                    break;
            }
            //if (0 == Language)
            //    ListBoxAdd(TempStr);
            //else
            //    ListBoxAdd(TempStrEnglish);
            //if (TempStr != "????!" || TempStrEnglish != "Connect success!")
            //    return -1;
            return 0;
        }


        private int SocketConnect()
        {
            IPAddress ipaddr;
            //if (txt_IpAddress.Text == "" || txt_Port.Text == "")
            //{
            //    return -2;            //???????(input info incomplete)
            //}
            if (!IPAddress.TryParse("192.168.1.178", out ipaddr))
            {
                return -3;
            }
            else
            {


                if (rfid_sp.Socket_ConnectSocket(ref sockClient,
                                                 "192.168.1.178",
                                                 Convert.ToInt32("100"))
                     == RFID_StandardProtocol.SUCCESS)
                    return 0;        //????(connect success)
                else
                    return -1;        //????(connect fail)
            }
        }


        private void DisconnectHandler()
        {
            int status = 0;
            status = DisConnectReader();


            if (0 == status)
            {
                //CONNECT_button.Enabled = true;
                //DISCONNECT_button.Enabled = false;
                ConnectFlag = 0;        //?????(connect falg)
            }
        }


        private int DisConnectReader()
        {
            int status = 0;
            string TempStr = "";
            string TempStrEnglish = "";


            status = SocketDisConnect();


            switch (status)
            {
                case 0:
                    TempStr = "????!";
                    TempStrEnglish = "Close success!";
                    break;
                case -1:
                    TempStr = "????!";
                    TempStrEnglish = "Close fail!";
                    break;
                case -2:
                    TempStr = "??????!";
                    TempStrEnglish = "Handle unavailable!";
                    break;
            }
            //if (0 == Language)
            //    ListBoxAdd(TempStr);
            //else
            //    ListBoxAdd(TempStrEnglish);
            //if (TempStr != "????!" || TempStrEnglish != "Close success!")
            //    return -1;
            return 0;
        }


        private int SocketDisConnect()
        {
            if (sockClient != null)
            {
                if (rfid_sp.Socket_CloseSocket(sockClient) == RFID_StandardProtocol.SUCCESS)
                {
                    CS.sockClient = null;
                    return 0;        //????(close success)
                }
                else
                    return -1;        //????(close fail)
            }
            else
            {
                return -2;            //????(handle unavailable)
            }
        }


        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            DisconnectHandler();
        }


    }
}
 
In form1 there are two text boxes one for ipaddress and other port. Testing socket connection using Third Party DLL and if Connection success, am just closing Connection.


In Form1 one more butten to get Ethenrnet settings of Given ip address and Port.

When i click on button ll open Form2.
 
In Form2 ConnectDevice() On form Load.
If ConnectionSuccess den Will call Function called QueryInternet(255);


My problem is, when Form2 loads first time ConnectDevice() and QueryInternet(255) working fine.
When am closing form den disconnecting socket connection.
If reopen form2,
QueryInternet(255) getting hanged in release mode.If i debug line by line Executes properly.
 

Latest posts

Back
Top Bottom