服务器:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace FileServer
{
/// <summary>
/// 数据包格式
/// </summary>
public struct Buffer
{
public int cmd;
public int length;
public string context;
public Buffer(int n_cmd, int n_length, string s_context)
{
cmd = n_cmd;
length = n_length;
context = s_context;
}
} public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnReady_Click(object sender, EventArgs e)
{
bool done = false;
Socket mySocket;
Buffer recBuff = new Buffer();
Buffer sendBuff = new Buffer();
byte[] recByte = new byte[1024];
byte[] sendByte = new byte[1024];
int n_recBytes;
string tempData; //for receive
string sTemp; //for send
string filename;
int n_pointer;
FileStream fs;
BinaryWriter fw; // create the socket
Socket listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp); // bind the listening socket to the port
IPAddress hostIP = (Dns.GetHostEntry(Dns.GetHostName())).AddressList[0];
IPEndPoint ep = new IPEndPoint(hostIP, int.Parse(txtPort.Text));
filename = "D:\\ttt\\";
txtMsg.Text ="等待连接请求......";
try
{
listenSocket.Bind(ep);
while(true)
{
// start listening
listenSocket.Listen(10);
mySocket = listenSocket.Accept();
while (!done)
{
n_recBytes = mySocket.Receive(recByte);
if (n_recBytes > 0)
{
tempData = Encoding.UTF8.GetString(recByte, 0, n_recBytes);
n_pointer = tempData.IndexOf(",");
recBuff.cmd = int.Parse(tempData.Substring(0, n_pointer));
tempData = tempData.Substring(tempData.IndexOf(",") + 1);
n_pointer = tempData.IndexOf(",");
recBuff.length = int.Parse(tempData.Substring(0, n_pointer));
tempData = tempData.Substring(tempData.IndexOf(",") + 1);
//recBuff.context = tempData.Substring(0, recBuff.length);
recBuff.context = tempData;
UTF8Encoding AE = new UTF8Encoding();
int bytesSent;
switch (recBuff.cmd)
{
case 1: //Create file to save -----D:\\ttt folder
filename += recBuff.context;
// Delete the file if it exists.
if (File.Exists(filename))
{
File.Delete(filename);
} // Create the file.
fs = new FileStream(filename, FileMode.CreateNew);
fw = new BinaryWriter(fs);
sendBuff.cmd = 2; //告诉客户端,已经建立了文件,准备接收文件内容
sendBuff.length = 1;
sendBuff.context = "0"; //可以是准备接收的包号,目前没有使用
sTemp = sendBuff.cmd.ToString() + "," + sendBuff.length.ToString() + "," + sendBuff.context;
sendByte = AE.GetBytes(sTemp);
// Send the data through the socket.
bytesSent = mySocket.Send(sendByte);
fs.Close();
fw.Close();
break;
case 2:
// write file
fs = new FileStream(filename, FileMode.Append);
fw = new BinaryWriter(fs);
fw.Write(recBuff.context);
fs.Close();
fw.Close();
sendBuff.cmd = 2; //告诉客户端,可以发送下一个数据包
sendBuff.length = 1;
sendBuff.context = "0"; //可以是准备接收的包号,目前没有使用
sTemp = sendBuff.cmd.ToString() + "," + sendBuff.length.ToString() + "," + sendBuff.context;
sendByte = AE.GetBytes(sTemp);
bytesSent = mySocket.Send(sendByte);
break;
case 3: // the file is finished, close the file
txtMsg.Text = "完成了文件的接收......";
done = true; break;
default:
//fw.Close();
//fs.Close();
break;
}
}
}
mySocket.Close();
filename = "D:\\ttt\\";
done = false;
}
}
catch (Exception se)
{
txtMsg.Text = se.ToString();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace FileServer
{
/// <summary>
/// 数据包格式
/// </summary>
public struct Buffer
{
public int cmd;
public int length;
public string context;
public Buffer(int n_cmd, int n_length, string s_context)
{
cmd = n_cmd;
length = n_length;
context = s_context;
}
} public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnReady_Click(object sender, EventArgs e)
{
bool done = false;
Socket mySocket;
Buffer recBuff = new Buffer();
Buffer sendBuff = new Buffer();
byte[] recByte = new byte[1024];
byte[] sendByte = new byte[1024];
int n_recBytes;
string tempData; //for receive
string sTemp; //for send
string filename;
int n_pointer;
FileStream fs;
BinaryWriter fw; // create the socket
Socket listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp); // bind the listening socket to the port
IPAddress hostIP = (Dns.GetHostEntry(Dns.GetHostName())).AddressList[0];
IPEndPoint ep = new IPEndPoint(hostIP, int.Parse(txtPort.Text));
filename = "D:\\ttt\\";
txtMsg.Text ="等待连接请求......";
try
{
listenSocket.Bind(ep);
while(true)
{
// start listening
listenSocket.Listen(10);
mySocket = listenSocket.Accept();
while (!done)
{
n_recBytes = mySocket.Receive(recByte);
if (n_recBytes > 0)
{
tempData = Encoding.UTF8.GetString(recByte, 0, n_recBytes);
n_pointer = tempData.IndexOf(",");
recBuff.cmd = int.Parse(tempData.Substring(0, n_pointer));
tempData = tempData.Substring(tempData.IndexOf(",") + 1);
n_pointer = tempData.IndexOf(",");
recBuff.length = int.Parse(tempData.Substring(0, n_pointer));
tempData = tempData.Substring(tempData.IndexOf(",") + 1);
//recBuff.context = tempData.Substring(0, recBuff.length);
recBuff.context = tempData;
UTF8Encoding AE = new UTF8Encoding();
int bytesSent;
switch (recBuff.cmd)
{
case 1: //Create file to save -----D:\\ttt folder
filename += recBuff.context;
// Delete the file if it exists.
if (File.Exists(filename))
{
File.Delete(filename);
} // Create the file.
fs = new FileStream(filename, FileMode.CreateNew);
fw = new BinaryWriter(fs);
sendBuff.cmd = 2; //告诉客户端,已经建立了文件,准备接收文件内容
sendBuff.length = 1;
sendBuff.context = "0"; //可以是准备接收的包号,目前没有使用
sTemp = sendBuff.cmd.ToString() + "," + sendBuff.length.ToString() + "," + sendBuff.context;
sendByte = AE.GetBytes(sTemp);
// Send the data through the socket.
bytesSent = mySocket.Send(sendByte);
fs.Close();
fw.Close();
break;
case 2:
// write file
fs = new FileStream(filename, FileMode.Append);
fw = new BinaryWriter(fs);
fw.Write(recBuff.context);
fs.Close();
fw.Close();
sendBuff.cmd = 2; //告诉客户端,可以发送下一个数据包
sendBuff.length = 1;
sendBuff.context = "0"; //可以是准备接收的包号,目前没有使用
sTemp = sendBuff.cmd.ToString() + "," + sendBuff.length.ToString() + "," + sendBuff.context;
sendByte = AE.GetBytes(sTemp);
bytesSent = mySocket.Send(sendByte);
break;
case 3: // the file is finished, close the file
txtMsg.Text = "完成了文件的接收......";
done = true; break;
default:
//fw.Close();
//fs.Close();
break;
}
}
}
mySocket.Close();
filename = "D:\\ttt\\";
done = false;
}
}
catch (Exception se)
{
txtMsg.Text = se.ToString();
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货