.net3.5 socket programming clinet demo do

Before freamwork1.1 I will write, is in the connect after the connect receipt which registered callback method, and after each subsequent receive to continue the callback method to fill the whole procedure can be completed.

3.5 version of the wp8 like beginConnect and beginReceive. no longer supports cancelled 1.1 sdk. due to the transplanted to the wp8 platform, I had to replace the wording.

I wrote, but in addition to the connect after the connect receipt can be the callback, the subsequent receive is broken, I do not know where to go to register the receive callback, also do not know to read data set in which the next length. try the following notation is not to ask the master..
The following code:
using System;
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);
			}
		}
		
	}
}


Started by Winston at November 16, 2016 - 11:15 PM

This.....................

Posted by Winston at November 20, 2016 - 11:48 PM

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202874(v=vs.105).aspx
Look at this and under the 3 article.

Posted by Edwiin at November 24, 2016 - 12:36 AM

Well, thank you, I look at the eye

Posted by Winston at December 05, 2016 - 1:12 AM