locked
TcpClient fails to send a specific set of data RRS feed

  • Question

  • Hello. I'm working on a FTP server for a private project and I've came across a pretty weird behavior. I have something like:

    public void send(string s){
    	...
    	byte[] message = System.Text.Encoding.ASCII.GetBytes(s);
    
    	BinaryWriter writer = new BinaryWriter(socket.GetStream());
    	writer.Write(message, 0, message.length);
    	writer.Flush();
    	...
    }

    "socket" is a TcpClient. When I try to respond to a PASV request:

    this.send("227 =127,0,0,1,0,20\r\n");

    I get an IOException while writing to the stream. It says the connection was aborted by the host. But if I change it to:

    this.send("527 =127,0,0,1,0,20\r\n");

    It works! Of course the client doesn't accept my reply, but the point is the data is sent, which didn't happen before. Using Wireshark I took a look at the packets. When I send "227", this happens:

    135  10.491942  127.0.0.1  127.0.0.1  FTP  46  Request: PASV

    136  10.491942  127.0.0.1  127.0.0.1  TCP  40  21→59556 [ACK] Seq=181 Ack=60 Win=65536 Len=0

    137  10.494943  127.0.0.1  127.0.0.1  TCP  40  21→59556 [RST, ACK] Seq=181 Ack=60 Win=0 Len=0

    So, the server is sending a reset. But when I send "527", I get:

    196  8.779808  127.0.0.1  127.0.0.1  FTP  46  Request: PASV

    197  8.779808  127.0.0.1  127.0.0.1  TCP  40  21→63973 [ACK] Seq=181 Ack=60 Win=65536 Len=0

    198  8.785812  127.0.0.1  127.0.0.1  FTP  61  Response: 527 =127,0,0,1,0,20

    It makes no send to me at all. Has anyone ever seen this before? What should I do?

    Thank you.


    • Edited by Leonardo B Monday, August 24, 2015 9:27 PM misspelling
    Monday, August 24, 2015 9:24 PM

Answers

  • That looks rather weird. Perhaps you have some sort of firewall/antivirus that scans network traffic and blocks connections if it doesn't like what it sees?

    As a side note you don't need to use BinaryWriter, just write the byte array directly into the stream. Not that it would make a difference in this case unless you're disposing the writer in code that you're not showing in your post.

    • Proposed as answer by Kristin Xie Wednesday, September 2, 2015 8:53 AM
    • Marked as answer by Fred Bao Monday, September 7, 2015 10:01 AM
    Tuesday, August 25, 2015 8:30 AM
  • Hi Leonardo ,

    I would suggest you use netstat -ano and then see tasklist, I think we may get more details from here.

    For example, we may see the following

    TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       656

    Which may means the port is used by other applications.

    Moreover,I agree with Mike,  TcpClient.GetStream Method returns the NetworkStream used to send and receive data. there is no need to use BinaryWriter, just write the byte array directly into the stream will be better.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Thursday, August 27, 2015 9:56 AM
    • Proposed as answer by Kristin Xie Wednesday, September 2, 2015 8:53 AM
    • Marked as answer by Fred Bao Monday, September 7, 2015 10:01 AM
    Thursday, August 27, 2015 9:56 AM

All replies

  • That looks rather weird. Perhaps you have some sort of firewall/antivirus that scans network traffic and blocks connections if it doesn't like what it sees?

    As a side note you don't need to use BinaryWriter, just write the byte array directly into the stream. Not that it would make a difference in this case unless you're disposing the writer in code that you're not showing in your post.

    • Proposed as answer by Kristin Xie Wednesday, September 2, 2015 8:53 AM
    • Marked as answer by Fred Bao Monday, September 7, 2015 10:01 AM
    Tuesday, August 25, 2015 8:30 AM
  • Hi Leonardo ,

    I would suggest you use netstat -ano and then see tasklist, I think we may get more details from here.

    For example, we may see the following

    TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       656

    Which may means the port is used by other applications.

    Moreover,I agree with Mike,  TcpClient.GetStream Method returns the NetworkStream used to send and receive data. there is no need to use BinaryWriter, just write the byte array directly into the stream will be better.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Thursday, August 27, 2015 9:56 AM
    • Proposed as answer by Kristin Xie Wednesday, September 2, 2015 8:53 AM
    • Marked as answer by Fred Bao Monday, September 7, 2015 10:01 AM
    Thursday, August 27, 2015 9:56 AM