Binary Udp Server : Udp Server « Network « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Network » Udp ServerScreenshots 
Binary Udp Server

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class BinaryUdpSrvr
{
   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);

      data = newsock.Receive(ref sender);

      Console.WriteLine("Message received from {0}:", sender.ToString());
      Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));

      string welcome = "Welcome to my test server";
      data = Encoding.ASCII.GetBytes(welcome);
      newsock.Send(data, data.Length, sender);

      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();
   }
}

           
       
Related examples in the same category
1.Test Udp Server
2.Simple Udp Server
3.Udp Server Sample
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.