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(6666);
}
public void ReceiveTCP(int portN)
{
TcpListener Listener = null;
try
{
Listener = new TcpListener(IPAddress.Any, 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();
}
}
}
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(6666);
}
public void ReceiveTCP(int portN)
{
TcpListener Listener = null;
try
{
Listener = new TcpListener(IPAddress.Any, 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();
}
}
}
解决方案 »
- c# winform richtextbox 插入文件选中后如何另存为原文件?
- 背景色为什么会跑到前景色的上层去?
- 关于窗口的打开
- C#控件名称可以用中文,各个事件过程名称前面都可以缀这个中文名称,这样程序在今后的长期运行过程中会不会出现问题
- 请各位指教,如何给一个动态生成的DataGrid增加事件??
- ListView子项重绘的问题——不知道该重载哪个函数或者说消息ID是多少,请教!:)
- c#查询问题,这怎么不显示了啊?
- listBox添加问题。
- SQL语句@%问题
- 问大家一个数据库读取的问题! 高分儿!(急)
- HttpWebRequest如何保持Keeplive呢?
- TCP/IP传输文件问题。[附伪码]
Status = "Connected to a client\n";
这句,以下的代码不执行,也就是后面的保存文件界面根本出不来,给看看需要修改什么么,查了好久了~~~
string message = "Accept the Incoming File ";
string caption = "Incoming Connection";
这一段代码执行成功的效果,这样算是通过socket建立了tcp连接了么 代码不执行的原因是因为连接每建立上么还是别的问题 比如权限,在线等明白人答疑
//判断连接
//执行一个阻塞调用来接受请求,可以用server.AcceptSocket()在这里。
TcpClient client = server.AcceptTcpClient();
//已经联通 data = null; //获取用于读取和写入一个流对象
NetworkStream stream = client.GetStream(); int i;
// 循环来接收所有由客户端发送的数据.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// 数据字节转换为ASCII字符串.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
//接收
//由客户端发送的数据.
data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); //发回一个响应
stream.Write(msg, 0, msg.Length);
//发送
}
client.Close();