C#SOCKET长连接小数据程序未响应

最近由于业务的需要,Linux系统嵌入式设备往我做的服务程序发送数据。
速度奇慢,而且还是程序无响应。后来自己用C#写了一个客户端用来测试。已经用了线程池,不知道用法对不对。请大家帮忙看下。

服务端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace consoleSocketServer
{
class Program
{
static int socketCount = 0;
static Socket serverSocket;
static Socket clientSocket;
public static int clientcount = 0;
public int clientnum = 0;
static int listSQL = 0;
static bool asy = true;
public static Int32 portServer = 0;
public static string ipServer = "";
public static Thread threadSocketListen;
public static Thread threadDoWork;
public static Thread threadInsert;
public static int countsql = 0;
static void Main(string[] args)
{
IPAddress ipAddress;
portServer = 4660;
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        ipAddress = IPAddress.Parse("192.168.33.29");
        ipServer = ipAddress.ToString();

        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, portServer);

        Console.WriteLine("服务器IP:" + ipServer + "  端口:" + portServer);
        serverSocket.Bind(localEndPoint);
        serverSocket.Listen(20);
        //设置线程池
        ThreadPool.SetMaxThreads(4, 4);
        //开启新的线程监听
        threadSocketListen = new Thread(SocketListen);
        threadSocketListen.Start();


        Console.WriteLine("服务启动成功");
    }

    static void SocketListen()
    {
        while (true)
        {

            //接受客户端信息
            clientSocket = serverSocket.Accept();
            //开启处理线程
            ThreadPool.QueueUserWorkItem(new WaitCallback(doWork));

            //threadDoWork = new Thread(new ThreadStart(doWork));
            //threadDoWork.Start();

        }
    }
    /// <summary>
    /// 处理方法
    /// </summary>
    static void doWork(object a)
    {

        //获取客户端信息 
        IPEndPoint ipEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint;
        string address = ipEndPoint.Address.ToString();
        //Console.WriteLine(address + ":连接成功");


        //取消息
        byte[] inBuffer = new byte[1024];
        byte[] outBuffer = new byte[8];
        string inBufferStr = "";
        int rec = 0;
        StringBuilder sb = new StringBuilder();
        while (true)
        {
            try
            {
                rec = clientSocket.Receive(inBuffer);//如果没有消息为空 阻塞 当前循环 
                //收到的长度不够 忽略此次数据
                if (rec <= 8)
                {
                    continue;
                }


                int ll = inBuffer[4] + inBuffer[5] * 255;


                inBufferStr = Encoding.UTF8.GetString(inBuffer, 8, ll);


                Console.WriteLine(inBufferStr);


                //回复收到 
                outBuffer[4] = 0;
                outBuffer[5] = 0;
                outBuffer[6] = 0;
                outBuffer[7] = 0;
                outBuffer[3] = 0;
                outBuffer[2] = 0;
                outBuffer[1] = 0;
                outBuffer[0] = 1;

                clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);
                socketCount++;
                Console.WriteLine(socketCount.ToString());
                //break;
            }
            catch (SocketException es)
            {
                //clientSocket.Close();
                return;
            }

        }

    }
}

}

客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace SocketClient
{
class Program
{
static Socket clientSocket;
static void Main(string[] args)
{
try
{
//将网络端点表示为IP地址和端口 用于socket侦听时绑定
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.33.29"), 4660);

            clientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            //将Socket连接到服务器 
            clientSocket.Connect(ipep);

            string outBufferStr;
            //发送消息 
            outBufferStr = "123456789qwe|789654123|1|2015-04-22 10:51:34|6|test1|test2| ";
            byte[] tempb = Encoding.UTF8.GetBytes(outBufferStr);


            byte[] outBuffer = new byte[tempb.Length + 8];


            //加功能和长度
            outBuffer[0] = 1;
            outBuffer[1] = 0;
            outBuffer[2] = 0;
            outBuffer[3] = 0;
            outBuffer[4] = Convert.ToByte(tempb.Length % 255);
            outBuffer[5] = Convert.ToByte(tempb.Length / 255);
            outBuffer[6] = 0;
            outBuffer[7] = 0;

            for (int i = 0; i < tempb.Length; i++)
            {
                outBuffer[8 + i] = tempb[i];
            }

            // Byte[] inBuffer = new Byte[1024];

            int kkk = 0;
            while (kkk <= 10000)
            {

                clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);
                kkk++;
                Thread.Sleep(1);
                Console.WriteLine(kkk.ToString());
                //Console.WriteLine("服务器响应:");
                //接收服务器端信息 
                //clientSocket.Receive(inBuffer, 1024, SocketFlags.None);
                //Console.WriteLine(Encoding.ASCII.GetString(inBuffer));
            }
            Console.WriteLine(kkk.ToString());
            Console.ReadLine();
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
            Console.ReadLine();

        }
    }

}

}

客户端收到数据,显示出来,在回发一个byte[] 1 0 0 0 0 0 0 0 用于确认收到。
实际上目前客户端没有对服务端收到的数据做处理,因为那样又要多开一个线程。
但是现在问题就是这样简单的测试发送10000条数据,居然会卡死,很纳闷。我前几天见他们Linux自己写的程序发送1W条数据,就是2 3秒的时间。请大神指导。难道C#真的是不能做服务端的程序?

1个回答

。。。。我的天,我感觉我不会用CSDN发帖发错地方了。

大家帮忙还是去看那个帖子吧

http://bbs.csdn.net/topics/391021913

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问