using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ProjectChatApps
{
public partial class Form1 : Form
{
Socket sck;
EndPoint epLocal, epRemote;
byte[] buffer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//set up socket,
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// get user IP
textLocalIp.Text = GetLocalIP();
textRemoteIp.Text = GetLocalIP();
}
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 "";
}
private void buttonConnect_Click(object sender, EventArgs e)
{
// binding socket
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text), Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
//conecting to remote IP
epRemote = new IPEndPoint(IPAddress.Parse(textRemoteIp.Text), Convert.ToInt32(textRemotePort.Text));
sck.Connect(epRemote);
//Listening the specific port
buffer=new byte[1500];
sck.BeginReceiveFrom(buffer,0,buffer.Length,SocketFlags.None,ref epRemote,new AsyncCallback(MessageCallBack),buffer);
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] receivedData = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
//converting byte[] tostring
ASCIIEncoding aEncoding = new ASCIIEncoding();
String receivedMessage=aEncoding.GetString(receivedData);
//adding this message into listbox
listMessage.Items.Add("Friend: "+receivedMessage);
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 buttonSend_Click(object sender, EventArgs e)
{
//Convert string message to byte[]
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage= aEncoding.GetBytes(textMessage.Text);
//Sending
sck.Send(sendingMessage);
//adding to listbox
listMessage.Items.Add("Me: "+textMessage.Text);
textMessage.Text="";
}
}
}
เช่น
A; สวัสดี
เเล้วมันขึ้นที่ฟอร์มเพื่อนว่า
A; ????
มันไม่ขึ้นเป็นภาษาไทย เเต่ภาษาENGขึ้นปกติ
C# ทำโปรเเกรมเเชท เเต่พิมพ์ไทยเเล้วขึ้นเป็น???เเต่ENGปกติ
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ProjectChatApps
{
public partial class Form1 : Form
{
Socket sck;
EndPoint epLocal, epRemote;
byte[] buffer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//set up socket,
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// get user IP
textLocalIp.Text = GetLocalIP();
textRemoteIp.Text = GetLocalIP();
}
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 "";
}
private void buttonConnect_Click(object sender, EventArgs e)
{
// binding socket
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text), Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
//conecting to remote IP
epRemote = new IPEndPoint(IPAddress.Parse(textRemoteIp.Text), Convert.ToInt32(textRemotePort.Text));
sck.Connect(epRemote);
//Listening the specific port
buffer=new byte[1500];
sck.BeginReceiveFrom(buffer,0,buffer.Length,SocketFlags.None,ref epRemote,new AsyncCallback(MessageCallBack),buffer);
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] receivedData = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
//converting byte[] tostring
ASCIIEncoding aEncoding = new ASCIIEncoding();
String receivedMessage=aEncoding.GetString(receivedData);
//adding this message into listbox
listMessage.Items.Add("Friend: "+receivedMessage);
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 buttonSend_Click(object sender, EventArgs e)
{
//Convert string message to byte[]
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage= aEncoding.GetBytes(textMessage.Text);
//Sending
sck.Send(sendingMessage);
//adding to listbox
listMessage.Items.Add("Me: "+textMessage.Text);
textMessage.Text="";
}
}
}
เช่น
A; สวัสดี
เเล้วมันขึ้นที่ฟอร์มเพื่อนว่า
A; ????
มันไม่ขึ้นเป็นภาษาไทย เเต่ภาษาENGขึ้นปกติ