tags:

views:

197

answers:

3

Hi i have got a TCP/IP Socket project.

i can send string messages to Server with Client side and i can get responses from server. But getting one string message and sending only one string (or any other object).I wanna Encode Personel class to Byte array after send to Clients from server side.And Decode it. than get values from my class.

//SERVER SIDE CODE Connect() starts at on form load

 private void Connect()         
 {
        // start listen socket
        dinleyiciSoket = new TcpListener(System.Net.IPAddress.Any, 10048);
        dinleyiciSoket.Start();
        Socket istemciSoketi = dinleyiciSoket.AcceptSocket();
        NetworkStream agAkisi = new NetworkStream(istemciSoketi);
        BinaryReader binaryOkuyucu = new BinaryReader(agAkisi);
        BinaryWriter binaryYazici = new BinaryWriter(agAkisi);
        string alinanMetin = binaryOkuyucu.ReadString();
        MessageBox.Show(alinanMetin, "Yeni Genelge", MessageBoxButtons.OK);
        binaryYazici.Write(true);
        dinleyiciSoket.Stop();
        Connect();
    }

////////// CLIENT SIDE //////////////

  private string IpAdresi(string host)
    {
        string address = "";
        IPAddress[] addresslist = Dns.GetHostAddresses(host);

        foreach (IPAddress theaddress in addresslist)
        {
            if (theaddress.AddressFamily == AddressFamily.InterNetwork)
            {
                address = theaddress.ToString();
            }


        }
        return address;
    }

    bool onay;
    private void button1_Click(object sender, EventArgs e)
    {
        //create socket connection
        Socket istemciBaglantisi = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //Bağlantıyı gerçekleştir
        if (istemciBaglantisi.Connected != true)
        {
            istemciBaglantisi.Connect(IPAddress.Parse(IpAdresi(txtHost.Text)), 10048);
        }


        agAkisi = new NetworkStream(istemciBaglantisi);
        binaryYazici = new BinaryWriter(agAkisi);
        binaryOkuyucu = new BinaryReader(agAkisi);
        binaryYazici.Write(txtMesaj.Text);
        onay = binaryOkuyucu.ReadBoolean();
        MessageBox.Show(onay.ToString());
        istemciBaglantisi.Close();



    }
+1  A: 

Take a look at object serialization. See here for examples. That should get you going in the right direction.

John Fricker
A: 

You can use google's protocol buffers. It is a fast and compact mechanism for serializing objects. There are two implementations on .NET: protobuf-net and protobuf.

Darin Dimitrov
A: 

I'd use object serialization or XmlSerialization, both available in .NET. I would not look at Google's protocol buffers, because that RPC encoding has little advantage over what's already in .NET, but it is obscure, especially in the .NET world, and especially now. I wouldn't bet on it becoming mainstream for .net devs. As a result, you will only make your code harder to maintain by using this RPC encoding.

I don't really see the need for protobufs when the apps that are interconnecting are homogeneous, and are NOT on the scale of Google's datacenters. I also don't see the need even when heterogeneity is the rule, because we already have JSON and XML. They are both readable and serviceable, where protobufs are not.

In any case .NET has what you need for this, built-in.

Cheeso