日期:2014-05-17 浏览次数:20541 次
private void BeginListen()
{
serverIP = IPAddress.Parse(tbxIP.Text);
serverFullAddr = new IPEndPoint(serverIP, int.Parse(tbxPort.Text));//设置IP,端口
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//指定本地主机地址和端口号
sock.Bind(serverFullAddr);
lbxMessage.Invoke(new SetTextCallback(SetText), "启动成功 时间:"+DateTime.Now, 1);
byte[] message = new byte[1024];
string mess = "";
while (true)
{
try
{
sock.Listen(5);//设置监听频率
Socket newSocket = sock.Accept();//阻塞方式
int bytes= newSocket.Receive(message);//接收数据
mess = Encoding.Default.GetString(message, 0, bytes);//对接收字节编码(S与C 两端编码格式必须一致不然中文乱码)(当接收的字节大于1024的时候 这应该是循环接收,测试就没有那样写了)
//do
//{
// bytes = newSocket.Receive(message, message.Length, 0);
// mess = mess + Encoding.ASCII.GetString(message, 0, bytes);