Binary Udp Server: receive binary data from Udp client : UdpClient « Network « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Network » UdpClient 
33.13.3.Binary Udp Server: receive binary data from Udp client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
      UdpClient newsock = new UdpClient(ipep);

      Console.WriteLine("Waiting for a client...");

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

      byte[] data1 = newsock.Receive(ref sender);
      int test1 = BitConverter.ToInt32(data1, 0);
      Console.WriteLine("test1 = {0}", test1);

      byte[] data2 = newsock.Receive(ref sender);
      double test2 = BitConverter.ToDouble(data2, 0);
      Console.WriteLine("test2 = {0}", test2);

      byte[] data3 = newsock.Receive(ref sender);
      int test3 = BitConverter.ToInt32(data3, 0);      
      Console.WriteLine("test3 = {0}", test3);

      byte[] data4 = newsock.Receive(ref sender);
      bool test4 = BitConverter.ToBoolean(data4, 0);
      Console.WriteLine("test4 = {0}", test4.ToString());

      byte[] data5 = newsock.Receive(ref sender);
      string test5 = Encoding.ASCII.GetString(data5);
      Console.WriteLine("test5 = {0}", test5);

      newsock.Close();
   }
}
33.13.UdpClient
33.13.1.Use UdpClient
33.13.2.Binary UdpClient: send binary data to Udp server
33.13.3.Binary Udp Server: receive binary data from Udp client
33.13.4.UdpClient: Send
33.13.5.UdpClient: receive for multicast group
33.13.6.Udp connection test
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.