Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2007

Tip: Looking for answers? Try searching our database.

UDP Server (what's wrong?)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick - 11 Jan 2007 05:16 GMT
I have written a simple UDP server. It uses a single socket and sends
incoming data to random ports on the same machine. After second call of
Receive () I got SocketException "An existing connection was forcibly
closed by the remote host". How it can be? I have UDP server and there
are no connections!

Also I noticed if I comment out SendTo or change SendTo IP to some real
IP then the server works perfectly. What can be wrong with this?

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace UdpTest
{
   class Program
   {
       private static string serverIP = "192.168.64.66";
       private static int serverPort = 5000;

       static void Main(string[] args)
       {
           Thread thread = new Thread(new ThreadStart(ServerThread));
           thread.Start();

           using (UdpClient client = new UdpClient())
           {
               client.Connect(serverIP, 5000);
               while (true)
               {
                   client.Send(new byte[100], 100);
                   Thread.Sleep(1000);
               }
           }
       }

       private static void ServerThread()
       {
           using (Socket socket = new
Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
           {
               socket.Bind(new IPEndPoint(0, serverPort));

               byte[] buffer = new byte[65536];
               while (true)
               {
                   int read = socket.Receive(buffer);
                   if (read > 0)
                   {
                       int sent = 0;
                       for (int i = 0; i < 10; i++)
                       {
                           int port = new Random().Next(10000, 11000);
                           EndPoint point = new
IPEndPoint(IPAddress.Parse(serverIP), port);
                           sent += socket.SendTo(buffer, 0, read,
SocketFlags.None, point);
                       }
                       Console.WriteLine("Received {0} bytes, sent {1}
bytes", read, sent);
                   }
               }
           }
       }
   }
}
Nick - 11 Jan 2007 05:19 GMT
Here's the socket log

System.Net.Sockets Verbose: 0 : [3244]
Socket#58225482::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [4012]
Socket#21950498::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [3244] Exiting
Socket#58225482::Socket()
System.Net.Sockets Verbose: 0 : [3244]
DNS::GetHostAddresses(192.168.64.66)
System.Net.Sockets Verbose: 0 : [3244] Exiting DNS::GetHostAddresses()
    -> IPAddress[]#54267293
System.Net.Sockets Verbose: 0 : [3244]
Socket#58225482::Connect(66:5000#1111538504)
System.Net.Sockets Verbose: 0 : [4012] Exiting
Socket#21950498::Socket()
System.Net.Sockets Verbose: 0 : [4012]
Socket#21950498::Bind(0:5000#5000)
System.Net.Sockets Verbose: 0 : [4012] Exiting Socket#21950498::Bind()
System.Net.Sockets Verbose: 0 : [4012] Socket#21950498::Receive()
System.Net.Sockets Verbose: 0 : [3244] Exiting
Socket#58225482::Connect()
System.Net.Sockets Verbose: 0 : [3244] Socket#58225482::Send()
System.Net.Sockets Verbose: 0 : [4012] Data from
Socket#21950498::Receive
System.Net.Sockets Verbose: 0 : [4012] 00000000 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000010 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000020 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000030 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000040 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000050 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000060 : 00 00 00 00
                         : ....
System.Net.Sockets Verbose: 0 : [4012] Exiting
Socket#21950498::Receive()     -> 100#100
System.Net.Sockets Verbose: 0 : [4012] Socket#21950498::SendTo()
System.Net.Sockets Verbose: 0 : [4012] Data from
Socket#21950498::SendTo
System.Net.Sockets Verbose: 0 : [4012] 00000000 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000010 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000020 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000030 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000040 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000050 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [4012] 00000060 : 00 00 00 00
                         : ....
System.Net.Sockets Verbose: 0 : [4012] Exiting
Socket#21950498::SendTo()     -> 100#100
System.Net.Sockets Verbose: 0 : [4012] Socket#21950498::Receive()
System.Net.Sockets Error: 0 : [4012] Exception in the
Socket#21950498::Receive - An existing connection was forcibly closed
by the remote host
System.Net.Sockets Verbose: 0 : [4012] Exiting
Socket#21950498::Receive()     -> 0#0
System.Net.Sockets Verbose: 0 : [3244] Data from Socket#58225482::Send
System.Net.Sockets Verbose: 0 : [3244] 00000000 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000010 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000020 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000030 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000040 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000050 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000060 : 00 00 00 00
                         : ....
System.Net.Sockets Verbose: 0 : [3244] Exiting Socket#58225482::Send()
    -> 100#100
System.Net.Sockets Verbose: 0 : [3244] Socket#58225482::Send()
System.Net.Sockets Verbose: 0 : [3244] Data from Socket#58225482::Send
System.Net.Sockets Verbose: 0 : [3244] 00000000 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000010 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000020 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000030 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000040 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000050 : 00 00 00 00 00 00 00
00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [3244] 00000060 : 00 00 00 00
                         : ....
System.Net.Sockets Verbose: 0 : [3244] Exiting Socket#58225482::Send()
    -> 100#100
System.Net.Sockets Verbose: 0 : [4012] Socket#21950498::Dispose()
Nick - 11 Jan 2007 07:53 GMT
Resolved
http://blog.devstone.com/aaron/archive/2005/02/20.aspx

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.