i have problem in C# CODE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrgmrk
    New Member
    • May 2010
    • 2

    #1

    i have problem in C# CODE

    i have code in C# between CLIENT and SERVER to collocates the packet lose and time delay the programming is working good in LAN 127.0.0.1 but in WAN is stopping at first PACKET LOSE it doesn’t complete to END loop,,,,,, How can i use THREAD in this code ..???

    THANK YOU .....

    SERVER CODE
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
     namespace UPDServer
    {    class Program
        {        static void Main(string[] args)
            {   int recv;
       byte[] data = new byte[1024];
       IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 7778);
       Socket newsock = new Socket(AddressFamily.InterNetwork,
               SocketType.Dgram, ProtocolType.Udp);
       newsock.Bind(ipep);
       Console.WriteLine("Waiting for a client...");
       IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
       EndPoint tmpRemote = (EndPoint)(sender);
       recv = newsock.ReceiveFrom(data, ref tmpRemote);
       Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
       Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
       string welcome = "Welcome to my test server";
       data = Encoding.ASCII.GetBytes(welcome);
       newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
       for (int i = 0; i < 100; i++)
       {       data = new byte[1024];
           recv = newsock.ReceiveFrom(data, ref tmpRemote);
           newsock.SendTo(data,data.Length, SocketFlags.None, tmpRemote);
           Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
       }      
       newsock.Close();
       Console.ReadLine();
            }
        }
    }
    CLIENT CODE
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    namespace ConsoleApplication1
    {    class Program
        {        static void Main(string[] args)
            {            byte[] data = new byte[1024];
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7778);
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                string welcome = "Hello, are you there?";
                data = Encoding.ASCII.GetBytes(welcome);
                server.SendTo(data, data.Length, SocketFlags.None, ipep);
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint tmpRemote = (EndPoint)sender;
                data = new byte[1024];
                int recv = server.ReceiveFrom(data, ref tmpRemote);
                Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
                string[] sndt = new string [100];
                string[] rcvt = new string [100];
                Console.WriteLine( "send time ^^" + "receive time ( m:s:ms:tt)");
                data = new byte[1024];
                for( int i=0 ; i<100 ; i++ )
                {                // save time to sndt[] and send the packet   .ToString
                sndt[i] = DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond + ":" + DateTime.Now.Ticks.ToString();
                server.SendTo( Encoding.ASCII.GetBytes ("packet NUM:" + i) , ipep);
                       //recieve the packet and save the time in rcvt[]...........
                recv = server.ReceiveFrom(data, ref tmpRemote );
                rcvt[i] = DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond + ":" + DateTime.Now.Ticks.ToString();
                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv) + "\r=========" + i + "=========\n");
                Console.WriteLine(sndt[i]);
                Console.WriteLine(rcvt[i]);
                }
                //}
                
                Console.WriteLine("Stopping client");
                server.Close();
                Console.ReadLine();
            }
        }
    }
    Last edited by tlhintoq; May 1 '10, 06:59 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you first created your question you were asked to wrap your code with [code] tags.
    [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
    It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • mrgmrk
      New Member
      • May 2010
      • 2

      #3
      i have code in C# between CLIENT and SERVER to collocates the packet lose and time delay the programming is working good in LAN 127.0.0.1 but in WAN is stopping at first PACKET LOSE it doesn’t complete to END loop,,,,,, How can i use THREAD in this code ..???

      THANK YOU .....

      Code:
        SERVER  CODE
       using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Net;
      using System.Net.Sockets;
       namespace UPDServer
      {    class Program
          {        static void Main(string[] args)
              {   int recv;
         byte[] data = new byte[1024];
         IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 7778);
         Socket newsock = new Socket(AddressFamily.InterNetwork,
                 SocketType.Dgram, ProtocolType.Udp);
         newsock.Bind(ipep);
         Console.WriteLine("Waiting for a client...");
         IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
         EndPoint tmpRemote = (EndPoint)(sender);
         recv = newsock.ReceiveFrom(data, ref tmpRemote);
         Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
         Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
         string welcome = "Welcome to my test server";
         data = Encoding.ASCII.GetBytes(welcome);
         newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
         for (int i = 0; i < 100; i++)
         {       data = new byte[1024];
             recv = newsock.ReceiveFrom(data, ref tmpRemote);
             newsock.SendTo(data,data.Length, SocketFlags.None, tmpRemote);
             Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
         }      
         newsock.Close();
         Console.ReadLine();
              }
          }
      }
      
      CLIENT  CODE
      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Net;
      using System.Net.Sockets;
      
      namespace ConsoleApplication1
      {    class Program
          {        static void Main(string[] args)
              {            byte[] data = new byte[1024];
                  IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7778);
                  Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                  string welcome = "Hello, are you there?";
                  data = Encoding.ASCII.GetBytes(welcome);
                  server.SendTo(data, data.Length, SocketFlags.None, ipep);
                  IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                  EndPoint tmpRemote = (EndPoint)sender;
                  data = new byte[1024];
                  int recv = server.ReceiveFrom(data, ref tmpRemote);
                  Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                  Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
                  string[] sndt = new string [100];
                  string[] rcvt = new string [100];
                  Console.WriteLine( "send time ^^" + "receive time ( m:s:ms:tt)");
                  data = new byte[1024];
                  for( int i=0 ; i<100 ; i++ )
                  {                // save time to sndt[] and send the packet   .ToString
                  sndt[i] = DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond + ":" + DateTime.Now.Ticks.ToString();
                  server.SendTo( Encoding.ASCII.GetBytes ("packet NUM:" + i) , ipep);
                         //recieve the packet and save the time in rcvt[]...........
                  recv = server.ReceiveFrom(data, ref tmpRemote );
                  rcvt[i] = DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond + ":" + DateTime.Now.Ticks.ToString();
                  Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv) + "\r=========" + i + "=========\n");
                  Console.WriteLine(sndt[i]);
                  Console.WriteLine(rcvt[i]);
                  }
                    
      
                  
                  //}
                  
                  
                  
                  
                  
                  Console.WriteLine("Stopping client");
                  server.Close();
                  Console.ReadLine();
      
      
              }
          }
      }

      Comment

      Working...