.net3.5以上的socket编程clinet的demo有吗
之前freamwork1.1的我会写, 就是在connect之后的connect回执里面注册上回调方法,并且在后续每次receive之后都把回调方法补上就可以完成整个过程的延续.3.5以后的wp8版好像取消了beginConnect和 beginReceive. 不再支持1.1的sdk. 由于要移植到wp8平台,我不得不替换写法.
我自己写了个,但是除了connect之后的connect回执能够被回调之外, 后续的receive就断了, 我不知道应该在哪去注册receive的回调,也不知道在哪设置下一次读取数据的length.自己尝试下面的写法发现也不行. 请教这方面高手.
代码如下:
using System;--------------------编程问答-------------------- 这个..................... --------------------编程问答-------------------- http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202874(v=vs.105).aspx
using System.Net.Sockets;
using System.Threading;
using System.Net;
using Net.Process;
using UnityEngine;
namespace Net
{
public class SocketTransferWP8
{
private const int TIMEOUT_MILLISECONDS = 15000;
//private ManualResetEvent clientDone = new ManualResetEvent (false);
private ProcessRecieve proMsg;
private SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
private Socket socket;
public SocketTransferWP8 (ProcessRecieve proMsg)
{
this.proMsg = proMsg;
}
public void Connect (String hostName, int serverPort)
{
Debug.Log("SocketAsyncOperation.Connect:" + SocketAsyncOperation.Connect);
Debug.Log("SocketAsyncOperation.Receive:" + SocketAsyncOperation.Receive);
Debug.Log("SocketAsyncOperation.Send:" + SocketAsyncOperation.Send);
//DnsEndPoint hostEntry = new DnsEndPoint (hostName, serverPort);
IPEndPoint hostEntry = new IPEndPoint (IPAddress.Parse(hostName), serverPort);
socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketEventArg = new SocketAsyncEventArgs ();
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = socket;
socket.ConnectAsync (socketEventArg);
//clientDone.WaitOne ();
}
public void Send (byte[] data)
{
SocketAsyncEventArgs saea = new SocketAsyncEventArgs();
saea.RemoteEndPoint = this.socket.RemoteEndPoint;
saea.UserToken = null;
saea.SetBuffer(data, 0, data.Length);
//this.clientDone.Reset();
this.socket.SendAsync(saea);
//this.clientDone.WaitOne(TIMEOUT_MILLISECONDS);
}
private void SocketEventArg_Completed (object sender, SocketAsyncEventArgs e)
{
Debug.Log("** SocketEventArg_Completed:e.LastOperation=" + e.LastOperation);
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect (e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive (e);
break;
case SocketAsyncOperation.Send:
ProcessSend (e);
break;
default:
throw new Exception ("Invalid operation completed");
}
}
private void ProcessConnect (SocketAsyncEventArgs e)
{
Debug.Log("ProcessConnect-->into 1");
if (e.SocketError == SocketError.Success)
{
// Successfully connected to the server
// Socket sock = e.UserToken as Socket;
// bool willRaiseEvent = sock.SendAsync (e);
// if (!willRaiseEvent)
// {
// ProcessSend (e);
// }
// register a continue
//SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (SocketEventArg_Completed);
}
else
{
throw new SocketException ((int)e.SocketError);
}
}
// Called when a ReceiveAsync operation completes
// </summary>
private void ProcessReceive (SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
// Received data from server
// Data has now been sent and received from the server.
// Disconnect from the server
// Socket sock = e.UserToken as Socket;
Debug.Log("received a message");
this.proMsg.MessageEvent(e.Buffer);
//clientDone.Set ();
//SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (SocketEventArg_Completed);
}
else
{
throw new SocketException ((int)e.SocketError);
}
}
// Called when a SendAsync operation completes
private void ProcessSend (SocketAsyncEventArgs e)
{
Debug.Log("ProcessSend-->");
if (e.SocketError == SocketError.Success)
{
// Sent "Hello World" to the server successfully
//Read data sent from the server
// Socket sock = e.UserToken as Socket;
// bool willRaiseEvent = sock.ReceiveAsync (e);
//
// if (!willRaiseEvent)
// {
// ProcessReceive (e);
// Debug.Log("sent a message and ProcessSend");
// }
// else
// {
// Debug.Log("sent a message");
// }
}
else
{
throw new SocketException ((int)e.SocketError);
}
}
}
}
看这个以及下属的3篇文章。 --------------------编程问答--------------------
好, 谢谢, 我先看眼
补充:.NET技术 , C#