求C# TCP简单文件发送,接收代码

Harrison_2009 2009-09-06 11:09:03
求C# TCP简单文件发送,接收代码
主要是接收的。。。
...全文
256 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
a122329706 2009-09-27
  • 打赏
  • 举报
回复
qinghan
gino_cheung 2009-09-08
  • 打赏
  • 举报
回复
强悍~~~~~~
zhangyanyang 2009-09-08
  • 打赏
  • 举报
回复
mark
龙宜坡 2009-09-08
  • 打赏
  • 举报
回复
接收端

using System;
using System.Collections;
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;
using System.Threading;

namespace ReceiveFiles
{
public partial class Form1 : Form
{
private const int BufferSize = 1024;
public string Status = string.Empty;
public Thread T = null;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Server is Running...";
ThreadStart Ts = new ThreadStart(StartReceiving);
T = new Thread(Ts);
T.Start();


}
public void StartReceiving()
{
ReceiveTCP(29250);
}


public void ReceiveTCP(int portN)
{
TcpListener Listener = null;
try
{
Listener = new TcpListener(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], portN);
Listener.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

byte[] RecData = new byte[BufferSize];
int RecBytes;

for (; ; )
{
TcpClient client = null;
NetworkStream netstream = null;
Status = string.Empty;
try
{


string message = "Accept the Incoming File ";
string caption = "Incoming Connection";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;


if (Listener.Pending())
{
client = Listener.AcceptTcpClient();
netstream = client.GetStream();
Status = "Connected to a client\n";
result = MessageBox.Show(message, caption, buttons);

if (result == System.Windows.Forms.DialogResult.Yes)
{
string SaveFileName=string.Empty;
SaveFileDialog DialogSave = new SaveFileDialog();
DialogSave.Filter = "All files (*.*)|*.*";
DialogSave.RestoreDirectory = true;
DialogSave.Title = "Where do you want to save the file?";
DialogSave.InitialDirectory = @"C:/";
if (DialogSave.ShowDialog() == DialogResult.OK)
SaveFileName = DialogSave.FileName;
if (SaveFileName != string.Empty)
{
int totalrecbytes = 0;
FileStream Fs = new FileStream(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write);
while ((RecBytes = netstream.Read(RecData, 0, RecData.Length)) > 0)
{
Fs.Write(RecData, 0, RecBytes);
totalrecbytes += RecBytes;
}
Fs.Close();
}
netstream.Close();
client.Close();

}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//netstream.Close();
}
}
}

private void btnExit_Click(object sender, EventArgs e)
{
T.Abort();
this.Close();
}


}
}
龙宜坡 2009-09-08
  • 打赏
  • 举报
回复
发送端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace SendFiles
{
public partial class Form1 : Form
{
public string SendingFilePath = string.Empty;
private const int BufferSize = 1024;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Visible=true;
progressBar1.Minimum=1;
progressBar1.Value=1;
progressBar1.Step=1;

}

private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog Dlg = new OpenFileDialog();
Dlg.Filter = "All Files (*.*)|*.*";
Dlg.CheckFileExists = true;
Dlg.Title = "Choose a File";
Dlg.InitialDirectory = @"C:\";
if (Dlg.ShowDialog() == DialogResult.OK)
{
SendingFilePath = Dlg.FileName;

}
}

private void btnSend_Click(object sender, EventArgs e)
{
if (SendingFilePath != string.Empty)
{
SendTCP(SendingFilePath, txtIP.Text, Int32.Parse(txtPort.Text));
}
else
MessageBox.Show("Select a file","Warning");
}
public void SendTCP(string M, string IPA, Int32 PortN)
{
byte[] SendingBuffer = null;
TcpClient client = null;
lblStatus.Text = "";
NetworkStream netstream = null;
try
{
client = new TcpClient(IPA, PortN);
lblStatus.Text = "Connected to the Server...\n";
netstream = client.GetStream();
FileStream Fs = new FileStream(M, FileMode.Open, FileAccess.Read);
int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(BufferSize)));
progressBar1.Maximum = NoOfPackets;
int TotalLength = (int)Fs.Length, CurrentPacketLength, counter = 0;
for (int i = 0; i < NoOfPackets; i++)
{
if (TotalLength > BufferSize)
{
CurrentPacketLength = BufferSize;
TotalLength = TotalLength - CurrentPacketLength;
}
else
CurrentPacketLength = TotalLength;
SendingBuffer = new byte[CurrentPacketLength];
Fs.Read(SendingBuffer, 0, CurrentPacketLength);
netstream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
if (progressBar1.Value >= progressBar1.Maximum)
progressBar1.Value = progressBar1.Minimum;
progressBar1.PerformStep();
}

lblStatus.Text=lblStatus.Text+"Sent "+Fs.Length.ToString()+" bytes to the server";
Fs.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
netstream.Close();
client.Close();

}
}


}
}
萨拉嘿 2009-09-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jutuo2009cs 的回复:]
是文件发送。。接收。。不是消息。。。
[/Quote]
呃。。。不好意思啊
BATTLERxANGE 2009-09-06
  • 打赏
  • 举报
回复
http://download.csdn.net/source/1550301
有文件传输功能,很简单
zzxap 2009-09-06
  • 打赏
  • 举报
回复
socket
Harrison_2009 2009-09-06
  • 打赏
  • 举报
回复
是文件发送。。接收。。不是消息。。。
龙宜坡 2009-09-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oonukeoo 的回复:]
Server:
C# codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace Server
{class Program
{staticvoid Main(string[] args)
{
TcpListener Listener=new TcpListener(IPAddress.Any,8888);
Listener.Start();while (true)
{
TcpClient client= Listener.AcceptTcpClient();
NetworkStream stream= client.GetStream();string input="Hello";byte[] bytes=newbyte[64];
bytes= Encoding.BigEndianUnicode.GetBytes(input.ToCharArray());
stream.Write(bytes,0, bytes.Length);
stream.Close();

}
}
}
}
Client:
C# codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace Client
{class Program
{staticvoid Main(string[] args)
{
TcpClient client=new TcpClient("127.0.0.1",8888);
NetworkStream stream= client.GetStream();byte[] bytes=newbyte[64];
stream.Read(bytes,0, bytes.Length);string receive= Encoding.BigEndianUnicode.GetString(bytes);
Console.WriteLine(receive);
stream.Close();
}
}
}
[/Quote]

的确是最简单的代码!
jack15850798154 2009-09-06
  • 打赏
  • 举报
回复
学习一下!!!!
萨拉嘿 2009-09-06
  • 打赏
  • 举报
回复
Server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
class Program
{

static void Main(string[] args)
{
TcpListener Listener = new TcpListener(IPAddress.Any, 8888);
Listener.Start();

while (true)
{
TcpClient client = Listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
string input = "Hello";
byte[] bytes = new byte[64];
bytes = Encoding.BigEndianUnicode.GetBytes(input.ToCharArray());
stream.Write(bytes, 0, bytes.Length);
stream.Close();

}
}
}
}

Client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;


namespace Client
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient("127.0.0.1",8888);
NetworkStream stream = client.GetStream();

byte[] bytes = new byte[64];
stream.Read(bytes, 0, bytes.Length);
string receive = Encoding.BigEndianUnicode.GetString(bytes);
Console.WriteLine(receive);
stream.Close();
}
}
}
龙宜坡 2009-09-06
  • 打赏
  • 举报
回复
通信和传文件,道理上是一样的,发mail到goga21cn#126.com 给你完整vs2005 Demo!
vsbook 2009-09-06
  • 打赏
  • 举报
回复
学习一下,
peterb 2009-09-06
  • 打赏
  • 举报
回复

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧