winforms - C# TcpClient updating (for an IRC client) -
i bascially trying make barebones irc client c#, winforms, , tcpclient display raw data irc server onto the text area (textbox1). struggling on updating code (reading stream server). right have timer runs function (listener
) reads tcp stream every 100ms. application freezes , cursor disapeears, , application hangs trying grab more data. better updating function?
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.io; using system.net.sockets; namespace logernirc { public partial class form1 : form { public form1() { initializecomponent(); } //variables tcpclient client; streamreader sr; streamwriter sw; //functions public void connect(string host) { client = new tcpclient(host, 6667); sr = new streamreader(client.getstream()); sw = new streamwriter(client.getstream()); } public void write(string str) { textbox1.text += str; } public void senddata(string str) { sw.writeline(str); sw.flush(); } public void listener() { string data = sr.readline(); write(data); } //end functions private void form1_load(object sender, eventargs e) { //initialize write("welcome logernirc. type \"/help\" commands.\r\n"); } private void button1_click(object sender, eventargs e) //submit button clicked { //textbox 1 text area , textbox 2 message/command area //command area if (textbox2.text == "/help") { write("help:\r\n/connect connect irc server\r\n/help display menu\r\n/join join channel"); } if (textbox2.text.startswith("/connect")) { write("\r\nconnecting " + textbox2.text.split(' ')[1] + " on port 6667..."); connect(textbox2.text.split(' ')[1]); } if (textbox2.text.startswith("/join")) { write("\r\njoining channel " + textbox2.text.split(' ')[1]); } if (textbox2.text == "/test") { timer1.start(); connect("irc.freenode.net"); write("\r\nactivating test function..."); senddata("nick logern"); senddata("user logern 0 * :logern"); listener(); } } private void timer1_tick(object sender, eventargs e) { //read data listener(); } } }
the delay happens when timer event raised, there's no data read. sit , wait until there is. best way address issue use asynchronous operations handle i/o. example:
public form1() { initializecomponent(); } //variables tcpclient client; streamreader sr; streamwriter sw; //functions public void connect(string host) { client = new tcpclient(host, 6667); sr = new streamreader(client.getstream()); sw = new streamwriter(client.getstream()); } public void write(string str) { textbox1.text += str; } public void senddata(string str) { sw.writeline(str); sw.flush(); } public async task listener() { try { string data while ((data = await sr.readlineasync()) != null) { write(data); } } catch (objectdisposedexception) { // socket closed forcefully } } //end functions private void form1_load(object sender, eventargs e) { //initialize write("welcome logernirc. type \"/help\" commands.\r\n"); } private void button1_click(object sender, eventargs e) //submit button clicked { //textbox 1 text area , textbox 2 message/command area //command area if (textbox2.text == "/help") { write("help:\r\n/connect connect irc server\r\n/help display menu\r\n/join join channel"); } if (textbox2.text.startswith("/connect")) { write("\r\nconnecting " + textbox2.text.split(' ')[1] + " on port 6667..."); connect(textbox2.text.split(' ')[1]); } if (textbox2.text.startswith("/join")) { write("\r\njoining channel " + textbox2.text.split(' ')[1]); } if (textbox2.text == "/test") { connect("irc.freenode.net"); // initiate async reading (storing returned task in variable // prevents compiler complaining don't await // call). var _ = listener(); write("\r\nactivating test function..."); senddata("nick logern"); senddata("user logern 0 * :logern"); } }
the above example leaves out error-checking , other niceties, it's basic idea of want do.
Comments
Post a Comment