s上傳文件
⑴ c# c/s結構Socket上傳文件的代碼
發送端(client)
private void button2_Click(object sender, EventArgs e)
{
this.button2.Enabled = false;
Thread TempThread = new Thread(new ThreadStart(this.StartSend));
TempThread.Start();
}private void StartSend()
{
//FileInfo EzoneFile = new FileInfo(this.textBox1.Text);string path = @"E:old F directoryTangWeikangge ew1.jpg";
FileInfo EzoneFile = new FileInfo(path);
FileStream EzoneStream = EzoneFile.OpenRead();
int PacketSize = 100000;
int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));
// this.textBox8.Text = PacketCount.ToString();
// this.progressBar1.Maximum = PacketCount;
int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));
// this.textBox9.Text = LastDataPacket.ToString();
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("163.180.117.229"), 7000);Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipep);
// TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));
// TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));
// TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));
// TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));
byte[] data = new byte[PacketSize];
for(int i=0; i<PacketCount; i++)
{
EzoneStream.Read(data, 0, data.Length);TransferFiles.SendVarData(client, data);
// this.textBox10.Text = ((int)(i + 1)).ToString();
// this.progressBar1.PerformStep();
}if(LastDataPacket != 0)
{
data = new byte[LastDataPacket];EzoneStream.Read(data, 0, data.Length);
TransferFiles.SendVarData(client,data);
// this.progressBar1.Value = this.progressBar1.Maximum;
}
client.Close();EzoneStream.Close();
this.button2.Enabled = true;
}接收端(server)
private void button2_Click(object sender, EventArgs e)
{
//int i = 0;
//FileStream recfs = new FileStream("E:\kangge.jpg", FileMode.OpenOrCreate);
//Byte[] recbyte = new Byte[2000000];
//Socket hostsocket = receive.Accept();
//BinaryWriter newfilestr = new BinaryWriter(recfs);
//hostsocket.Receive(recbyte, recbyte.Length, SocketFlags.None);
//for (i = 0; i < recbyte.Length; i++)
//{
// newfilestr.Write(recbyte, i, 1);
//}
//recfs.Close();//hostsocket.Shutdown(SocketShutdown.Receive);
//hostsocket.Close();this.button2.Enabled = false;
Thread TempThread = new Thread(new ThreadStart(this.StartReceive));
TempThread.Start();
}
private void StartReceive()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("163.180.117.229"), 7000);Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(ipep);
server.Listen(10);
Socket client = server.Accept();
// IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
// string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
// string BagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
// int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));
// string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
int file_name = 1;string fileaddr = "E:\old F directory\TangWei\Copy of kangge\" + file_name.ToString() + ".jpg";
FileStream MyFileStream = new FileStream(fileaddr, FileMode.Create, FileAccess.Write);// int SendedCount = 0;
while(true)
{
byte[] data = TransferFiles.ReceiveVarData(client);
if(data.Length == 0)
{
break;
}
else
{
// SendedCount++;
MyFileStream.Write(data, 0, data.Length);
}
}MyFileStream.Close();
client.Close();
this.button2.Enabled = true;
}公共類。 TransferFiles
class TransferFiles
{public TransferFiles()
{}
public static int SendVarData(Socket s, byte[] data) // return integer indicate how many data sent.
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;
byte[] datasize = new byte[4];
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);//send the size of data array.while (total < size)
{
sent = s.Send(data, total, dataleft, SocketFlags.None);
total += sent;
dataleft -= sent;
}return total;
}public static byte[] ReceiveVarData(Socket s) // return array that store the received data.
{
int total = 0;
int recv;
byte[] datasize = new byte[4];
recv = s.Receive(datasize, 0, 4, SocketFlags.None);//receive the size of data array for initialize a array.
int size = BitConverter.ToInt32(datasize, 0);
int dataleft = size;
byte[] data = new byte[size];while (total < size)
{
recv = s.Receive(data, total, dataleft, SocketFlags.None);
if (recv == 0)
{
data = null;
break;
}
total += recv;
dataleft -= recv;
}return data;
}
}