Need help with UDP Handler

I need help with Building a UDP Server.

Page 1 of 1

0 Replies - 655 Views - Last Post: 16 March 2009 - 03:43 AM Rate Topic: -----

#1 RobinV  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 21-May 08

Need help with UDP Handler

Post icon  Posted 16 March 2009 - 03:43 AM

I am building a MultiThreaded TCP / UDP Server.
I did the TCP part in like 3 hours.
But the UDP is making me crazy.
I made this Snipper after reading some tutorials.
		static void UDPHandler()
		{
			try
			{
				IPEndPoint myIPEndPoint = new IPEndPoint(IPAddress.Any, Server.iPort);
				EndPoint myEndPoint = (EndPoint)myIPEndPoint;
				byte[] bBuffer = new byte[1024];
				List<EndPoint> lstEP = new List<EndPoint>();
				UDPFlags objFlag = new UDPFlags();
				for (;; )
				{
					Socket mySocket = new Socket(AddressFamily.Unspecified, SocketType.Dgram, ProtocolType.Udp);
					mySocket.Bind(myIPEndPoint);
					mySocket.ReceiveFrom(bBuffer, SocketFlags.None, ref myEndPoint);
					 objFlag.bBuffer = bBuffer;
					 objFlag.EndPoint = myEndPoint;
					 bool bSetDiffer = true;
					 mySocket.Close();
					 #region EndPoint Handling
					 foreach (EndPoint ep in lstEP)
					 {
						if (ep == objFlag.EndPoint) { bSetDiffer = false; break; }
					 }
					 if (bSetDiffer)
					 {
						 lstEP.Add(objFlag.EndPoint);
						 ThreadPool.QueueUserWorkItem(new WaitCallback(UDPReader), objFlag);
					 }
					 bSetDiffer = false;
					 #endregion
					 Thread.Sleep(1);
				}
			}
			catch(Exception ex)
			{
				#if DEBUG
					Debugger.Break();
				#endif
				EventLog.WriteEntry("Server", "UDPHandler Crashed\n\n" + ex.Message.ToString(), EventLogEntryType.Error);
			}
		}

		static void UDPReader(object arg)
		{
			UDPFlags objFlag = (UDPFlags)arg;
			//IPEndPoint myIPEndPoint = new IPEndPoint(objFlag.EndPoint.AddressFamily, Server.iPort);
			EndPoint myEndPoint = objFlag.EndPoint;
			ASCIIEncoding byteExtractor = new ASCIIEncoding();
			Socket mySocket = new Socket(objFlag.EndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
			byte[] bBuffer = new byte[1024];
			bBuffer = objFlag.bBuffer;
			mySocket.Bind(myEndPoint);
			for(;;)
			{
				mySocket.ReceiveFrom(bBuffer, SocketFlags.None, ref myEndPoint);
				WriteInfo(byteExtractor.GetString(bBuffer) + "::" + myEndPoint.AddressFamily.ToString());
				mySocket.Send(bBuffer);
				switch (byteExtractor.GetString(bBuffer))
				{
					case "exit":
						mySocket.Close();
						break;
					default:
						break;
				}
			}
		}


And I see what I do wrong, I close the connection before I parse it..
But I need to find a way to make this work.
Who can help me with this? I use netcat to test the thingy.


My TCP Handler looks like this:

	   static void TCPHandler(object objArguments)
		{
			try
			{
				//Server.iTCP_Port;
				//int iTCP_Port = (int)objArguments;
				TcpListener objTCP = new TcpListener(IPAddress.Parse(szIP), Server.iPort);
				WriteInfo("Listerning on Port: " + Convert.ToString(Server.iPort));
				objTCP.Start();
				for (;; )
				{
					TcpClient ObjConnectedClient = objTCP.AcceptTcpClient();
					NetworkStream objStream = ObjConnectedClient.GetStream();
					
					ThreadPool.QueueUserWorkItem(new WaitCallback(StreamHandler), objStream);
					Thread.Sleep(1);
				}
			}
			catch
			{
				#if DEBUG
					Debugger.Break();
				#endif
				EventLog.WriteEntry("Server", "TCPHandler Crashed", EventLogEntryType.Error);
			}
		}

		static void StreamHandler(object objArg)
		{
			try
			{
				NetworkStream objNetStream = (NetworkStream)objArg;
				Byte[] bBuffer = new Byte[1024];
				int iX;

				string szData;
				while ((iX = objNetStream.Read(bBuffer, 0, bBuffer.Length)) != 0)
				{
					szData = null;
					szData = Encoding.ASCII.GetString(bBuffer, 0, iX);

					String[] szNetCommand = szData.Split(':');
					switch (szNetCommand[0])
					{
						case "SendADinfo":
							ASCIIEncoding byteExtractor = new ASCIIEncoding();
							byte[] sendBuffer = byteExtractor.GetBytes(szNetCommand[1]);
							objNetStream.Write(sendBuffer, 0, sendBuffer.Length);
							break;
						case "Exit":
							objNetStream.Close();
							break;
						default:
							break;
					}
				}
			}
			catch (IOException ex)
			{
				#if DEBUG
					Debugger.Break();
				#endif
				WriteError(ex.Message.ToString());
			}
		}




I kinda want to keepusing the same style.

So, any tips or advice?
Thanks
~Robin

ps. This is a Service application. So I can't do Console.writeline or anything like that the WriteError and Write info are functiont to write to the event log I wrote ;)


Quick Update:
I did add this now:
objFlag.sSocket = cpySocket(mySocket);


and then use the Socket again. But It looks like it doesn't work anymore..


here my my cpySocket Function
		private static Socket cpySocket(Socket cloneMe)
		{
			FieldInfo[] fis = cloneMe.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
			Socket newSocket = new Socket(AddressFamily.Unspecified, SocketType.Dgram, ProtocolType.Udp); 
			foreach (FieldInfo fi in fis)
			{
				fi.SetValue(newSocket, fi.GetValue(cloneMe));
			}
			return newSocket;
		}



Basicly thats a reflection Cloneing technique..
And in the UDPReader I did add this:
			UDPFlags objFlag = (UDPFlags)arg;
			ASCIIEncoding byteExtractor = new ASCIIEncoding();
			byte[] bBuffer = new byte[1024];
			bBuffer = objFlag.bBuffer;
			Socket mySocket = objFlag.sSocket;  <<---



Any advice?
Thanks

Another EDIT.
I was thinking of maybe putting all the sockets in a List.
and then with a Separate Thread to do something for each Item in that list.
Though I think that will product really dragging slow code. I'd like to hear a opinion about this?

This post has been edited by RobinV: 16 March 2009 - 07:39 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1