I want to listen port data completely

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LKRAJ
    New Member
    • Mar 2013
    • 2

    I want to listen port data completely

    I generate a application which listen the data from tcp/port. My application is working properly but it missed some data from Tcp/port. Please tell me how to listen data completely...
    my code is as following...... .

    Code:
    class Program
        {
            private Byte[] data = new Byte[2048];
            private int size = 2048;
            private Socket server;
            
            static ManualResetEvent allDone = new ManualResetEvent(false);
          
            static void Main(string[] args)
            {
                Program ob = new Program();
                ob.Run();
                //Timer tm = new Timer(new TimerCallback(ob.CloseApp()),null,12000,12000);
            }
    
            public void Run()
            {
                try
                {
    
                    server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPEndPoint iep = new IPEndPoint(IPAddress.Any, 32000);
                    server.Bind(iep);
                    
                    Console.WriteLine("Server initialized..");
                    server.Listen(200);
                    
                    Console.WriteLine("Listening...");
                    while (true)
                    {
                        allDone.Reset();
                        server.BeginAccept(new AsyncCallback(AcceptCon), server);
                        allDone.WaitOne();
                    }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
    
            void AcceptCon(IAsyncResult iar)
            {
                allDone.Set();
                try
                {
                    Socket oldserver = (Socket)iar.AsyncState;
                    Socket client = oldserver.EndAccept(iar);
                    Console.WriteLine(client.RemoteEndPoint.ToString() + " connected");
                    byte[] message = Encoding.ASCII.GetBytes("Welcome");
                    client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client);
    
                    //client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
                }
                catch (Exception)
                {
                    Console.WriteLine("Connection closed.......\n Error in accept connection");
                    return;
                }
            }
    
            void SendData(IAsyncResult iar)
            {
                Socket client = (Socket)iar.AsyncState;
                try
                {
    
                    int sent = client.EndSend(iar);
                    client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
                    //client.Receive(data, 0, size, SocketFlags.None);
                }
                catch (Exception)
                {
                    Console.WriteLine("Connection closed..\n Error in Sending data");
                    client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
                    return;
                }
            }
    
            void ReceiveData(IAsyncResult iar)
            {
                Socket client = (Socket)iar.AsyncState;
                byte[] message2 = Encoding.ASCII.GetBytes("reply");
                try
                {
    
                    int recv = client.EndReceive(iar);
                    if (recv == 0)
                    {
                        client.Close();
                        server.BeginAccept(new AsyncCallback(AcceptCon), server);
                        return;
                    }
                    //string receivedData = Data_Asc_Hex(Encoding.ASCII.GetBytes(data.ToString()).ToString().ToUpper());
                    //string receivedD = Encoding.ASCII.GetString(data, 0, recv);
                    Encoding enc = Encoding.GetEncoding("iso-8859-1");
                    string receivedD = enc.GetString(data, 0, recv);
                    string receivedData = Data_Asc_Hex(receivedD);
                    Console.WriteLine("***(TCP) Connected" + Environment.NewLine + " " + receivedData.ToUpper());
                 
                    client.BeginSend(message2, 0, message2.Length, SocketFlags.None, new AsyncCallback(SendData), client);
                }
                catch (Exception)
                {
                    Console.WriteLine("Connection closed..........\n Error in Receiving data");
                    //client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
                    client.BeginSend(message2, 0, message2.Length, SocketFlags.None, new AsyncCallback(SendData), client);
                    return;
                }
            }
            public string Data_Asc_Hex(string Data)
            {
                //first take each charcter using substring.
                //then convert character into ascii.
                //then convert ascii value into Hex Format
    
                string sHex = "";
                for (int a = 0; a < Data.Length; a = a + 1)
                {
                    string Char2Convert = Data.Substring(a, 1);
                    char c = Char2Convert.ToCharArray(0, 1)[0];
                    string n = Convert.ToString(c, 16);
                    //string n = System.Convert.ToChar(System.Convert.ToUInt32(Data.Substring(0, 2), 16)).ToString();
    
                    sHex = sHex + n + " ";
    
                }
                return sHex;
            }
           
        }
    Last edited by Rabbit; Mar 31 '13, 09:26 PM. Reason: Please use code tags when posting code.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You should wrap your code in code tags so that it becomes more readable.

    If you are missing some data while reading either

    1.) Your data reading loop doesn't run until all data is read (perhaps some off by one error) or
    2.) Data is being lost during transmission in which case there is little you can do about it in your code. You could just expect the errors and force a retransmit when they happen.

    Comment

    • LKRAJ
      New Member
      • Mar 2013
      • 2

      #3
      Thanks for comment.... I think there is no data lose in traffic. Plz tell me how could I remove all errors in my code.....plz

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Compare the MSDN example with your code :http://msdn.microsoft.com/en-us/library/bew39x2a.aspx

        Comment

        Working...