gpt4 book ai didi

C# tcp socket客户端从服务器下载文件

转载 作者:可可西里 更新时间:2023-11-01 02:52:58 24 4
gpt4 key购买 nike

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

在客户端,在 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 正常。但客户端没有得到 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);
}

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

最佳答案

我自己解决了这个问题。实际上,我只是构建解决方案而不是运行调试,服务器和客户端应用程序都可以正常工作。问题似乎只有在我尝试调试时才会出现。也许这是一个 Visual Studio 错误,或者我的代码中有某些东西阻止了调试。

关于C# tcp socket客户端从服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4441935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com