private int sendAGSByLine() { try { AGS_Package ap = new AGS_Package(ags_data, dictionaryfile, datastructure); String s1 = ap.getContentsAGS(); TextReader tr = new StringReader(s1); String line = ""; Stream networkStream = socket.GetStream(); StreamWriter s_out = new StreamWriter(networkStream) { NewLine = "\r\n", AutoFlush = true }; while ((line = tr.ReadLine()) != null) { s_out.Write(line); s_out.WriteLine(); } s_out.Flush(); // status = enumStatus.AGSSent; return(1); } catch (Exception e) { Console.WriteLine(e.Message); // status = enumStatus.AGSSendFailed; return(-1); } }
private int sendAGSByStream() { try { AGS_Package ap = new AGS_Package(ags_data, dictionaryfile, datastructure); String s1 = ap.getContentsAGS(); byte[] buffer = new byte[MAX_BUFFER_SIZE]; int length = 0; int total = 0; Stream networkStream = socket.GetStream(); BufferedStream s_out = new BufferedStream(networkStream); MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(s1)); // reset to beginning of string' ms.Position = 0; while ((length = ms.Read(buffer)) != 0) { s_out.Write(buffer, 0, length); total = +length; } s_out.Flush(); // String msg = "AGS data sent (" + total + ")"; // System.out.println(msg); // status = enumStatus.AGSSent; return(1); } catch (Exception e) { Console.WriteLine(e.Message); // status = enumStatus.AGSSendFailed; return(-1); } }
protected async virtual Task <int> readXML() { // recieve xml data from ags_server // https://stackoverflow.com/questions/5867227/convert-streamreader-to-byte DateTime dtStart = DateTime.Now; while (IsConnected()) { try { Stream networkStream = socket.GetStream(); StreamReader s_in = new StreamReader(networkStream); Boolean IsXMLData = false; int read; byte[] buffer = new byte[MAX_BUFFER_SIZE]; // MemoryStream ms = new MemoryStream(); StringBuilder sb = null; while ((read = s_in.BaseStream.Read(buffer, 0, MAX_BUFFER_SIZE)) > 0) { // ms.Write(buffer, 0, read); string s = System.Text.Encoding.UTF8.GetString(buffer, 0, read); if (IsXMLData == true) { sb.Append(s); if (s.IndexOf(AGS_Package.FILE_END) > 0) { break; } } if (IsXMLData == false) { if (s.IndexOf(AGS_Package.FILE_START) == 0) { IsXMLData = true; sb = new StringBuilder(); sb.Append(s); if (s.IndexOf(AGS_Package.FILE_END) > 0) { break; } } else { // status = enumStatus.XMLReceiveFailed; return(-1); // throw new Exception ("Unexpected response from AGS Server [" + s.Substring(0,32) + "]"); } } } // xml_data = Encoding.UTF8.GetString(ms.ToArray()); AGS_Package ap = new AGS_Package(AGS_Package.ContentType.XML, sb.ToString()); if (ap.HasXMLData()) { xml_data = ap.data_xml; status = enumStatus.XMLReceived; return(1); } else { DateTime dtNow = DateTime.Now; TimeSpan tsDuration = dtNow - dtStart; if (tsDuration.Minutes >= MAX_WAIT_MINS) { return(-1); } } } catch (Exception e) { Console.WriteLine(e.Message); // status = enumStatus.XMLReceiveFailed; return(-1); } } return(-1); }