求c# 采用应答式发送文件的例子
对应用应答式的方式发送文件的方法我不知道该怎么写,也没有搜到资料和例子,希望各位提供一些资料尤其是范例。邮箱:anada1985_07_27@yahoo.com.cn
我会加分的谢谢 --------------------编程问答--------------------
给你个移动USSD服务的示意图,典型的对等通讯
--------------------编程问答-------------------- 你的意思是在response里附带 文件的byte报文? 不走常规FTP模式? --------------------编程问答--------------------
我的意思是发送文件(尤其是分包发的大文件)是采用应答式的,收到一个\一个包才发下一个。我是初学的,不知道这样该怎么写 --------------------编程问答--------------------
我是初学的,这个看不懂。 --------------------编程问答--------------------
就是发起方的所有请求,接收方都必须应答,发起方收到接收方的应答,则认为指令成功.
底层的TCP协议中数据包的传输其实也是应答机制.
举个常见例子
用http上传到http://abc.edf.com
客户上传了一个id为123的文件包
服务器response必须返回123标识给客户,作为收到的依据.
--------------------编程问答--------------------
太简单的问题实在不知道怎么回答你。
一个应用服务可能有上百个命令,所以开发服务命令之前肯定需要首先制定(应用层)通信协议。
网上大多数通信“范例”都只是个无用的小程序,勉强完成1、2个命令,而没有协议设计的影子。 --------------------编程问答-------------------- 作为一种“范例”,你可以看看xmpp,既rfc 3921。它是长连接的。而我过去设计的服务基本上都是端连接的(虽然同时连接客户端有上千万)。基本上不管是长短连接,协议设计都类似(短连接协议稍微复杂一点点,但是服务器更稳定可靠)。 --------------------编程问答--------------------
我是用的SOCKET的TCP协议,想做到:接收到一个,返回一个信息,这样再发下条。因为我原来的发送,小文件没大有问题,可是大文件的话,发的太快,接的比较慢,我就收不到文件了。所以想改进。可是我真的不知道该怎么做到这样。
贴出我代码,多多批评
//服务器
//读写基础类
public class StateObject{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
public void SetBuffer()
{
buffer = null;
buffer = new byte[BufferSize];
}
}
//
private void StartListening()
{
IPAddress myipAddress = Dns.GetHostAddresses(Dns.GetHostName())[2];
IPEndPoint localEndPoint = new IPEndPoint(myipAddress, listenport);
_svrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_svrSocket.Bind(localEndPoint);
_svrSocket.Listen(100);
while (true)
{
try
{
Done.Reset();
_svrSocket.BeginAccept(new AsyncCallback(AcceptCallback), _svrSocket);
Done.WaitOne();
}
catch { }
}
}
//
private void AcceptCallback(IAsyncResult ai)
{
try
{
Done.Set();
Socket serverSock = (Socket)ai.AsyncState;
Socket clientSocket = serverSock.EndAccept(ai);
state = new StateObject();
state.workSocket = clientSocket;
clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
catch { }
}
//发消息
private void Send(Socket client, string data)
{
byte[] bytedata = Encoding.UTF8.GetBytes(data);
client.BeginSend(bytedata, 0, bytedata.Length, 0, new AsyncCallback(SendCallback2), client);
}
private void SendCallback2(IAsyncResult ai)
{
try
{
Socket clientSocket = (Socket)ai.AsyncState;
int byteSend = clientSocket.EndSend(ai);
}
catch { }
}
private void ReadCallback(IAsyncResult ai)
{
StateObject state = (StateObject)ai.AsyncState;
Socket clientSocket = state.workSocket;
try
{
int bytesRead = clientSocket.EndReceive(ai);
string strF = Encoding.UTF8.GetString(state.buffer, 0, bytesRead);
string strIPP = clientSocket.RemoteEndPoint.ToString();
string[] strip = strIPP.Split(':');
if (bytesRead == 0)
{
return;
}
else
{
TreateRecv(strF, strip[0].ToString(), clientSocket, state);//根据客户端发来的消息判断其是否在线
}
}
catch { }
}
客户端代码
--------------------编程问答-------------------- 这个必须mark --------------------编程问答-------------------- 不要沉啊 --------------------编程问答-------------------- 希望学习一下 --------------------编程问答-------------------- 重新改了一下:client每收到一个包,就用一个变量记录收到的是第几个包,并把这个值传回server。但只有前几个回合回这样正常的进行。后面就不这样做了。真是郁闷啊,到底为什么? --------------------编程问答-------------------- 唉,没人了吗 --------------------编程问答-------------------- MARK --------------------编程问答-------------------- 学习~! --------------------编程问答-------------------- 采用了另一种写法,终于ok了。
private void StartConn()
{
string serveraddress = "";
StreamReader sr = new StreamReader(Application.StartupPath + @"\clientip.ini", Encoding.GetEncoding("utf-8"));
string read = null;
while ((read = sr.ReadLine()) != null)
serveraddress = read;
sr.Close();
IPAddress ipAddress = IPAddress.Parse(serveraddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 5555);
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
while (true)
{
try
{
clientSocket.BeginConnect(remoteEP, new AsyncCallback(ConnCallback), clientSocket);
Done.WaitOne();
}
catch { }
}
}
private void ConnCallback(IAsyncResult ai)
{
Socket clientSock = (Socket)ai.AsyncState;
try
{
Done.Set();
clientSock.EndConnect(ai);
state = new StateObject();
state.workSocket = clientSock;
byte[] byteData = Encoding.ASCII.GetBytes("<CONN>");
clientSock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), clientSock);
clientSock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
catch { }
}
private void ReadCallback(IAsyncResult ai)
{
try
{
StateObject stateclient = (StateObject)ai.AsyncState;
Socket client = stateclient.workSocket;
int byteread = client.EndReceive(ai);
if (byteread > 0)
{
string ss = System.Text.Encoding.UTF8.GetString(stateclient.buffer);
ss = ss.Substring(0, 6);
TreateRecv(ss, stateclient, byteread, client);
stateclient.SetBuffer();
client.BeginReceive(stateclient.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), stateclient);
}
else
{
intPacknum = 0;
}
}
catch { }
}
private void TreateRecv(string strf, StateObject stateclient, int byteread, Socket client)
{
switch (strf)
{
case "<PEOF>":
HeadMsg = System.Text.Encoding.UTF8.GetString(stateclient.buffer, 0, byteread);
string[] sss = HeadMsg.Split((char)191);
FileName = sss[1].ToString();
intP = Convert.ToInt32(sss[3].ToString());
break;
case "<MEOF>":
HeadMsg = System.Text.Encoding.UTF8.GetString(stateclient.buffer, 0, byteread);
SetText(HeadMsg);
break;
case "<WEOF>":
HeadMsg = System.Text.Encoding.UTF8.GetString(stateclient.buffer);
SetText(HeadMsg);
break;
default:
ms.Write(stateclient.buffer, 0, byteread);
intPacknum += 1;
break;
}
if (strf == "<Over>")
{
SetFile();//保存
ms.Dispose();
ms = new MemoryStream();
}
}
//保存文件
private void SetFile()
{
string[] ss = HeadMsg.Split((char)191);
int fileLen = 0;
fileLen = (ss.Length == 5) ? Convert.ToInt32(ss[2].ToString()) : fileLen;
FileStream fs = new FileStream(System.Windows.Forms.Application.StartupPath + "\\" + FileName, FileMode.Create);
byte[] byteD = ms.GetBuffer();
fs.Write(byteD, 0, fileLen);
fs.Close();
//MessageBox.Show("保存成功");
}
补充:.NET技术 , C#