[C #] Socket Communications objects via C #

Yesterday we showed you do simple example transfers the socket in c #, hôm nay tiếp tục hướng dẫn các bạn truyền đối tượng qua socket 😉

This article guides you write the program transfers between client and server objects. Client is 1 From information 1 book (Book) including title, author, Publishing year and category. Client 2 send button (will send information to the server) and receive (receive information from the server). When you press the submit button, the server Client will receive and record information on file on the server. When the client receives the server button reads the file and sent to the Client, Client displays the information received on From. Like the picture below.

socket trong c#

Now of what we will build 2 BookClient and BookServer Project is structured like this:

The object communication via socket in c # it similar as the previous day, only thing we need to do to move objects byte array and then send receive. Upon receipt of the object are needed to restore function object from a byte array that.

Ham move 1 object to byte array and vice versa.

// nen data thanh 1 mang byte
public byte[] SerializeData(Object o)
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf1 = new BinaryFormatter();
    bf1.Serialize(ms, o);
    return ms.ToArray();
}

// giai nen 1 mang byte thanh 1 doi tuong (bject)
public object DeserializeData(byte[] theByteArray)
{
    MemoryStream ms = new MemoryStream(theByteArray);
    BinaryFormatter bf1 = new BinaryFormatter();
    ms.Position = 0;
    return bf1.Deserialize(ms);
}

But your attention to perform the compression object to Serialize the object class that should have keywords [Serialize] before class. Specifically for the following class Book (class Book nằm trong project của Client)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookClient
{
    [Serializable]
    class Book
    {
        public String title, author, yearOfPublication, genre;
    }
}

Following is the code From Client (The interface design of your offline)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;

namespace BookClient
{
    public partial class Form1 : Form
    {
        Socket client;
        private const int BUFFER_SIZE = 1024;
        private const int PORT_NUMBER = 7826;
        static ASCIIEncoding encoding = new ASCIIEncoding();
        public Form1()
        {
            InitializeComponent();
            connectServer();
        }


        // ket noi den server
        private void connectServer()
        {
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), PORT_NUMBER);
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(iep);
        }


// nen data thanh 1 mang byte
public byte[] SerializeData(Object o)
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf1 = new BinaryFormatter();
    bf1.Serialize(ms, o);
    return ms.ToArray();
}

// giai nen 1 mang byte thanh 1 doi tuong (bject)
public object DeserializeData(byte[] theByteArray)
{
    MemoryStream ms = new MemoryStream(theByteArray);
    BinaryFormatter bf1 = new BinaryFormatter();
    ms.Position = 0;
    return bf1.Deserialize(ms);
}

        // gui du lieu cho server
        private void btnSend_Click(object sender, EventArgs e)
        {
            // tao va lay du lieu tu form
            Book b = new Book();
            b.title = tbTitle.Text;
            b.author = tbAuthor.Text;
            b.yearOfPublication = tbYear.Text;
            b.genre = tbGenre.Text;

            // gui lenh yeu cau la client muon gui du lieu
            client.Send(encoding.GetBytes("send"));

            // gui du lieu
            client.Send(SerializeData(b));
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {
            // gui lenh yeu cau lay du lieu tu server
            client.Send(encoding.GetBytes("receive"));
            byte[] data = new byte[BUFFER_SIZE];

            // nhan du lieu tu server gui xuong
            int rec = client.Receive(data);

            // hien thi thong tin nhan duoc len form
            Book b = (Book)DeserializeData(data);
            tbTitle.Text = b.title;
            tbAuthor.Text = b.author;
            tbYear.Text = b.yearOfPublication;
            tbGenre.Text = b.genre;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Code của server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
namespace BookServer
{
    class Program
    {
        private const int BUFFER_SIZE = 1024;
        private const int PORT_NUMBER = 7826;

        static ASCIIEncoding encoding = new ASCIIEncoding();
        Socket server;

        // ghi data xuong file
        protected bool SaveData(byte[] Data)
        {
            BinaryWriter Writer = null;
            string Name = "book";

            try
            {
                Writer = new BinaryWriter(File.OpenWrite(Name));          
                Writer.Write(Data);
                Writer.Flush();
                Writer.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }

        private void initServer()
        {
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), PORT_NUMBER);
            // tao server
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Console.WriteLine("dang cho client ket noi...");
            
            // lang nghe ket noi
            server.Bind(iep);
            server.Listen(10);

            // chap nhan client ket noi
            Socket client = server.Accept();
            Console.WriteLine("Chap nhan ket noi tu: " + client.RemoteEndPoint.ToString());
            byte[] data = new byte[BUFFER_SIZE];

            // vong lap vo han thuc hien viec nhan va gui du lieu
            while (true)
            {
                // nhan lenh: gui hoac nhan du lieu
                int rec = client.Receive(data);
                String command = encoding.GetString(data, 0, rec);
                Console.WriteLine("command = " + command);
                
                // client send data den server
                if (command.Equals("send"))
                {
                    client.Receive(data);
                    Console.WriteLine("Nhan thanh cong");
                    if (SaveData(data))
                    {
                        Console.WriteLine("Ghi thanh cong");
                    }
                    else
                    {
                        Console.WriteLine("Ghi that bai");
                    }
                }
                // client muon lay data tu server
                else
                {
                    Console.WriteLine("Dang gui...");
                    // doc du lieu tu file
                    byte[] arraydata = File.ReadAllBytes("book");
                    // gui du lieu doc duoc cho client
                    client.Send(arraydata);
                }
            }
        }

        static void Main(string[] args)
        {
            new Program().initServer();

        }
    }
}

You can download 2 Client and Server in this project.