tags:

views:

107

answers:

1

Hi to all,

I want to connect a device [a kind of modem] over TCP-Protocol.

The device could be considered as a client. Over a program I want to connect to this device. The program is located on a server and has a special port open and a static ip.

How can I do it?

Here is my code :

    // where ip= 10.0.0.50 and port= 5050, established for the server!
    TcpListerner listener;
    if listener!= null)
        {
            listener.Stop();
        }
        listener= new TcpListener(ip, port);
        listener.Start();


         Socket socket;
         if (!listener.Pending())
         {
             IAsyncResult ar = listener.BeginAcceptSocket(new AsyncCallback(DoAcceptSocketCallback), socket);
             Thread.Sleep(15000);  // try to connect in 15 seconds

             if (socket != null && socket.Connected)
             {
          // if socket is connected
          // put the code here or do nothing
             }
             else
             {
                    if (socket != null)
                    {
                        socket.Close();
                    }
                    if listener!= null)
                    {
                        listener.Stop();
                        listener = null;
                    }

              }
         }

    public void DoAcceptSocketCallback(IAsyncResult ar)
    {
        try
        {
            TcpListener listener = (TcpListener)ar.AsyncState;

            if (listener != null)
            {
                // connected to socket :
                socket = listener.EndAcceptSocket(ar);
            }
        }
        catch
        {
            socket = null;
        }
    }

although I can not connect the device and I can not solve the hang problem by socket connection. I can not do anything on my form while this code is running cause of 15seconds to try connecting.

+1  A: 

I think you should make some changes to your code.

When a client connects, create a TcpClient object by using this statement

TcpClient client = listener.AcceptTcpClient()

You can then create a Thread to process the client so that the main thread doesn't get stuck up.

if (!listener.Pending())
{
    TcpClient client = listener.AcceptTcpClient();
    Thread newThread = new Thread(this.ServeClient);
    newThread.Start(client);
}

Then the method for handling the threads

public static void ServerClient(TcpClient client)
{
    // Do some work
}


It would be better if you use the Thread Pool instead of creating one new thread at every connection. Using this method, you can control the maximum number of connections which your server can handle. I don't know much about this functionality as I havn't used it in large programs.

I would suggest you to check the Code Project example on Thread Pool

Manish Sinha
thanks Manish, it works.
Cmptrb
I have updated the answer with info about Thread Pool. Check it out and please mark is as answer if you find the new added info useful.
Manish Sinha