Jump to content

blackfriday

Members
  • Content count

    1
  • Joined

  • Last visited

Community Reputation

0 Neutral

About blackfriday

  • Rank
    Newbie

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. I am trying to make a C# Chat. So far i have been able to work on getting the 2 clients to work together and send notification using the notifyicon. now What i want to achieve is something done by yahoo messenger. If i am online and dont have a messenger window on (if i am not talking to anyone) and someone sends me a message on LAN, I should be able to receive a new Message window. My code looks like this : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace ChatTestNowx { public partial class Form1 : Form { Socket sock; EndPoint epLocal, epRemote; byte[] buffer; public Form1() { InitializeComponent(); } private void groupBox2_Enter(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); pc1IP.Text = GetLocalIP(); Pc2IP.Text = GetLocalIP(); Message.KeyDown += new KeyEventHandler(MyKeyDownFunction); // Event to open new chat window on PC } //Message.KeyDown += new KeyEventHandler(MyKeyDownFunction); private void MyKeyDownFunction(object sender, KeyEventArgs e) { // Open new Chat window and show chat information } private void MessageCallBack(IAsyncResult aResult) { try { byte[] receivedData = new byte[1500]; receivedData = (byte[])aResult.AsyncState; ASCIIEncoding asc = new ASCIIEncoding(); string recievedMsg = asc.GetString(receivedData); listMessage.Items.Add("Friend:" + recievedMsg); buffer = new byte[1500]; sock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void send_btn_Click(object sender, EventArgs e) { ASCIIEncoding aec = new ASCIIEncoding(); byte[] sendingMessage = new byte[1500]; sendingMessage = aec.GetBytes(Message.Text); sock.Send(sendingMessage); listMessage.Items.Add("Me :" + Message.Text); Message.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 Message_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //this.WindowState = FormWindowState.Normal; ASCIIEncoding aec = new ASCIIEncoding(); byte[] sendingMessage = new byte[1500]; sendingMessage = aec.GetBytes(Message.Text); sock.Send(sendingMessage); listMessage.Items.Add("Me :" + Message.Text); Message.Text = ""; } } private void connect_btn_Click(object sender, EventArgs e) { epLocal = new IPEndPoint(IPAddress.Parse(pc1IP.Text), Convert.ToInt32(pc1Port.Text)); sock.Bind(epLocal); epRemote = new IPEndPoint(IPAddress.Parse(Pc2IP.Text), Convert.ToInt32(Pc2Port.Text)); sock.Connect(epRemote); buffer = new byte[1500]; sock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer); connect_btn.Text = "&Connected!"; connect_btn.Enabled = false; } } } What i have tried so far, I created events. And wanted to use this event only it does not work as i expected i added this and it does not work as i expected , hence i had to take it out. if (e.KeyCode == Keys.Enter) { Form1 frm = new Form1(); frm.Show(); ASCIIEncoding aec = new ASCIIEncoding(); byte[] sendingMessage = new byte[1500]; sendingMessage = aec.GetBytes(Message.Text); //sock.Send(sendingMessage); listMessage.Items.Add("Me :" + Message.Text); Message.Text = ""; }
×