dotnet / WatsonTcp

WatsonTcp is the easiest way to build TCP-based clients and servers in C#.
MIT License
596 stars 117 forks source link

Help/Problem timeout sendandwait #61

Closed philipphagen87 closed 4 years ago

philipphagen87 commented 4 years ago

Hello,

I really need help with the Server/Client.. My Configuration of the Server is

public static class AsyncServer
    {
        private static string serverIp = "10.0.0.46";
        public static int ServerPort = 9000;
        private static bool useSsl = false;
        private static WatsonTcpServer server = null;
        private static string certFile = "";
        private static string certPass = "";
        private static bool debugMessages = false;
        private static bool acceptInvalidCerts = true;
        private static bool mutualAuthentication = true;

        public static void ConfigServer()
        {
            server = new WatsonTcpServer(serverIp, ServerPort);
            server.ClientConnected += ClientConnected;
            server.ClientDisconnected += ClientDisconnected;
            server.MessageReceived += MessageReceived;
            server.SyncRequestReceived = SyncRequestReceived;
            // server.PresharedKey = "0000000000000000";
            // server.IdleClientTimeoutSeconds = 10;
            logger.Info("Server configuration done");
        }

        public static void StartServer()
        {
            Task serverStart = server.StartAsync();
        }

My client:

public static class AsyncClient
    {
        public static  string ServerIp { get; set; }
        public static int ServerPort { get; set; }
        private static bool useSsl = false;
        private static string certFile = "";
        private static string certPass = "";
        private static bool debugMessages = false;
        private static bool mutualAuthentication = true;
        private static WatsonTcpClient client = null;
        private static string presharedKey = null;

        public static void ConfigClient()
        {
            client = new WatsonTcpClient(ServerIp, ServerPort);
            client.ServerConnected += ServerConnected;
            client.ServerDisconnected += ServerDisconnected;
            client.MessageReceived += MessageReceived;
            client.SyncRequestReceived = SyncRequestReceived;
        }

        public static void StartClient()
        {
            try
            {
                Task startClient = client.StartAsync();
            }
            catch (Exception ex)
            {
                logger.Error("Could not connect to server: {0} Port: {1}", ServerIp, ServerPort);
            }
        }

I want to SendAndWait:

public static void SendAndWait()
        {
            int timeoutMs = 20000;
            string userInput = "testingstring";
            try
            {
                SyncResponse resp = client.SendAndWait(timeoutMs, userInput);
                if (resp.Metadata != null && resp.Metadata.Count > 0)
                {
                    logger.Info("Metadata:");
                    foreach (KeyValuePair<object, object> curr in resp.Metadata)
                    {
                    }
                }
            }
            catch (Exception e)
            {
            }
        }

At server side

private static SyncResponse SyncRequestReceived(SyncRequest req)
        {
            if (req.Metadata != null && req.Metadata.Count > 0)
            {
                logger.Info("Metadata:");
                foreach (KeyValuePair<object, object> curr in req.Metadata)
                {
                    logger.Info("  " + curr.Key.ToString() + ": " + curr.Value.ToString());
                }
            }
            Dictionary<object, object> retMetadata = new Dictionary<object, object>();
            retMetadata.Add("foo", "bar");
            retMetadata.Add("bar", "baz");

            return new SyncResponse(req,retMetadata, "Here is your response!");
        }

It is the same sample as stored in git. If i download the current version from git and test ist, everything is fine. But if i do the same thing with the code above I always get the timeout exception (no msg received after timeout) If i debug then the server recognize the incomming msg and work and "send" back.

Can anybody help me?

nice greetings phil

developervariety commented 4 years ago

What is your version of WatsonTcp? This bug was fixed in 3.1.3, although exists when the server sends a sync request to the client.

philipphagen87 commented 4 years ago

Sorry i forgot to write that i also tried with V3.1.3

developervariety commented 4 years ago

No problem, so you are sending a sync request from the server to the client correct?

philipphagen87 commented 4 years ago

From the client to the Server

developervariety commented 4 years ago

Client -> Server is working completely fine on my end. That's odd! It's only Server -> Client is the problem for me.

jchristn commented 4 years ago

Hi @philipphagen87 are the client and server running on the same machine or different machines?

philipphagen87 commented 4 years ago

they are running on the same machine

jchristn commented 4 years ago

Can you send any messages back and forth using other Send APIs?

Could you package your solution up and share it with me and I'll take a look?

Cheers, Joel

jchristn commented 4 years ago

Hello @philipphagen87 , were you able to get this working? Or able to package up a portion of your solution for me to examine?

philipphagen87 commented 4 years ago

Sorry for the late answer... i fixed my problem.. it was my architecture! thanks for support..

jchristn commented 4 years ago

Awesome thanks @philipphagen87 please let me know if I can help in the future.