[英]C# tcp socket client downloads files from server
目前我有這個問題。 客戶端第一次僅從服務器成功下載。 第二次它不起作用(什么也沒有發生,沒有崩潰)。 下面是雙方的代碼:
在客戶端的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錯誤,或者我的代碼中有某些內容阻止調試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.