I've tested your program.. nice work!
I've coded a similiar program in the past.. but not for remote users.
btw: i've coded a little fix for ipv6.. if you use localhost as address with windows 7 you can't connect.
Network.cs
It's very interesting. I've never worked with WPF before, but it looks great. Thanks for this suggestion ^^
I've coded a similiar program in the past.. but not for remote users.
btw: i've coded a little fix for ipv6.. if you use localhost as address with windows 7 you can't connect.
Network.cs
Code:
/// <summary>
///
/// </summary>
/// <param name="ipAddressOrHostName"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool Connect(string ipAddressOrHostName, int port)
{
if (Connected)
{
Disconnect();
}
BytesReceived = 0;
BytesSent = 0;
bool success = true;
try
{
if (!IPAddress.TryParse(ipAddressOrHostName, out _serverAddress))
{
IPAddress[] ipAddresses = Dns.GetHostAddresses(ipAddressOrHostName);
if (ipAddresses.Length == 0)
{
throw new NetworkException("Host address was unreachable or invalid, unable to obtain an ip address.");
}
else
{
ServerAddress = ipAddresses[0];
if (ServerAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
string ip = string.Empty;
double doubleValue;
try
{
string val = ServerAddress.ToString();
val = val == "::1" ? "127.0.0.1" : val; // localhost in ipv6
if (val != null)
{
if (Double.TryParse(val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.CurrentCulture, out doubleValue))
{
Console.WriteLine(val);
ip = val;
}
}
}
catch
{
ip = "0.0.0.0";
}
IPAddress.TryParse(ip, out _serverAddress);
}
}
}
_log.Debug("Connecting...");
_serverEndPoint = new IPEndPoint(_serverAddress, port);
_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverSocket.Connect(_serverEndPoint);
if (_serverSocket.Connected)
{
_log.Debug("Connected.");
SocketState state = new SocketState(_serverSocket, ushort.MaxValue);
_serverSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, OnReceive, state);
}
}
catch (Exception e)
{
success = false;
_log.Fatal(e);
//throw;
}
Connected = success;
return success;
}
It's very interesting. I've never worked with WPF before, but it looks great. Thanks for this suggestion ^^