c# - windows form app sending strings -
i made tcp/ip chat windows form application , works fine want make application send text automatically don't want user click send button live streaming!
and using asynchronous connection.
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; using system.net.sockets; namespace chat { public partial class form1 : form { socket sck; endpoint eplocal, epremote; public form1() { initializecomponent(); sck = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); sck.setsocketoption(socketoptionlevel.socket, socketoptionname.reuseaddress, true); textbox1.text = getlocalip();//this application running ip address //textbox5.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 "127.0.0.1"; //here put android device's ip } 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 receivedmessage = eencoding.getstring(receiveddata); //b3deen bntba3 el msg bs b7aletna bdna n5li el touch active. listbox1.items.add("sender:" + receivedmessage); } byte[] buffer = new byte[1500]; sck.beginreceivefrom(buffer, 0, buffer.length, socketflags.none, ref epremote, new asynccallback(messagecallback), buffer); } catch (exception exp) { messagebox.show(exp.tostring()); } } private void button1_click(object sender, eventargs e) { try { //binding message eplocal = new ipendpoint(ipaddress.parse("192.168.1.9"), convert.toint32("80")); sck.bind(eplocal); //hoon el address ta3 el mobile epremote = new ipendpoint(ipaddress.parse("192.168.1.9"), convert.toint32("81"));//texbox5 bn7at el ip ta3 el android wl txt el tani ta3 le port //hoon bn3ml connect network sck.connect(epremote); byte[] buffer = new byte[1500]; sck.beginreceivefrom(buffer, 0, buffer.length, socketflags.none, ref epremote, new asynccallback(messagecallback), buffer); button1.text = "connected"; button1.enabled = false; button2.enabled = true; textbox3.focus(); //trying live sending } catch (exception exp) { messagebox.show(exp.tostring()); } } private void button2_click(object sender, eventargs e) { try { system.text.asciiencoding enc = new system.text.asciiencoding(); byte[] msg = new byte[1500]; msg = enc.getbytes(textbox3.text); sck.send(msg); listbox1.items.add("you:" + textbox3.text); }catch (exception exp) { messagebox.show(exp.tostring()); } // close(); } } }
i assume want send text user entered finished type it. if i'd bad idea because confuse users, add timer counting time since user's last key stroke , if given time passed (let's 5 secs), invokes method button click invoking send string.
Comments
Post a Comment