-
July 4th, 2013, 12:39 PM
#1
communication problem server-multiple clients
My programming knowledge is really low(as much as my english), thus my problems are trivial and hopefully i will receive fast and valuable answers
1.At the end this part of my app shoudl be some kind of a server, which is sending and receiving messages from clients. Now its kind of communicator, but works good only when i'm connected with one client. How can I make it good for multiple clients(one my [pathetic;] tries is under "//")?
2.How can I take my IP when I'm using router?
3.How can I make function auto-taking clients IP(not writing it manually)
I'll take any help
here's code:
Code:
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);
take.Items.Add("Friend: " + receivedMessage);
// take.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 Send_Click(object sender, EventArgs e)
{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
// byte[] sendingMessage2 = new byte[1500];
sendingMessage = aEncoding.GetBytes(write.Text);
// sendingMessage2 = aEncoding.GetBytes(write.Text);
sck.Send(sendingMessage);
// sck.Send(sendingMessage2);
take.Items.Add("Me: " + textNapisz.Text);
write.Text = "";
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|