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.