Send Binary Data to the Client

This is one of the example of socket programming
The following is the code for send the binary data to the client

Namespace part

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

Main code part

public class BinaryDataSender
public static void Main()
Employee MyEmployee1 = new Employee();
Employee MyEmployee2 = new Employee();

MyEmployee1.LastName = "MS";
MyEmployee1.FirstName = "Nathan";
MyEmployee1.Salary = 30000;

MyEmployee2.LastName = "MY";
MyEmployee2.FirstName = "RAji";
MyEmployee2.Salary = 50000;

TcpClient MyTcpClient = new TcpClient("", 9050);
IFormatter Myformatter = new BinaryFormatter();
NetworkStream Mystr = MyTcpClient.GetStream();

Myformatter.Serialize(Mystr, MyEmployee1);
Myformatter.Serialize(Mystr, MyEmployee2);

Serializable Employee Class

public class Employee
public string LastName;
public string FirstName;
public double Salary;

public SerialEmployee()
LastName = null;
FirstName = null;
Salary = 0.0;

Code Expalantion

1. Create the Serializable class Employee
2. Create the instancess for the class
3. Assign the data
4. Create the instance TcpClient
5. Create the instance of the Iformatter
6. Create the instance of the NetworkStream
7. Send the data


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: