开发者

Problem in communicating using Socket in C#

I'm hoping I haven't missed something stupid. I am working on an application that communicates between a number of hand-held window's mobile computers and a central server. The current architecture only allows short messages (up to 4096 bytes), I am modifying the application to allow larger communications when necessary. This code seems to work intermittently, depending on the server's speed and the network communication speed. I need to find a way make the server's receiving code more solid. It occasionally works perfectly, other times the hand-held throws the error "Unable to write data to the transport connection. An existing connection was forcible closed by the remote host."

Hand-held transmitting code:

public bool SendMessage(NetworkStream ansStream, string asMessage)  
{  
    try  
    {  
        Byte[] wBytes = System.Text.Encoding.ASCII.GetBytes(asMessage.ToCharArray());  
        ansStream.Write(wBytes, 0, wBytes.Length);  
        return true;  
    }  
    catch (Exception E)  
    {  
        MessageBox.Show(E.Message, "Network Error");  
        return false;  
    }  
}  

Server side receiving code:

public const int BUFFER_LENGTH开发者_如何学编程 = 4096;  
public void ProcessMessage(object aClient)  
{  
    TcpClient wClient = (aClient as TcpClient);  
    try  
    {  
        byte[] wBytes = new byte[BUFFER_LENGTH];  
        StringBuilder wMessage = new StringBuilder();  
        using (NetworkStream wNetStream = wClient.GetStream())  
        {  
            try  
            {  
                int wiRead = 0;  
                int wiTotalBytes = 0;  
                do  
                {  
                    wiRead = wNetStream.Read(wBytes, 0, wBytes.Length);  
                    wMessage.AppendFormat("{0}", Encoding.ASCII.GetString(wBytes, 0, wiRead));  
                    wiTotalBytes += wiRead;  

                    // my attempt to ensure entire message is read  
                    if (wNetStream.DataAvailable)  
                    {  
                        Thread.Sleep(1000);  
                    }  

                } while (wNetStream.DataAvailable);  

                if (wiTotalBytes > 0)  
                {  
                    string wsAnswerFile = "";  
                    string wsResult = DoBusinessProcess(wMessage.ToString(), out wsAnswerFile);  
                    if (string.IsNullOrEmpty(wsAnswerFile))  
                    {  
                        wBytes = Encoding.ASCII.GetBytes(wsResult);  
                        wNetStream.Write(wBytes, 0, wBytes.Length);  
                    }  
                    else  
                    {  
                        if (File.Exists(wsAnswerFile))  
                        {  
                            SendFile(wsAnswerFile, wNetStream);  
                        }  
                    }  
                }  
            }  
            catch (Exception E)  
            {  
                MessageBox.Show(E.Message, "Network Error");  
            }  
            finally  
            {  
                if (wNetStream != null)  
                {  
                    wNetStream.Close();  
                }  
            }  
        }  
    }  
}  


When you are calling ProcessMessage and passing it a TCPClient, how are you initializing those TCPCLient connections? If you are doing it in a loop without multithreading then you may get results like the ones you've explained. Of course I can't be sure without knowing how you have your client listening loop setup.

Check out this link: http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server The second code snippet shows a listener loop that will start a new thread for each client. If you don't start a new thread for each client, if a new one is trying to connect while you are receiving data from the other one, it will say the server can't respond.

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜