I have written this code to search for a string from a file. Since this program cannot be used for multiple clients, i was asked to make it more robust by using socket. I want to know where and how socket could be used in this program.. I'm still learning about sockets.. Couldn't exactly figure out where and how exactly it could be used.. Could someone just help me with it? I'm supposed to write the server side program.. I've also tried creating a tcp connection.. But i don know how and where exactly to imply my program into the first program.. This is my tcp connection program, followed by my actual program..
class Server
{
private TcpListener tcpListener;
private Thread listenThread;
public Server()
{
this.tcpListener = new TcpListener(IPAddress.Any, 3000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
break;
}
if (bytesRead == 0)
{
break;
}
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
}
tcpClient.Close();
}
}
}
class Program
{
static void Main(string[] args)
{
{
foreach (var word in new[] { "and", "so", "not", "c", "to", "by", "has", "do", "behavior", "dance", "france", "ok", "thast", "please", "hello", "system", "possible", "impossible", "absolutely", "sachin", "bradman", "schumacher", "http", "console", "application" })
{
var w = word;
new Thread(() => Console.WriteLine("{0}: {1}", w, text.Contains(w) ? "Present" : "Not Present")).Start();
}
}
else
Console.WriteLine("File Does not exist");
Console.ReadLine();
}
}