1

目前我有这个问题。客户端仅第一次从服务器成功下载。第二次它不起作用(没有任何反应,没有崩溃)。这里是双方的代码:

在客户端,在 mainForm 中,如果我单击下载按钮,我将从另一个类 loginForm 调用方法 sendComment(string request)。

在服务器端,收到客户端的字符串请求后,服务器会调用 sendComment(string listFiles)。listFiles 包含客户端需要下载的所有文件的名称和大小。

字符串 listFiles 格式:“commitRequest mName usID fiName1 fiSize1 fiName2 fiSize2...”。客户端收到此字符串后,将请求字符串中的每个文件。

客户端:

登录表格:

    private void Connect()
    {
        try
        {
            serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serversocket.Blocking = true;

            IPHostEntry IPHost = Dns.Resolve(textBox1.Text);
            string[] aliases = IPHost.Aliases;
            IPAddress[] addr = IPHost.AddressList;

            IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
            serversocket.Connect(ipepServer);
            clientsock = serversocket;

            Thread MainThread = new Thread(new ThreadStart(listenclient));
            MainThread.Start();
            MessageBox.Show("Connected successfully", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (SocketException se)
        {
            Console.WriteLine(se.Message);
        }
        catch (Exception eee)
        {
            MessageBox.Show("Socket Connect Error.\n\n" + eee.Message + "\nPossible Cause: Server Already running. Check the tasklist for running processes", "Startup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }
    }
    void listenclient()
    {
        Socket sock = clientsock;
        string cmd = server;
        byte[] sender = System.Text.Encoding.ASCII.GetBytes("CLIENT " + cmd);
        sock.Send(sender, sender.Length, 0);

            while (sock != null)
            {
                cmd = "";
                byte[] recs = new byte[32767];
                int rcount = sock.Receive(recs, recs.Length, 0);
                string clientmessage = System.Text.Encoding.ASCII.GetString(recs);
                clientmessage = clientmessage.Substring(0, rcount);

                string smk = clientmessage;

                cmdList = null;
                cmdList = clientmessage.Split(' ');
                string execmd = cmdList[0];

                sender = null;
                sender = new Byte[32767];

                string parm1 = "";


                if (execmd == "CommitRequest") 
                {
                    for (int i = 3; i < cmdList.Length - 1; i++)
                    {
                        if (i % 2 == 1)
                        {
                            sendComment("downloadFile " + cmdList[i]); // after receiving this, server will upload the file requested
                            downloadMFromServer(sock, cmdList[2], cmdList[1], cmdList[i], cmdList[i + 1]);                                  
                        }

                    }
                    continue;
                }
            }


private void downloadMFromServer(Socket s, string userID, string mName, string fileN, string fileS)
    {                
                    Socket sock = s;
                    string rootDir;
                    rootDir = @"C:\Client Data" + "\\" + userID + "\\" + mName;
                    Directory.CreateDirectory(rootDir);
                    System.IO.FileStream fout = new System.IO.FileStream(rootDir + "\\" + fileN, FileMode.Create, FileAccess.Write);
                    NetworkStream nfs = new NetworkStream(sock);
                    long size = int.Parse(fileS);
                    long rby = 0;
                    try
                    {
                        while (rby < size)
                        {
                            byte[] buffer = new byte[1024];
                            int i = nfs.Read(buffer, 0, buffer.Length);
                            fout.Write(buffer, 0, (int)i);
                            rby = rby + i;
                        }
                        fout.Close();
                    }
                    catch (Exception ed)
                    {
                        Console.WriteLine("A Exception occured in file transfer" + ed.ToString());
                        MessageBox.Show(ed.Message);
                    }
                }

第一次点击下载按钮后,文件成功下载到客户端,然后我删除了所有下载的文件,然后我第二次点击了下载按钮,但这次没有成功。

文件没有被下载。我尝试调试,它没有显示任何错误,但客户端应用程序在从服务器接收字符串 listFiles 的步骤停止。我的意思是客户端发送的字符串重新确定。服务器收到字符串重新确定。服务器发送字符串 listFiles ok。但客户没有得到 listFiles。有谁知道为什么它不起作用?提前感谢您的帮助。

这是 sendComment 方法的代码,客户端和服务器应用程序都相同。

public void sendComment(string comment)
{
    Socket serversock = serversocket;
    if (serversock == null)
    {
        MessageBox.Show("Client not connected", "Connect Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
     byte[] b = System.Text.Encoding.ASCII.GetBytes(comment + " ");
    serversock.Send(b, b.Length, 0);
}

我不上传服务器端代码,因为我认为它没有任何问题,而且这篇文章会有点长,但如果你需要说,我会发布它。

4

1 回答 1

1

I just solved this on my own. Actually, I just build the solution instead of running debug and both server and client app work fine. The problem seems to rise only when I try debugging. Maybe it's a Visual Studio bug or there's something in my code prevent the debug.

于 2010-12-16T07:31:03.547 回答