socket客户端使用后台线程

时间:2014-02-27 21:21:47

标签: c# multithreading sockets tcpclient

我正在尝试编写一个从服务器接收字符串的C#客户端。我创建了一个后台线程,它应该始终等待来自服务器的响应。当我启动程序时,我按下一个按钮(button1_Click_1)来启动发送和接收消息的线程,但我只收到一些消息(可能是每10个消息中有1个)。我做错了什么?

System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
Thread threadReceiveData;

string serverIP = some ip;    

static int port = 8200;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        msg("Client Started");
        clientSocket.Connect(serverIP, port);
        msg("Client Socket Program - Server Connected ...");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        threadReceiveData = new Thread(new ThreadStart(startReceivingTrades));
        threadReceiveData.IsBackground = true;
        threadReceiveData.Start();     
    }

    private void startReceivingTrades()
    {
        while (true)
        {

            try
            {
                NetworkStream serverStream = clientSocket.GetStream();
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes("SendMeStuff" + System.Environment.NewLine);
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();

                byte[] inStream = new byte[10025];
                serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                msg("Data from Server : " + returndata);
            }

            catch (Exception ex)
            {
                msg(ex.ToString());
            }

        }
    }

3 个答案:

答案 0 :(得分:1)

不知道你的协议,这有假设,但我认为你的意思是这样的(即你只需要获得一次流,并且只发送一次sendmestuff消息:

private void startReceivingTrades()
{
    NetworkStream serverStream;
        try
        {
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("SendMeStuff" + System.Environment.NewLine);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        catch (Exception ex)
        {
            msg(ex.ToString());
        }

    while (true)
    {

        try
        {
            byte[] inStream = new byte[clientSocket.ReceiveBufferSize];
            int bytesRead = serverStream.Read(inStream, 0, (int)inStream.Length);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream, 0, bytesRead);
            msg("Data from Server : " + returndata);
        }
        catch (Exception ex)
        {
            msg(ex.ToString());
        }

    }
}

答案 1 :(得分:1)

当出现TCP问题时,我立即转到第一个Read调用,因为我总是发现它的返回值未使用。使用其返回值来查找实际接收的字节数。

答案 2 :(得分:1)

你的意思是不断发送“SendMeStuff”吗?似乎更应该只在循环之外发送一次

另外,在GetString()调用中使用Read()中的返回值 ...并传递正在使用的实际缓冲区的长度:

private void startReceivingTrades()
{
    NetworkStream serverStream = clientSocket.GetStream();
    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("SendMeStuff" + System.Environment.NewLine);
    serverStream.Write(outStream, 0, outStream.Length);
    serverStream.Flush();

    byte[] inStream = new byte[10025];
    while (true)
    {
        try
        {
            int count = serverStream.Read(inStream, 0, inStream.Length);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream, 0, count);
            msg("Data from Server : " + returndata);
        }

        catch (Exception ex)
        {
            msg(ex.ToString());
        }
    }
}