套接字服务器只接受第一次呼叫

时间:2015-06-07 14:33:14

标签: c# sockets

我在c#中编写了一个简单的客户端 - 服务器应用程序。一切都很好,但服务器只接受客户端的第一次呼叫而不再接受。我试图将receive方法放在循环中(作为acceptTcpSocket方法),但它仍然是相同的。

简化的服务器代码:

public class XMLServer
{
    public void start()
    {
        server = new TcpListener(_serverIP, _serverPort);

        try
        {
            server.Start();
        }
        catch (SocketException socketError)
        {
            Console.WriteLine(socketError.Message);
        }
    }
    public void listen()
    {
        try
        {
            client = server.AcceptTcpClient();

            while (true)
            {
                receiveFromClient();
            }
        }
        catch (SocketException error)
        {
            Console.WriteLine(error.Message);
        }
    }

    public void receiveFromClient()
    {
        byte[] bytes = new byte[client.ReceiveBufferSize];
        byte[] send;
        int readed;

        stream = client.GetStream();
        readed = stream.Read(bytes, 0, client.ReceiveBufferSize);

        if (readed > 0)
        {
            string[] request = Encoding.UTF8.GetString(bytes).Split(':');

            Console.WriteLine(request[0]);
            switch (request[0])
            {
                case "getFileList":
                    send = encode(XMLFile.getFileList());

                    if (stream.CanWrite)
                    {
                        stream.Write(send, 0, send.Length);
                    }
                    break;
                case "getFile":
                    send = encode(XMLFile.getFile(request[1]));

                    if (stream.CanWrite)
                    {
                        stream.Write(send, 0, send.Length);
                        stream.Flush();
                    }
                    break;
            }
        }
    }
}

使用服务器代码:

XMLServer server = new XMLServer("10.0.0.5", "7777");
server.start();

while (true)
{
    server.listen();
}

客户代码:

public partial class Client : Form
{
    private TcpClient client;
    private NetworkStream stream;

    public Client(TcpClient parentClient)
    {
        InitializeComponent();
        client = parentClient;
        getFileList();
    }

    private void getFileList()
    {
        byte[] fileList = Encoding.UTF8.GetBytes("getFileList:null");
        byte[] fileListResponse;
        string[] files;
        int Y = 30;

        stream = client.GetStream();
        stream.Write(fileList, 0, fileList.Length);

        fileListResponse = new byte[client.ReceiveBufferSize];
        stream.Read(fileListResponse, 0, client.ReceiveBufferSize);
        files = Encoding.UTF8.GetString(fileListResponse).Split(';');

        foreach (string file in files)
        {
            RadioButton radioButton = new RadioButton();
            radioButton.Text = file;
            radioButton.Location = new Point(10, Y);

            groupBoxFiles.Controls.Add(radioButton);
            Y += 30;
        }
    }

    private void buttonOpenFile_Click(object sender, EventArgs e)
    {
        String fileName = groupBoxFiles.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked).Text;
        byte[] getFile = Encoding.UTF8.GetBytes("getFile:" + fileName);
        byte[] getFileResponse;
        string fileContent;

        stream = client.GetStream();
        stream.Write(getFile, 0, getFile.Length);

        getFileResponse = new byte[client.ReceiveBufferSize];
        stream.Read(getFileResponse, 0, client.ReceiveBufferSize);
        fileContent = Encoding.UTF8.GetString(getFileResponse);

        textBoxEditor.Enabled = true;
        textBoxEditor.Text = fileContent;
    }
}

首先我打电话给XMLFile.getFileList,我的工作很好。然后我想打电话给XMLFile.getFile,之后应用停止。

有什么问题?

1 个答案:

答案 0 :(得分:0)

很难说明单一根本原因,但至少存在一个概念性问题:Stream.Read Method调用的结果,实际读取的字节数被忽略。

  

套接字的发送/接收功能不保证您在一次通话时发送/接收您提供的所有数据。这些函数返回实际发送/接收字节数。

     

- TCP/IP client-server application: exchange with string messages, Sergey Brunov

另外,请注意:

  

发送和接收数据时,必须关闭NetworkStream。关闭TcpClient不会释放NetworkStream。

     

- TcpClient.GetStream Method, MSDN