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("127.0.0.1", 9050);
IFormatter Myformatter = new BinaryFormatter();
NetworkStream Mystr = MyTcpClient.GetStream();
Myformatter.Serialize(Mystr, MyEmployee1);
Myformatter.Serialize(Mystr, MyEmployee2);
Mystr.Close();
MyTcpclient.Close();
}
}
Serializable Employee Class
[Serializable]
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
