Home All Groups Group Topic Archive Search About

UDP Server (what's wrong?)

Author
11 Jan 2007 5:16 AM
Nick
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);
                    }
                }
            }
        }
    }
}

Author
11 Jan 2007 5:19 AM
Nick
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()
Author
11 Jan 2007 7:53 AM
Nick
Resolved
http://blog.devstone.com/aaron/archive/2005/02/20.aspx

AddThis Social Bookmark Button