简体   繁体   中英

Constant reading from StreamReader from TCP connection, C#

I have working script in Swift, perfectly reading TCP connection stream non-stop. I need to make the same in C# and here i got stuck. The remote service works like this: it waits for the first command, which is "*99*1##", then it replies "*#*1##" and start forwarding another similar messages to the same stream. All i need is to record all those server answers to a file. The problem is i can't get more then one server answer. Just after first server answer, i'm not able to read from the stream anymore. Peek()/Read() methods return -1, and ReadLine returns null. And i'm sure that it's wrong as from Swift script i can see that server sends me much more then just one message. First, here is Swift code (reading stream)

    let queryString: String = "*99*1##"
    let queryData: Data = queryString.data(using: String.Encoding.utf8, allowLossyConversion: false)! as Data
    queryData.withUnsafeBytes { outputStream.write($0, maxLength: queryData.count) }
    var readChars: Int = 0
    while true {
        readChars = inputStream.read(&buffer, maxLength: buffer.count)
        if (readChars > 0) {
            let readString: String = NSString(data: NSData(bytes:buffer, length:readChars) as Data, encoding: String.Encoding.utf8.rawValue)! as String
            print("\(readString)\n")
        } else {
            print ("Server closed connection\n")
            inputStream.close()
            outputStream.close()
            break
        }
    }

Communication looks like that:

Client->*99*1##
Server->*#*1##
Server->*1*34*#6#4#02##
Server->*1*1*13#4#01##
Server->*1*0*13#4#01##
and etc...

In C# i have tried StreamReader methods like ReadLine, Read, ReadToEnd and none of them gave me more then one server answer.

string address = "192.168.0.10";
int port = 20000;
TcpClient client = new TcpClient(address, port);
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
string command = "*99*1##";
Console.WriteLine("Client->" + command);
sw.WriteLine(command);
sw.Flush();

string data;
while (true)
{
    data = sr.ReadToEnd();
    if (data != null) 
    {
        Console.WriteLine(Server->" + data);
    } else
    {
        break;
    }
}
Console.WriteLine("Server closed connection.");

Here communication looks totally wrong:

Client->*99*1##
Server->*#*1##
Server closed connection

Second and other server answers comes after 5 to 10 seconds later then first. Maybe there is some timeout problem on my side? Server has timeout around 30 mins and i can keep session with it through the Swift or Telnet that long.

Thank you in advance for all the ideas.

You should use Read() instead of ReadToEnd() because reader stream keeps alive continuously. So, try like this;

string address = "127.0.0.1";
int port = 25000;
var client = new TcpClient(address, port);
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
client.
string command = "*99*1##";
Console.WriteLine("Client->" + command);
sw.WriteLine(command);
sw.Flush();
char[] charArray = new char[100];
while (true)
{
    var readByteCount = sr.Read(charArray, 0, charArray.Length);
    if (readByteCount > 0)
    {
        var data = new string(charArray, 0, readByteCount);
        Console.WriteLine(data + " + data");
    }
    if (!client.Connected)
    {
        break;
    }
}
Console.WriteLine("Server closed connection.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM