using System;
using System.Collections.Generic;
using System.Text;using System.IO;
using System.Net.Sockets;namespace Warehouse.IssueFactory.Socket
{
public delegate void StringLineReceive(CSocketConnection sender,string e); public class CSocketConnection : Object
{
private const int BUFFERSIZE = 256;
private byte[] ReadBuffer = new byte[BUFFERSIZE];
private TcpClient client; public string ClientName;
public event StringLineReceive LineReceived; public CSocketConnection(TcpClient client)
{
this.client = client;
this.client.GetStream().BeginRead(ReadBuffer, 0, BUFFERSIZE, new AsyncCallback(StreamReceiver), null);
} private void StreamReceiver(IAsyncResult ar)
{
int ByteRead = 0;
string StringInfo = string.Empty; try
{
lock (client.GetStream())
{
ByteRead = this.client.GetStream().EndRead(ar);
} StringInfo = Encoding.ASCII.GetString(this.ReadBuffer, 0, ByteRead - 1);
LineReceived(this, StringInfo); lock (client.GetStream())
{
this.client.GetStream().BeginRead(ReadBuffer, 0, BUFFERSIZE, new AsyncCallback(StreamReceiver), null);
}
}
catch (Exception ex)
{
string s = ex.Message;
}
} public void SendString(string e)
{
lock (client.GetStream())
{
StreamWriter writer = new StreamWriter(client.GetStream());
writer.Write(e + "\r\n");
writer.Flush();
}
}
}
}
using System.Collections.Generic;
using System.Text;using System.IO;
using System.Net.Sockets;namespace Warehouse.IssueFactory.Socket
{
public delegate void StringLineReceive(CSocketConnection sender,string e); public class CSocketConnection : Object
{
private const int BUFFERSIZE = 256;
private byte[] ReadBuffer = new byte[BUFFERSIZE];
private TcpClient client; public string ClientName;
public event StringLineReceive LineReceived; public CSocketConnection(TcpClient client)
{
this.client = client;
this.client.GetStream().BeginRead(ReadBuffer, 0, BUFFERSIZE, new AsyncCallback(StreamReceiver), null);
} private void StreamReceiver(IAsyncResult ar)
{
int ByteRead = 0;
string StringInfo = string.Empty; try
{
lock (client.GetStream())
{
ByteRead = this.client.GetStream().EndRead(ar);
} StringInfo = Encoding.ASCII.GetString(this.ReadBuffer, 0, ByteRead - 1);
LineReceived(this, StringInfo); lock (client.GetStream())
{
this.client.GetStream().BeginRead(ReadBuffer, 0, BUFFERSIZE, new AsyncCallback(StreamReceiver), null);
}
}
catch (Exception ex)
{
string s = ex.Message;
}
} public void SendString(string e)
{
lock (client.GetStream())
{
StreamWriter writer = new StreamWriter(client.GetStream());
writer.Write(e + "\r\n");
writer.Flush();
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货