public void ReceMsgCallBack(IAsyncResult ar)
{
ClientState cs = ar.AsyncState as ClientState;
Socket thisSk = cs.sk; try
{
byte[] fnl = cs.state;
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
ms.Write(fnl, 0, fnl.Length);
ms.Position = 0;
ArrayList aal = (ArrayList)bf.Deserialize(ms);
int length = Convert.ToInt32(aal[0]);
string ffname = aal[1] as string;
byte[] filelag = new byte[length];
int bytesRead;
int totalBytes = 0;
fs = new FileStream(@"C:\Users\Administrator\" + ffname, FileMode.OpenOrCreate, FileAccess.Write);
do
{
bytesRead = sk.Receive(filelag);
fs.Write(filelag, 0, bytesRead);
totalBytes += bytesRead; }
while (totalBytes<filelag.Length);
fs.Close();
ClientState thiscs = new ClientState();
thiscs.sk = thisSk;
thisSk.BeginReceive(thiscs.state, 0, thiscs.state.Length, SocketFlags.None, new AsyncCallback(ReceMsgCallBack), thiscs);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的代码 我在客户端发送一个图片文件 将其转换为二进制后发送 然后在服务端接受使用的就是上面这段
但是在发送完一次还可以 继续发送就报错了 不知道是什么原因 还是我代码写错了 求高手赐教
{
ClientState cs = ar.AsyncState as ClientState;
Socket thisSk = cs.sk; try
{
byte[] fnl = cs.state;
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
ms.Write(fnl, 0, fnl.Length);
ms.Position = 0;
ArrayList aal = (ArrayList)bf.Deserialize(ms);
int length = Convert.ToInt32(aal[0]);
string ffname = aal[1] as string;
byte[] filelag = new byte[length];
int bytesRead;
int totalBytes = 0;
fs = new FileStream(@"C:\Users\Administrator\" + ffname, FileMode.OpenOrCreate, FileAccess.Write);
do
{
bytesRead = sk.Receive(filelag);
fs.Write(filelag, 0, bytesRead);
totalBytes += bytesRead; }
while (totalBytes<filelag.Length);
fs.Close();
ClientState thiscs = new ClientState();
thiscs.sk = thisSk;
thisSk.BeginReceive(thiscs.state, 0, thiscs.state.Length, SocketFlags.None, new AsyncCallback(ReceMsgCallBack), thiscs);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的代码 我在客户端发送一个图片文件 将其转换为二进制后发送 然后在服务端接受使用的就是上面这段
但是在发送完一次还可以 继续发送就报错了 不知道是什么原因 还是我代码写错了 求高手赐教
解决方案 »
- datagridview1显示SQL查询出来的100行2列数据,一列保存为一个数组。数组经过简单计算重新保存到SQL,,怎么处理?
- C# 窗体如何在ctrl+tab 中隐藏
- 求一个窗体控件的应用思路
- C# winform 程序如何自动刷新
- ASP.NET入门
- 怎么样获取窗体里面所有控件的属性和控件类型?(c# winform)
- 大家看看这个控件用WPF是怎么实现的?
- 请教用C#操作excel的基本命令
- 对注册表项 HKEY_CLASSES_ROOT\logistics.BusinessRules.Wharehouse 的访问被拒绝。
- DataGrid,DataView的两个问题.
- WCF 传递dictionary<strin,object> 当object为datatable时出错
- vs2005 选中datagridview中的一列数据进行编辑
public static void Listen_Callback(IAsyncResult ar){
allDone.Set();
Socket s = (Socket) ar.AsyncState;
Socket s2 = s.EndAccept(ar);
StateObject so2 = new StateObject();
so2.workSocket = s2;
s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0,
new AsyncCallback(Async_Send_Receive.Read_Callback), so2);
} public static void Read_Callback(IAsyncResult ar){
StateObject so = (StateObject) ar.AsyncState;
Socket s = so.workSocket;
int read = s.EndReceive(ar);
if (read > 0) {
so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,
new AsyncCallback(Async_Send_Receive.Read_Callback), so);
}
else{
if (so.sb.Length > 1) {
//All of the data has been read, so displays it to the console
string strContent;
strContent = so.sb.ToString();
Console.WriteLine(String.Format("Read {0} byte from socket" +
"data = {1} ", strContent.Length, strContent));
}
s.Close();
}
}