c# - 通过 TCPIP 发送字符串和文件的简单方法
问题描述
我想做一个简单的客户端-服务器应用程序。服务器等待来自客户端的连接。客户端可以向服务器发送各种查询(少于 100 个字符的短字符串),服务器应通过发送字符串或文件来响应这些查询。我在网上找到了使用 tcpListener.AcceptSocket() 的代码,我可以发送和接收来自服务器的字符串和使用 tcpListener.AcceptTcpClient() 的代码,我可以将文件发送到不同的服务器程序。下面有一个通过 TcpIP 发送文件的函数和一个通过 TcpIP 发送字符串的函数。服务器代码应该如何适应这两个功能?服务器是一个控制台程序。
public void SendFileViaTCP(string sFile, string sIPAddress, int portNo)
{
byte[] sendingBuffer = null;
TcpClient client = null;
toolStripStatusLabel1.Text = "";
lbConnect.Items.Clear();
progressBar1.Value = 0;
Application.DoEvents();
NetworkStream networkStream = null;
int bufferSize = 5000;
string checksum = CalculateMD5(sFile);
lbConnect.Items.Add("File " + sFile + " checksum = " + checksum);
try
{
client = new TcpClient(sIPAddress, portNo);
toolStripStatusLabel1.Text = "Connected to the Server...\n";
networkStream = client.GetStream();
FileStream fileStream = new FileStream(sFile, FileMode.Open, FileAccess.Read);
long fsLength = fileStream.Length;
int nPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(fsLength) / Convert.ToDouble(bufferSize)));
progressBar1.Maximum = nPackets;
Application.DoEvents();
int currentPacketLength;
for (int i = 0; i < nPackets; i++) {
if (fsLength > bufferSize) {
currentPacketLength = bufferSize;
fsLength -= currentPacketLength;
Application.DoEvents();
}
else {
currentPacketLength = Convert.ToInt32(fsLength);
}
sendingBuffer = new byte[currentPacketLength];
fileStream.Read(sendingBuffer, 0, currentPacketLength);
networkStream.Write(sendingBuffer, 0, (int)sendingBuffer.Length);
progressBar1.PerformStep();
Application.DoEvents();
}
toolStripStatusLabel1.Text = "Sent " + fileStream.Length.ToString() + " bytes to the server";
fileStream.Close();
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
finally {
networkStream.Close();
client.Close();
}
}
void SendString(string str, string sIPAddress, int portNo)
{
try {
TcpClient tcpClient = new TcpClient();
lbConnect.Items.Add("Connecting...");
Application.DoEvents();
// use the ipaddress as in the server program
var tsk = tcpClient.ConnectAsync(sIPAddress, portNo);
tsk.Wait(3000); // here we set how long we want to wait before deciding the server is not responding.
lbConnect.Items.Add("Connected");
lbConnect.Items.Add("Sending string: " + str);
Application.DoEvents();
Stream stream = tcpClient.GetStream();
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
lbConnect.Items.Add("Transmitting...");
Application.DoEvents();
stream.Write(ba,0,ba.Length);
byte[] bb=new byte[100];
int k = stream.Read(bb,0,100);
string sResponse = string.Empty;
for (int i = 0; i < k; i++) {
sResponse += Convert.ToChar(bb[i]);
}
lbConnect.Items.Add(sResponse);
Application.DoEvents();
tcpClient.Close();
}
catch (Exception e) {
lbConnect.Items.Add("Error: " + e.StackTrace);
Application.DoEvents();
}
}
解决方案
推荐阅读
- tensorflow - Tensorflow 服务于使用 saved_model 保存的训练模型
- karate - The best conditional logic for match and match each case
- python - 无法弄清楚为什么在尝试将 JSON 值输入 DF 时出现错误
- ios - 未找到自定义模块
- webpack - 如何缩小遗留 JavaScript 库(ES6)
- python - 如何在 Python 中创建从最后一次击键到当前时间的计时器?
- javascript - jquery block ui 获取 $.blockUI 不是函数
- c - C: Linux Kernel - 如何在 struct ieee80211_mgmt 中使用 union 的变量字段数组(u8 变量 [0])?
- apache-kafka - 如何在 Kafka 主题中找到消息的偏移量
- amazon-web-services - 更改 EKS 中的 Horizontal-pod-autoscaler-sync-period