C#socket只收到第一条消息

时间:2015-03-10 20:36:08

标签: c# sockets server

我已经搜索过许多示例和教程以及什么不是,但我不能为我的生活弄清楚我在这里做错了什么...如果我发送几条消息到这个服务器我只做了第一个是在控制台打印。 Writeeline命令,其余部分从未打印过......我必须做一些基本错误的事情,但我真的找不到......:S

这是服务器代码:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms.VisualStyles;

namespace HL7_Manager
{
public class MonitorServer
{
    private int _port;
    private Socket _serverSocket;
    private List<ClientObject> _clients;

    public bool IsConnected { get; set; }
    public MonitorServer(int port)
    {
        _port = port;
        _clients = new List<ClientObject>();
    }

    public void StartListening()
    {
        _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        Thread listenThread = new Thread(new ThreadStart(ListenerThread));
        listenThread.IsBackground = true;
        listenThread.Start();
    }

    public void StopListening()
    {
        IsConnected = true;
        _serverSocket.Close();
        while (_clients.Count > 0)
        {
            _clients[0].KeepProcessing = false;
            _clients[0].ClientSocket.Close();
            _clients.RemoveAt(0);
        }
    }

    private void ListenerThread()
    {
        _serverSocket.Bind(new IPEndPoint(IPAddress.Any, _port));
        _serverSocket.Listen(100);

        Console.WriteLine("Listening on port 8000");

        while (true)
        {
            Socket clientSocket = _serverSocket.Accept();
            ClientObject client = new ClientObject();
            client.KeepProcessing = true;
            client.ClientSocket = clientSocket;
            _clients.Add(client);
            ParameterizedThreadStart ptStart = new ParameterizedThreadStart(ProcessClientThread);
            Thread processThread = new Thread(ptStart);
            processThread.IsBackground = true;
            processThread.Start(client);
            clientSocket = null;
            client = null;
        }
    }

    private void ProcessClientThread(object clientObj)
    {
        Console.WriteLine("Client connected");
        ClientObject client = (ClientObject) clientObj;
        Socket clientSocket = client.ClientSocket;
        byte[] buffer = new byte[clientSocket.ReceiveBufferSize];
        int receiveCount = 0;

        while (client.KeepProcessing)
        {
            try
            {
                receiveCount = clientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                Console.WriteLine(Encoding.ASCII.GetString(buffer));
            }
            catch (Exception ex)
            {
                if (!client.KeepProcessing)
                    return;
                Console.WriteLine(ex.Message);
            }
        }
        clientSocket.Close();
        _clients.Remove(client);
    }
}
}

2 个答案:

答案 0 :(得分:1)

这是您应该明确更改的方法以及如何更改它。

    private void ProcessClientThread(object clientObj)
    {
        Console.WriteLine("Client connected");
        ClientObject client = (ClientObject)clientObj;
        Socket clientSocket = client.ClientSocket;
        byte[] buffer = new byte[clientSocket.ReceiveBufferSize];
        int receiveCount = 0;

        while (client.KeepProcessing)
        {
            try
            {
                receiveCount = clientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                if (receiveCount == 0)
                    break; //the client has closed the stream
                var ret = Encoding.ASCII.GetString(buffer, 0, receiveCount);
                Console.WriteLine(ret);
            }
            catch (Exception ex)
            {
                if (!client.KeepProcessing)
                    return;
                Console.WriteLine(ex.Message);
            }
        }
        clientSocket.Close();
        _clients.Remove(client);
    }

答案 1 :(得分:0)

检查您真正收到的字节数。 TCP是一种流媒体协议,这意味着如果您的客户端正在一个接一个地发送几个小消息,您将在接收器中一次性接收它们。 如果您的接收缓冲区中碰巧出现空字符,您可能会认为您没有收到所有这些字符串,但实际上您已经收到了。 通过检查收到的字节数和检查缓冲区内容来检查这一点。 如果你犯了这个错误,你的代码可能会有一些更深层次的问题。 TCP流式传输的事实使它更加复杂