Всем привет! вот решил написать чатик с помощью протокола UDP
не я конечно писал а в инете искал коды подходящие ибо сам не знал с чего начать, хочу сервер поставить на ай пи, вставляю свой ай пи, и ошибка в строке multicastaddress = IPAddress.Parse("мой айпишник");
помогите разобраться пожалуйста, заранее спасибо!
сама форма
Код:
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
namespace chat
{
public partial class Form1 : Form
{
public static string Ip;
public Form1()
{
InitializeComponent();
}
private void textBoxSendMessage_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
UdpChat chat = new UdpChat();
Thread ListenThread = new Thread(new ThreadStart(chat.Listen));
ListenThread.Start();
string data = textBoxSendMessage.Text;
chat.SendMessage(data);
}
}
}
класс UdpChat
Код:
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace chat
{
class UdpChat
{
private UdpClient udpclient;
private IPAddress multicastaddress;
private IPEndPoint remoteep;
public UdpChat()
{
multicastaddress = IPAddress.Parse("239.0.0.222");
udpclient = new UdpClient();
udpclient.JoinMulticastGroup(multicastaddress);
remoteep = new IPEndPoint(multicastaddress, 2222);
}
public void SendMessage(string data)
{
Byte[] buffer = Encoding.UTF8.GetBytes(data);
Byte[] encrypted = Encrypt(data);
udpclient.Send(encrypted, encrypted.Length, remoteep);
}
public void Listen()
{
UdpClient client = new UdpClient();
client.ExclusiveAddressUse = false;
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2222);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;
client.Client.Bind(localEp);
client.JoinMulticastGroup(multicastaddress);
string formatted_data;
while (true)
{
Byte[] data = client.Receive(ref localEp);
formatted_data = Decrypt(data);
Console.WriteLine(formatted_data);
}
}
private byte[] Encrypt(string clearText, string EncryptionKey = "123")
{
byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
byte[] encrypted;
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
encrypted = ms.ToArray();
}
}
return encrypted;
}
private string Decrypt(byte[] cipherBytes, string EncryptionKey = "123")
{
string cipherText = "";
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.UTF8.GetString(ms.ToArray());
}
}
return cipherText;
}
}
}