Poziom mojej wiedzy jest znikomy, ale dzięki temu (mam nadzieję) problemy są banalne i szybko dostanę jakieś podpowiedzi;)
1.Jest to aplikacja, która docelowo ma być serwerem w programie typu-komunikator. Z połączeniem z jednym klientem nie ma problemu, pojawia się on, gdy próbuję podłączyć sie z trzeciego itd... jak do tego podejść, bo niestety kończą mi się pomysły(jeden z nich w "zakomentarzowanych" fragmentach).
2.Jak przy używaniu rutera obejść IP wew by móc komunikować się na zewnątrz.
3.Jak zrobić, by nie trzeba było ręcznie wpisywać adresów IP klientów
Przyjmę każdą pomoc;)
kodzik:
public partial class Form1 : Form
{
Socket sck;
EndPoint epLocal, epRemote, epRemote2;
byte[] buffer, buffer2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
textLocalIP.Text = GetLocalIP();
textRemoteIP.Text = "10.0.0.5";//GetLocalIP();
//textRemoteIP2.Text = "10.0.0.3";
//MessageBox.Show(textRemoteIP.Text);
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
private void btnPolacz_Click(object sender, EventArgs e)
{
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIP.Text), Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(textRemoteIP.Text), Convert.ToInt32(textRemotePort.Text));
sck.Connect(epRemote);
// epRemote2 = new IPEndPoint(IPAddress.Parse(textRemoteIP2.Text), Convert.ToInt32(textRemotePort2.Text));
// sck.Connect(epRemote2);
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
// buffer2 = new byte[1500];
// sck.BeginReceiveFrom(buffer, 0, buffer2.Length, SocketFlags.None, ref epRemote2, new //AsyncCallback(MessageCallBack), buffer2);
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] receivedData = new byte[1500];
// byte[] receivedData2 = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
// receivedData2 = (byte[])aResult.AsyncState;
ASCIIEncoding aEncoding = new ASCIIEncoding();
string receivedMessage = aEncoding.GetString(receivedData);
// string receivedMessage2 = aEncoding.GetString(receivedData2);
textOdbierz.Items.Add("Friend: " + receivedMessage);
// textOdbierz.Items.Add("Friend2: " + receivedMessage2);
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnWyslij_Click(object sender, EventArgs e)
{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
// byte[] sendingMessage2 = new byte[1500];
sendingMessage = aEncoding.GetBytes(textNapisz.Text);
// sendingMessage2 = aEncoding.GetBytes(textNapisz.Text);
sck.Send(sendingMessage);
// sck.Send(sendingMessage2);
textOdbierz.Items.Add("Me: " + textNapisz.Text);
textNapisz.Text = "";
}