日期:2014-05-18  浏览次数:21043 次

求教!关于Socket通讯的优化,如何应对通讯速度慢的问题
使用Socket通讯时总是被速度问题困扰,尤其是在同时连续多次Socket通讯时。
这里请教各位大大几点问题:
1.我的客户端可能同时向服务器多次Socket请求数据,但次数又不是很多,所以没有用异步Socket,而是使用多线程Socket。现在我是定义了一个SocketConnect类,类里方法如下:
C# code

/// <summary>
        /// Socket通讯接受后转换成字符串
        /// </summary>
        /// <param name="ip">Socket通讯IP</param>
        /// <param name="sendStr">发送内容</param>
        /// <returns>通讯后终端返回字符串</returns>
        public static string SocketSendAndReceiveString(IPEndPoint ip, string sendStr)
        {
            lock(ip)
            {

                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
                    s.Connect(ip);
                    byte[] sendBy = Encoding.UTF8.GetBytes(sendStr);
                    s.Send(sendBy, sendBy.Length, 0);
                    string recvStr = "";
                    byte[] recvBy = new byte[10024];
                    int bytes = 0;
                    bytes = s.Receive(recvBy, recvBy.Length, 0);
                    recvStr += Encoding.UTF8.GetString(recvBy, 0, bytes);
                    do
                    {
                        try
                        {
                            StringReader sr = new StringReader(recvStr);
                            XmlTextReader reader = new XmlTextReader(sr);
                            DataSet ds = new DataSet();
                            ds.ReadXml(reader);
                            break;
                        }
                        catch
                        {
                            bytes = s.Receive(recvBy, recvBy.Length, 0);
                            recvStr += Encoding.UTF8.GetString(recvBy, 0, bytes);
                        }
                    } while (true);
                    s.Close();
                    return recvStr;
                }
                catch (Exception ex)
                {
                    s.Close();
                    return null;
                    //throw ex;
                }
            }
        }

代码写的很烂...呵呵,这个方法就好像是公共资源一样,每次线程Socket都调用它。为了防止死锁,我把ip锁上了。不知道这样好不好,反正是一但当前占用的线程通讯慢的话后面的就都要等了...有没有其他的方法。
2.将Socket放入线程中,怎样才能随时的终止它呢。如果线程a里的Socket出现了问题半天没有返回,我想线程a终止了另开一个线程重新通讯,但是最终线程a在后台还是抛出了异常,这个怎么弄好呀?
例如:
按钮BUTTON单击后新建线程进行Socket通讯,但其实没有连接,这样肯定连不上抛出异常,catch捕捉异常给予提示,那我要是多次单击BUTTON呢,会弹出很多提示,怎么才能之弹出一次提示。

------解决方案--------------------
改成多线程异步
------解决方案--------------------
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
s.Connect(ip);

每次发送接受都从新和服务段连接。 把socket连接提出去,保存起来,连接一次就可以。
另:异步方式最适合。

~~~复制的代码

一. Server

using System;
using System.Threading; // Sleeping
using System.Net; // Used to local machine info
using System.Net.Sockets; // Socket namespace
using System.Collections; // Access to the Array list

namespace ChatServer
{
/// <summary>
/// Main class from which all objects are created
/// </summary>
class AppMain
{
// Attributes
private ArrayList m_aryClients = new ArrayList(); // List of Client Connections
/// <summary>
/// Application starts here. Create an instance of this class and use it
/// as the main object.
/// </summary>
/// <param name=&