i am building a chat and i have this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    class Program
    {
   

        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 5000);
            server.Start();
            Console.WriteLine("Server started");
            int a = 0;

          

            while (true)
            {
                TcpClient connection = server.AcceptTcpClient();
                Console.WriteLine("connection accepted");
                //TcpClient[] client = new TcpClient[100];
                //client[a++] = connection;
                
             

                ThreadPool.QueueUserWorkItem(ProssecClient, connection);
            }
        }

        

        public static void ProssecClient(object o)
        {
            TcpClient connection = o as TcpClient;
            if (connection == null)
                return;
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
            string word = "";
  
       

            try
            {
                while (true)
                {
                    savedObject saved = new savedObject();
                    //sw.WriteLine(sr.ReadLine());
                    word = sr.ReadLine();
                
                    sw.WriteLine(saved.AllWords(word));
                 
                    sw.Flush();


                  
                    Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
                    BinaryFormatter bformatter = new BinaryFormatter();

                    Console.WriteLine("Writing words Information");
                    bformatter.Serialize(stream, saved);
                    stream.Close();
                }
            }
            catch
            { 
                Console.WriteLine("client left"); 
            
            }
        }

      
    }
}

[Serializable()] 
class savedObject :  ISerializable 
{
   public string allwords;

    public string AllWords(string words)
    {
        allwords += words + " ";

        return allwords;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {

        info.AddValue("RetrievedWord", allwords);

    }
   

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        allwords = (String)info.GetValue("RetrievedWord", typeof(string));

    }

   
}

i wrote some code to serialize the object. The goal is to receive input from the user, that input will be passed to the savedObject object, once it is there, the object needs to be serialized (saved). That saved object should pass to every user that connects..
(This runs in a loop , basically.

My problem is that i dont know how to send the object to the current user.

I don't understand what you mean by send the serialised object to the current user?

The serialised object should be represented as a byte stream. In answer to the title of the thread, to send it over the send, you simply send these bytes over a Socket connection.

MSDN Article

how do i send those bytes to another socket to be read?

i am thinking of saving all the inputs of the user connected to a file and then break it down to bytes and send them..

but i am not sure of the class that deals with that


File.Open("EmployeeInfo.osl", FileMode.Create);

i am thinking of sending the file that i created to the user..

after the object gets serialized, i want to add an object that sends the serialized object to the client

Please read the article on sockets, it will tell you what you need to know for network transmission.

You can read a file a byte at a time if you need to. There is a method to do so in the StreamReader object.

i need to use StreamWriter object to write the file to the user.. but i dont know the convention of doing so.

i am thinking of passing the object as such:

sw.WriteLine(saved);

the object is deserialized, the compiler doesnt show a mistake. but what does the client has to have to read this?

I'm not sure what you're trying to achieve any more. What you're doing is correct, it will save that to a file to whatever file you want. But to get that file across the network, you need to use a socket on both sides.

Please read the article I sent you. It explains everything you need to do.

i can receive information from the server to the client with no problem.

i have a problem when there are multiple users.

As you can see in my loop. it only reads and retrieves.

Here is my modified server code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    class Program
    {
   

        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 5000);
            server.Start();
            Console.WriteLine("Server started");
            int a = 0;
            //Deserialize
            //if (File.Exists("EmployeeInfo.osl"))
            //{
            //    stream = File.Open("EmployeeInfo.osl", FileMode.Open);
            //    bformatter = new BinaryFormatter();

            //    saved = (savedObject)bformatter.Deserialize(stream);
            //    stream.Close();
            //}

            //Serialize
            //bformatter.Serialize(stream, saved);
            //stream.Close();
            while (true)
            {
                TcpClient connection = server.AcceptTcpClient();
                Console.WriteLine("connection accepted");
                //TcpClient[] client = new TcpClient[100];
                //client[a++] = connection;
                
             

                ThreadPool.QueueUserWorkItem(ProssecClient, connection);
            }
        }

        

        public static void ProssecClient(object o)
        {
            TcpClient connection = o as TcpClient;
            if (connection == null)
                return;
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
            string word = "";
            //Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();
            savedObject saved = new savedObject();

            try
            {
                while (true)
                {
                    //saved = null;

                    //Open the file written above and read values from it.
                    //Deserialize
                 
                    
                    
                  
                    //Write a word
                   
                
                    sw.WriteLine(sr.ReadLine());
                 
                    sw.Flush();


                    
                    //serialize.
              
                  


                    //sw.WriteLine(saved);
                
                }
            }
            catch
            { 
                Console.WriteLine("client left"); 
            
            }
        }

      
    }
}

My goal is transfer objects between the server and the user.

i create an object on the users side with everything he types!. then i try to save the object with serialization...

For now, thats all what i am trying to do.

next i might send the serialized object to the server to be taken by a new user that logs in or be read by the server...


here is the client side code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
            savedObject saved = new savedObject();
            Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();
            string word = "";
            string allwords = "";
            while (true)
            {
                word = Console.ReadLine();
                allwords = saved.AllWords(word);
                sw.WriteLine(allwords);
                sw.Flush();
                Console.WriteLine(sr.ReadLine());


                //Serialize
                //bformatter.Serialize(stream, saved);
                //stream.Close();

                //sw.WriteLine(saved);
            }

               
        }


      
    }
}



[Serializable()]
class savedObject : ISerializable
{
    public string allwords;

    public string AllWords(string words)
    {
        allwords += words + " ";

        return allwords;
    }

    public void Words(SerializationInfo info, StreamingContext ctxt)
    {

        info.AddValue("RetrievedWord", allwords);

    }


    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        allwords = (String)info.GetValue("RetrievedWord", typeof(string));

    }


}

what i want to send is this object ----- "saved" (class savedObject) and pass it to the server.

Thats all.

i thought to use serializaiton (i read about serialization this morning). but i am not sure how to send the serialized object and how to open it to be read with all the info in it

Serialisation allows you to take an object and turn it into it's binary/byte representation so you can store it in a file.

So if you serialise an object, you have the bytes you need to send already.

The article I linked, provides an example of how to send and receive information between clients.

You have all the information you need already, you just need to read it properly.

so if i send a serialized object to the server will it return it to the user..
thats all i need to know.

can i do this.

StreamWriter sr=new StreamWriter();

sr.WriteLine(SerializedObject);

and send it to the server to read?

will the server read it? and be able to retrieve it?

i run a thread on the client side to send and receive information.

but i need more information on how to use the binaryFormattar

send the class as XML:

Serializer is of type System.Xml.Serialization.XmlSerializer

System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;

Note the 'this' reference - it acts on the class this exists in; in a function

memoryStream = new System.IO.MemoryStream();
Serializer.Serialize(memoryStream, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();

Don't forget to dispose of all readers!

Now...receive the class - get xml string from transport:

System.IO.StringReader stringReader = null;

stringReader = new System.IO.StringReader(xml);
return (([CLASSTYPE])(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));

commented: #Thank you +0

alright, thanks i will save the code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.