CSharp Equivalent of java.socket.getOutputStream and java.io.OutputStream using Visual Studio 2008 SP1 RRS feed

  • Question

  • Dear All,

    I am looking for CSharp Equivalent of  java.socket.getOutputStream and java.io.OutputStream using Visual Studio 2008 SP1.

    Here is my draft java code below

    // in Java

    public byte[] connectData



    sk.setSoLinger(true, 3);



    sk.connect(new InetSocketAddress(address, port), 5000);

    OutputStream os = sk.getOutputStream();      // How abt in .NET     

    InputStream is = sk.getInputStream(); // How abt in .NET     








    socket sk = new Socket();

    However, I would like to confirm and advice from someone else the way I implement is correct.

    Thanks and best regards

    • Moved by Mike Feng Thursday, February 14, 2013 9:01 AM
    Wednesday, February 13, 2013 9:46 AM

All replies

  • For outputStream use StreamWriter and for InputStream you can use StreamReader

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, February 13, 2013 10:57 AM
  • Thank you and any working code snippets pls.

    Thursday, February 14, 2013 2:06 AM
  • using (StreamWriter writer = new StreamWriter("important.txt"))
    	    writer.Write("Word ");
    	    writer.WriteLine("word 2");
                // Create an instance of StreamReader to read from a file. 
                // The using statement also closes the StreamReader. 
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                    string line;
                    // Read and display lines from the file until the end of  
                    // the file is reached. 
                    while ((line = sr.ReadLine()) != null) 
            catch (Exception e) 
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Thursday, February 14, 2013 4:10 AM
  • NetworkStream myNetworkStream = new NetworkStream(sk);
    //following for asynchronous read and write
    //following for synchronous read and write
    //Please refer to http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx

    Please refer to http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx for exact help regarding your business requirement

    The NetworkStream class provides methods for sending and receiving data over Stream sockets in blocking mode. For more information about blocking versus nonblocking Sockets, see Using an Asynchronous Client Socket. You can use the NetworkStream class for both synchronous and asynchronous data transfer. For more information about synchronous and asynchronous communication, see Sockets.
    To create a NetworkStream, you must provide a connected Socket. You can also specify what FileAccess permission the NetworkStream has over the provided Socket. By default, closing the NetworkStream does not close the provided Socket. If you want the NetworkStream to have permission to close the provided Socket, you must specify true for the value of the ownsSocket parameter.
    Use the Write and Read methods for simple single thread synchronous blocking I/O. If you want to process your I/O using separate threads, consider using the BeginWrite and EndWrite methods, or the BeginRead and EndRead methods for communication.
    The NetworkStream does not support random access to the network data stream. The value of the CanSeek property, which indicates whether the stream supports seeking, is always false; reading the Position property, reading the Length property, or calling the Seek method will throw a NotSupportedException.
    Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

    Thanks, Durgesh Chaudhary. http://home.techphernalia.com

    Thursday, February 14, 2013 5:44 AM
  • Hi Durgesh,

    The equivalent of Java in Outputstream/InputSteam is System.IO.Stream according to the book named ".NET for Java Developers".

    Even, I saw some forum,  used NetworkStream with Outputstream/InputSteam however, I didn't achieve in simple test program.

    Pls advise me with working code snippet if you don't mind.

    Best regards

    Thursday, February 14, 2013 7:22 AM
  • You are right, but in current scenario you need to use NetworkStream. Please try and it will solve your problem

    Thanks, Durgesh Chaudhary. http://home.techphernalia.com

    Thursday, February 14, 2013 4:51 PM
  • Hi Durgesh,

    It might be true but pls help me to have a working code snippet to test before actual API implementation as shown below my compiling code

    // in CSharp
    // Tx is Trnsmit Class
    public sbyte[] sendrecv(Tx tx, sbyte[] data; int len, int comp)
       sbyte[] recv = null;
       // Start Socket
       Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
       System.Net.IPAddress	remoteIPAddress	 = System.Net.IPAddress.Parse(asaddress);
       System.Net.IPEndPoint	remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, asport);
          LingerOption lingerOpt = new LingerOption(true, 3);
          sk.LingerState = lingerOpt;
          sk.NoDelay = true;
          sk.SendTimeout = 0;
          sk.ReceiveTimeout = 5000;
          sbyte[] send;
          // Here how to differentiate or specify output and input stream
          NetworkStream oss = new NetworkStream(sk);  // sk.send ?
          NetworkStream iss = new NetworkStream(sk);  // sk.receive ?
          oss.Write((byte[])(Array)send, 0, send.Length);
          oss.Write((byte[])(Array)data, 0, data.Length);
          sbyte[] hdr;
          // Do something here
          //recv = new byte[Length];
          return recv;

    Pls have a look my code with test data then can prove it will work in actual implementaiton.

    Thanks and best regards

    Friday, February 15, 2013 3:05 AM
  • Good morning Durgesh and RohitArora,

    Any good news for my assumption and since I start to test today onwards all methods.

    Thanks and best regards

    Monday, February 18, 2013 2:06 AM