Reputation: 29
I created a simple client and server app in windows forms, now I wanted to create it in windows store app framework. using System.Net.Sockets; isnt supported there . How should I approach to this and what class should i use ? Following is the code in winforms c#: many thanks
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Media;
namespace ChatClient
{
public partial class Form1 : Form
{
Socket sck;
EndPoint epLocal, epRemote;
public Form1()
{
InitializeComponent();
//what would I use instead in windows store app, using System.Net.Sockets; namespace isnt supported ?
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
//Getting local IP address through code
private string GetLocalIP()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
private void MessageCallBack (IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size>0)
{
byte[] receivedData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receiveMessage = eEncoding.GetString(receivedData);
listMessage.Items.Add("Friend : "+receiveMessage);
}
byte[] 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 textBox3_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
groupBox1.Text = Environment.UserName;
textBox1.Text = GetLocalIP().ToString();
btnSend.Enabled = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
if (CheckConnection() == 1)
{
btnStart.Text = "Please Wait...";
Thread.Sleep(2000);
epLocal = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(comboBox1.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(fndIP.Text), Convert.ToInt32(comboBox2.Text));
sck.Connect(epRemote);
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
btnStart.Enabled = false;
btnStart.Text = "Connected";
btnStart.BackColor = Color.LightGreen;
btnStart.ForeColor = Color.White;
btnSend.Enabled = true;
textBox5.Focus();
button1.Enabled = true;
}
else
{
MessageBox.Show("Check Your Internet connection");
}
}
catch(Exception ex1)
{
MessageBox.Show(ex1.ToString());
}
}
private void btnSend_Click(object sender, EventArgs e)
{
try
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] msg = new byte[1500];
msg = enc.GetBytes(textBox5.Text);
new SoundPlayer(Properties.Resources.chat).Play();
sck.Send(msg);
listMessage.Items.Add("Me : "+textBox5.Text);
new SoundPlayer(Properties.Resources.chat).Play();
textBox5.Clear();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private int CheckConnection ()
{
Ping myPing = new Ping();
String host = "74.125.20.147";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success)
{
return 1;
}
else
{
return 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
sck.Close();
}
}
}
Upvotes: 0
Views: 355
Reputation: 977
It's in the Windows.Networking.Sockets
namespace
See .Net for Windows Store Apps overview article for a complete description on how to deal with these differences.
Upvotes: 0
Reputation: 151588
Send and receive data with TCP or UDP sockets in your Windows Runtime app using features in the
Windows.Networking.Sockets
namespace.
You use UDP, so see How to connect with a datagram socket (XAML) which uses the class Windows.Networking.Sockets.DatagramSocket
.
Upvotes: 2