Search the Community
Showing results for tags 'sockets'.
-
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 = ""; }
-
Am creat o aplicatie de android care consta intr-un client si un server. Comunicarea se face prin bluetooth sockets. Totul merge bine, serverul ruleaza in background si asteapta incontinuu conexiuni de la clienti. Problema este ca se descarca bateria prea repede pe telefonul pe care este instalat serverul. Un approach pe care l-am folosit ca sa realizez serverul si care pare sa manance repede din baterie este asta: BluetoothSocket bt_socket = null; BluetoothServerSocket bt_conn = null; try { bt_conn = bt.listenUsingRfcommWithServiceRecord("GEAR_SMS", UUID.fromString(uuid_str)); while (true) { bt_socket = bt_conn.accept(); new Thread(new BtSocket(bt_socket)).start(); } } catch (Exception e) { e.printStackTrace(); } In marea parte a timpului serverul este idle si doar asteapta conexiuni, deci consumul ridicat de baterie se poate rezuma doar la bucata asta: while (true) { bt_socket = bt_conn.accept(); } Exista alta metoda de a realiza astfel de conexiuni intre client si server, dar fara a consuma bateria? Sau fac eu ceva gresit?