public void startClient(TcpClient inTcpClient, string clineNo, Hashtable cList, Server s) { tcpClient = inTcpClient; clNo = clineNo; clientsList = cList; srv = s; Thread ctThread = new Thread(() => doChat(srv)); ctThread.Start(); }
private void doChat(Server s) { srv = s; Thread.CurrentThread.Name = "doChatThread"; string messageFile = "message.xml"; string dataFromClient = null; while ((true)) { try { int buffSize = 1024; byte[] bytesFrom = new byte[buffSize]; tcpClient.GetStream().Read(bytesFrom, 0, buffSize); dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); string xmlText = dataFromClient.Replace("$", ""); xmlText = xmlText.Substring(0, xmlText.LastIndexOf(">")+1); File.WriteAllText(messageFile, xmlText); xmlMessageReader xmr = new xmlMessageReader(); xmr.read(messageFile); srv.broadcast(xmr.body, clNo, true); } catch (System.ObjectDisposedException) { string leaverNick = dataFromClient; srv.broadcast("", dataFromClient, false); return; } catch (System.Xml.XmlException e) { Console.WriteLine(e); tcpClient.GetStream().Close(); tcpClient.Close(); srv.broadcast("", dataFromClient, false); return; } catch (Exception ex) { Console.WriteLine("errorrrr" + ex.ToString()); tcpClient.GetStream().Close(); tcpClient.Close(); srv.broadcast("", dataFromClient, false); return; } }//end while }
private void hostButton_Click(object sender, EventArgs e) { int serverPort; try { serverPort = int.Parse(portField.Text); } catch (System.FormatException err) { MessageBox.Show("Server Port: can only contain numbers"); return; } Server srv = new Server(this); Thread connectThread = new Thread(() => srv.hostServer(serverPort)); connectThread.Start(); }