C#里面的socket编程,以下是我写的一个简单里面,是用户客户端接收服务端文件用的。
参考了msdn官网里面的例子(tcp聊天例子);本人菜鸟,写的不好请见谅。
https://docs.microsoft/zh-cn/dotnet/api/system.sockets.tcplistener?view=net-5.0
https://docs.microsoft/zh-cn/dotnet/api/system.sockets.tcpclient?view=net-5.0
首先百度一些socket的概念,如下(具体我不太懂)
Socket【套接字】
用于两个程序之间通信,描述IP地址和port端口号。
根据通信质量和效率要求,分为几种协议传输数据。
(1)TCP协议
TCP是一种面向连接的、可靠的,基于字节流的传输层通信协议。这种协议确保了数据传输的可靠性,但是效率比较低。
(2)UDP协议
UDP是一种简单的、面向数据报的无连接的协议。这种协议虽然不能提供可靠的数据传输,但是效率很高。
下面开始写tcp传输文件的例子代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace SocketTest2
{
/// <summary>
/// 服务器端
/// </summary>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Server Listening");
TcpListener listener = null;
try
{
listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 50012);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
if (client.Connected)
{
Console.WriteLine("client connected");
FileStream fstream = new FileStream("D:\\SocketTest2\\bin\\testmo.txt", FileMode.Open, FileAccess.Read);
NetworkStream networksm = client.GetStream();
int readSize = 0;
long fileLength = 0;
while (fileLength < fstream.Length)
{
byte[] bff = new byte[1024];
readSize= fstream.Read(bff, 0, bff.Length);
networksm.Write(bff, 0, readSize);
fileLength += readSize;
}//end while send file
//close client and stream
fstream.Flush();
networksm.Flush();
fstream.Close();
networksm.Close();
client.Close();
Console.WriteLine("file has already send to client");
}
}//end while listen
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (listener != null) {
listener.Stop();
}
}
Console.ReadKey();
Console.WriteLine("Process END");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace SocketClient2
{
/// <summary>
/// 客户端
/// </summary>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("TCP Client begin to receive");
TcpClient client = null;
try
{
client = new TcpClient("127.0.0.1", 50012);
if (client.Connected)
{
NetworkStream networksm = client.GetStream();
string dirPath =AppDomain.CurrentDomain.BaseDirectory + "\\WebFile";
if (Directory.Exists(dirPath) == false) {
Directory.CreateDirectory(dirPath);
}
dirPath += "\\testmo.txt";
FileStream fstream = new FileStream(dirPath, FileMode.Create, FileAccess.Write);
int readSize = 0;
byte[] bff = new byte[1024];
while ( ( readSize= networksm.Read(bff , 0 , bff.Length)) > 0)
{
fstream.Write(bff, 0, readSize);
}//end while write network new file
//close tcp connection
fstream.Flush();
fstream.Close();
networksm.Flush();
networksm.Close();
Console.WriteLine("receive file success");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (client != null)
{
client.Close();
}
}
Console.ReadKey();
Console.WriteLine("client end process");
}
}
}
备注:
(1)代码里面的ip地址 127.0.0.1 代表本机(localhost);大家可以在内网中测试不同的IP地址。内网中的IP地址要可以互相ping通才行。
(2)发送或者接收文件数据的时候,定义了一个固定长度的byte[ ] 数组,由于不同文件的大小是不同的,有的几KB,有的几MB , 所以要用while语句循环将stream 处理。
更多推荐
TcpListener和TcpClient传输文件
发布评论