Getting stream from Socket?

Im am using the following code to receive an object over a socket connection.

How do i get the stream from the socket

 

Socket listener = (Socket)ar.AsyncState;

Socket handler = listener.EndAccept(ar);

 

// how do i get the sockets stream so i can go:

 

BinaryFormatter bf;

bf = new BinaryFormatter();

Astruct reci = (Astruct)bf.Deserialize(stream);



Answer this question

Getting stream from Socket?

  • hye_heena

    Can anyone confirm there is a problem with using .net serialisation using sockets across the internet
  • Luxsy

    RizwanSharp wrote:

    try this:

    See BitConvertor class and make your own Serializer and Desreializer that works fine on Internet Ready Application.

    Can you show me a simple example


  • ThunderRock

    Make a NetworkStream out of the socket.
    For more details you can take a look here.
    http://msdn2.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx

    -Amit



  • MotteKarotte

    First problem is that, It sucks Bandwidth. You send 1000 bytes it puts 500 bytes of its own .

    Its just an example and all depends on your object's structures, field names' length etc.

    I hope to listen more experiences of people. Post this question of .Net Serialization Forums and ask for its use on internet and also send me link of your post. at r.ahmed@protectstar.com



  • dabd

    Amit Paka - MSFT wrote:

    Make a NetworkStream out of the socket.
    For more details you can take a look here.
    http://msdn2.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx

    -Amit

    Works great. Thank you.


  • DTHMTLGOD

    You are always welcome:

    Supose you have to move a complex object on the NetworkStream:

    I spent some time to write this code specially for you. I always prefer this using in my Projects rather than Binary or XML formatter. I hope you'll find this implementation robust and lest memory consuming as compare to built in formatters.

    class Employee

    {

    private int employeeID;

    private string firstName;

    private string lastName;

    private int yearsService;

    private double salary;

    private int firstNameSize; //for internal use only

    private int lastNameSize; //for internal use only

    public Employee() //Default Constructor

    {

    }

    public Employee(int employeeID, string firstName, string lastName, int yearsService, double salary)

    {

    this.employeeID = employeeID;

    this.firstName = firstName;

    this.lastName = lastName;

    this.yearsService = yearsService;

    this.salary = salary;

    }

    public byte[] GetBytes() //Use this method to get bytes to get bytes of object = BinaryFormatter.Serialize();

    {

    using (System.IO.MemoryStream msBuffer = new System.IO.MemoryStream())

    {

    byte[] bEmployeeID = BitConverter.GetBytes(this.employeeID); //Convert employeeID to byte[]

    msBuffer.Write(bEmployeeID, 0, bEmployeeID.Length); // Write converted employeeID in memory stream

    byte[] bFirstNameLength = BitConverter.GetBytes(this.firstName.Length);

    msBuffer.Write(bFirstNameLength, 0, bFirstNameLength.Length);

    byte[] bFirstName = Encoding.ASCII.GetBytes(this.firstName);

    msBuffer.Write(bFirstName, 0, bFirstName.Length);

    byte[] bLastNameLength = BitConverter.GetBytes(this.lastName.Length);

    msBuffer.Write(bLastNameLength, 0, bLastNameLength.Length);

    byte[] bLastName = Encoding.ASCII.GetBytes(this.lastName);

    msBuffer.Write(bLastName, 0, bLastName.Length);

    byte[] bYearsService = BitConverter.GetBytes(this.yearsService);

    msBuffer.Write(bYearsService, 0, bYearsService.Length);

    byte[] bSalary = BitConverter.GetBytes(this.salary);

    msBuffer.Write(bSalary, 0, bSalary.Length);

    return msBuffer.ToArray();

    }

    }

    public Employee(byte[] data) //use this method to convert bytes back to the class object on receiving end

    {

    int place = 0;

    this.employeeID = BitConverter.ToInt32(data, place);

    place += 4; // Move 4 bytes ahead

    this.firstNameSize = BitConverter.ToInt32(data, place);

    place += 4;

    this.firstName = Encoding.ASCII.GetString(data, place, firstNameSize);

    place += this.firstNameSize;

    this.lastNameSize = BitConverter.ToInt32(data, place);

    place += 4;

    this.lastName = Encoding.ASCII.GetString(data, place, lastNameSize);

    place += this.lastNameSize;

    this.yearsService = BitConverter.ToInt32(data, place);

    place += 4;

    this.salary = BitConverter.ToDouble(data, place);

    }

    }

    Try it and compare the results with Built in formatters specially in terms of number of bytes used by this method and the native one. You can reduce its size more if you convert int to short in case of meta data here i.e firstNameSize etc...

    If you face any problem, feel free to write again here.

    Best of Luck ;-)



  • Napalm-uk-RTW

    try this:

    NetworkStream networkStream = new NetworkStream(socket);

    By the way why are you using .Net serialization. I had a very bad experience with it in commercial environment. It works fine on Local Network but on internet it bangs.

    See BitConvertor class and make your own Serializer and Desreializer that works fine on Internet Ready Application.

    Cheers ;-)



  • Getting stream from Socket?