Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public static List<ClientManager> Vehicles;
       //public static List<ClientUpdateData> ClientFirmware;
       private static BackgroundWorker bwListener;
       private static  TcpListener listenerSocket;
       /// <summary>
       /// Port used for connection
       /// </summary>
       public static Int32 Port;
       /// <summary>
       /// Flag used for connection
       /// </summary>
       public static  bool Status;
       /// <summary>
       /// event handler ClientConnected
       /// </summary>
       public static event ConnectedEventHandler ClientConnected = delegate { };
       /// <summary>
       /// event handler ClientDisconnected
       /// </summary>
       public static event DisconnectedEventHandler ClientDisConnected = delegate { };
       /// <summary>
       /// event handler DataReceived
       /// </summary>
       public static event DataReceivedEventHandler DataReceived = delegate { };
       /// <summary>
       /// event hanlder for notification
       /// </summary>
       public static event NotificationEventHandler OnNotificationReceived = delegate { };
       /// <summary>
       /// Starts a background thread that listen for TCP connection
       /// </summary>
       /// <param name="port"></param>
       /// <param name="status"></param>
       public static void Start( String port, String status)
       {
           Port = Convert.ToInt32(port);
           // Port = 14126;
           Status = Convert.ToBoolean(status);
           if (Status)
           {
               Vehicles = new List<ClientManager>();
               bwListener = new BackgroundWorker();
               bwListener.WorkerSupportsCancellation = true;
               //background worker that listens to the devices
               bwListener.DoWork += new DoWorkEventHandler(StartToListen);
               bwListener.RunWorkerAsync();
           }
       }


       /// <summary>
       /// No of vehicles connected
       /// </summary>
       public static Int32 VehicleCount
       {
           get { return Vehicles.Count; }
       }

       /// <summary>
       /// Starts a background thread that listens for a TCP connection
       /// </summary>
       public static void StartServer()
       {
           if (!Status)
           {
               bwListener = new BackgroundWorker();
               bwListener.WorkerSupportsCancellation = true;
               //background worker that starts to listen for devices
               bwListener.DoWork += new DoWorkEventHandler(StartToListen);
               bwListener.RunWorkerAsync();
               Status = true;
           }

       }
       /// <summary>
       /// Function that listens for the TCP connection and when connected creates a new client
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private static void StartToListen(object sender, DoWorkEventArgs e)
       {
           //starts listening to the specified port
           listenerSocket = new TcpListener(IPAddress.Any, Port);
           listenerSocket.Start();

           while (Status)
           {
               if (!listenerSocket.Pending())
               {
                   Thread.Sleep(500);
                   continue;
               }
               //creates a ClientManager object in case there is a device trying to connect to this port
              // CreateNewClientManager(listenerSocket.AcceptTcpClient());
               //  CreateNewClientManager(listenerSocket);
               listenerSocket.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
           }
       }
       /// <summary>
       /// Creates a new client and assigns the event handlers
       /// </summary>
       /// <param name="socket"></param>
       private static void AcceptTcpClientCallback(IAsyncResult asyncResult)
       {
           TcpClient client = listenerSocket.EndAcceptTcpClient(asyncResult);
               listenerSocket.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
               CreateNewClientManager(client);


       }




       private static void CreateNewClientManager(TcpClient socket)
       {
           //creates a object of ClientManager class and assign event handlers
           ClientManager newClientManager = new ClientManager(socket);
           newClientManager.CommandReceived += new PacketReceivedEventHandler(newPacketReceived);
           newClientManager.Disconnected += new DisconnectedEventHandler(ClientDisconnected);
           newClientManager.Connected += new ConnectedEventHandler(VehicleConnected);
           newClientManager.Notification +=new NotificationEventHandler(NotificationReceived);
           ClientConnected(null, new ClientEventArgs(newClientManager.Address, newClientManager.Address));
       }
Posted
Comments
phil.o 10-Jun-14 8:03am    
I'm sorry? What do you expect from us?
You have to state some kind of issue, describe a concrete problem.
What is wrong with this code? What do you get that you did not expect to? What don't you get that you did expect to? Are there some error messages? Some exceptions thrown?
Please help us to help you :)
Muhammad Amman 10-Jun-14 8:17am    
Can you just tell me i want to connect my client in asynchronous way please verify my code and tell me where i can edit for asynchrounsly communication
ZurdoDev 10-Jun-14 9:24am    
It's not likely anyone is going to run your code for you to make sure it works. You do that and then let us know if you are stuck on something.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900