C#(socket)上传视频到云服务器
下面的代码是完整的控制台代码,需要拷贝去测试,需要修改我加红色的代码即可,也可以传输其它类型的大文件。
下面的服务器端代码可以运行在自己的计算机上,也可以运行在装有window server系统的云服务器上。
服务器端代码如下:
ServerDemo类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 远程服务器端
{
class ServerDemo
{
static void Main(string[] args)
{
RemoteServer server = new RemoteServer(20000);
}
}
}
RemoteServer类
/**
* 远程服务器端
* 功能:接收视频文件
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace 远程服务器端
{
class RemoteServer
{
Socket serverSocket;
//绑定本地服务器
public RemoteServer(int port)
{
try
{
//定义服务器Socket
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定ip和端口号
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
serverSocket.Bind(endPoint);
//指定最大连接客户端数量
serverSocket.Listen(10);
Console.WriteLine("服务器启动成功");
Accept();
}
catch (Exception)
{
Console.WriteLine("无法启动服务器");
}
}
/// <summary>
/// 接收连接到的客户端socket
/// </summary>
void Accept() {
Socket client = serverSocket.Accept(); //接收客户端socket
Console.WriteLine(((IPEndPoint)client.RemoteEndPoint).Port+" :连接成功");
Thread acceptThread = new Thread(Accept); //开启线程,继续接收客户端socket
acceptThread.Start(); //启动线程
Receive(client);
}
//定义字节存储空间,1M
byte[] byteArray = new byte[1024 * 1024];
//存储视频的路径
string path = Directory.GetCurrentDirectory() + "\\01_课时介绍.mp4"; //要接收到的文件保存的路径,
//需要修改的是01_课时介绍.mp4,保存在bin/Debug目录下,如果是发送视频文件可以不修改
/// <summary>
/// 接收数据的方法
/// </summary>
/// <param name="client"></param>
private void Receive(Socket client) {
Console.WriteLine(path);
//定义文件流
try
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
int SumReceiveCount = 0; //定义接收到的总字节数
while (true)
{
//接收数据
int intCount = client.Receive(byteArray);
SumReceiveCount = SumReceiveCount + intCount;
//写入数据
if (intCount > 0)
{
fs.Write(byteArray, 0, intCount);
if (SumReceiveCount== byteArray.Length) //判断是否接受完1M的数据
{
//发送接收完标志位
Console.WriteLine("服务器发送标志位啦");
Send(client, "Ack");
SumReceiveCount = 0;
}
}
}
}
}
catch (Exception)
{
Console.WriteLine("接收消息失败");
}
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="client">客户端socket</param>
/// <param name="msg">Ack回应</param>
private void Send(Socket client,string msg) {
//定义字节存储空间
byte []byteMsg=new byte[1024];
try
{
//把消息转成字节的形式
byteMsg = Encoding.UTF8.GetBytes(msg);
//发送消息
client.Send(byteMsg);
}
catch (Exception)
{
Console.WriteLine("发送消息失败");
}
}
}
}
客户端代码:
ClientDemo类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 客户端
{
class ClientDemo
{
static void Main(string[] args)
{
Client client = new Client();
client.Connect("47.94.0.36",20000); //连接云服务器,把47.94.0.36修改为你的云服务器的ip,如果在本
//机测试,则把它修改为127.0.0.1
// client.Connect("127.0.0.1",20000); //连接本地服务器
Console.ReadLine();
}
}
}
Client类:
/***
* 客户端
* 功能:发送视频文件
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace 客户端
{
class Client
{
Socket client; //客户端Socket
bool IsCanSend=true; //是否可以发送字节标志位
FileStream fs; //定义文件流
public Client()
{
}
/// <summary>
/// 连接远程服务器
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
public void Connect(string ip,int port) {
try
{
//初始化客户端socket
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//指定远程服务器的ip和端口号
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
//进行连接
client.Connect(endPoint);
Console.WriteLine("连接服务器成功");
//接收数据
Receive();
}
catch (Exception)
{
Console.WriteLine("无法连接到服务器");
}
}
/// <summary>
/// 接收消息
/// </summary>
private void Receive() {
//定义字节数组存储空间
byte []byteReceive=new byte[1024];
Send();
try
{
while (true)
{
//接收数据
if (client != null)
{
int countReceive = client.Receive(byteReceive);
if (countReceive > 0)
{
string msg = Encoding.UTF8.GetString(byteReceive, 0, countReceive);
if (msg == "Ack" || IsCanSend)
{
//发送数据
Send();
}
}
}
}
}
catch (Exception)
{
Console.WriteLine("无法接收数据");
}
}
byte []byteSend=new byte[1024*1024]; //1M字节空间,用来存储读取到的字节
/// <summary>
/// 发送数据
/// </summary>
private void Send() {
try
{
if (fs == null)
{
fs = new FileStream(@"E:\01_课时介绍.mp4", FileMode.OpenOrCreate); //把E:\01_课时介绍.mp4
//修改你要发送的视频文件目录
}
IsCanSend = false;
//读取文件
int countSend = fs.Read(byteSend, 0, byteSend.Length);
//发送数据
if (countSend > 0)
{
Console.Write("发送中....");
client.Send(byteSend);
}
//表示发送完毕,关闭数据流
else {
fs.Close();
if(client!=null){
client.Close(); //关闭资源
}
}
}
catch (Exception)
{
Console.WriteLine("发送数据失败");
}
}
}
}
需要先启动服务器,再启动客户端,远程连接服务器桌面的运行结果如下: