Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have written this code to check for a particular string from a file. Right now it just checks it and gives the output on the server side output screen itself. I'm not sure how to send (the reply) back to the client. The server side program should have all the codes. It also accepts multiple clients. The main problem here is, the searchstring should be typed by the client. But i've written searchstring in the program itself. How do i make it that way?

The Procedure of this program is as follows-

Basically if a client wants to check if there's a particular string(word) in a file, he connects this code through a port on telnet. He types in the strings he wants to search(on telnet) and sends it to the server side. And this server side program, accepts the request and checks it for him from the file. And if it is present, it sends a message back to the client saying "The string is present in the file" And if it isn't, It should send a message saying "It is not".

This is where I've come till with a lot of help and tutorials. Can someone please help me?
EDITED - I have changed the code such that it sends a reply back to the client. All I need to know now is, how can I enable the client to search (type the word he wants to search for) through the client side(telnet)? Any help will be really appreciated. I have updated my code too.

C#
class Program
{
    static void Main(string[] args)
    {
        IPAddress ipad = IPAddress.Parse("127.0.0.1");
        TcpListener serversocket = new TcpListener(ipad, 8888);
        TcpClient clientsocket = default(TcpClient);
        Byte[] bytes = new Byte[256];
        serversocket.Start();

        Console.WriteLine(">> Server Started");
        while(true)
        {
            clientsocket = serversocket.AcceptTcpClient();
            Console.WriteLine("Accepted Connection From Client");

            LineMatcher lm = new LineMatcher(clientsocket);
            Thread thread = new Thread(new ThreadStart(lm.Run));
            thread.Start();
            Console.WriteLine("Client connected");
        }

        Console.WriteLine(" >> exit");
        Console.ReadLine();
        clientsocket.Close();
        serversocket.Stop();
    }
}

public class LineMatcher
{
    public string fileName = "c:/myfile2.txt";
    private TcpClient _client;

    public LineMatcher(TcpClient client)
    {
        _client = client;
    }

    public void Run()
{
    byte[] data = new byte[256];
    NetworkStream strm = _client.GetStream();
    try
    {
        using (var r = new StreamReader("c:/myfile2.txt"))
        {

            string line = "";
            bool done = false;

            int lineNumber = 0;
            String s = r.ReadToEnd();

            ASCIIEncoding encoder = new ASCIIEncoding();
            while (String.IsNullOrEmpty(s))
            {
                data = encoder.GetBytes("There is no data in the file.");
                Console.WriteLine("There is no data in the file.");
            }
            if (s.IndexOf("hello", StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                data = encoder.GetBytes("It is Present.");

            }
            else
            {
                data = encoder.GetBytes("It is not Present");
            }
        }
    }
    catch (Exception ex)
    {
        Console.Error.WriteLine(ex.ToString());
    }

    strm.Write(data, 0, data.Length);
    strm.Flush();
    Console.WriteLine("Closing client");
    _client.Close();
}
    }
Posted
Updated 17-Apr-12 18:46pm
v3
Comments
TheDarkCode 16-Apr-12 0:32am    
Can someone please help me out?

1 solution

I solved it.. :)
C#
namespace ServerSideAppln
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener socketListener = new TcpListener(8888);
            TcpClient netClient = default(TcpClient);
            StreamReader sr;
            StringBuilder sb = new StringBuilder();

            socketListener.Start();
            sr = new StreamReader("c:\\myfile2.txt");
            sb.Append(sr.ReadToEnd());
            while (true)
            {
                netClient = socketListener.AcceptTcpClient();
                Console.WriteLine("Accepted Connection From Client" + Environment.NewLine + "Client connected");
                ServerSide ss = new ServerSide();
                ss.startServerSide(netClient, sb);
            }
            socketListener.Stop();
        }
    }

    class ServerSide
    {

        TcpClient netClient;
        StringBuilder sb;

        public void startServerSide(TcpClient netClient, StringBuilder sb)
        {
            this.netClient = netClient;
            this.sb = sb;
            Thread thread = new Thread(processRequest);
            thread.Start();
        }
        private void processRequest()
        {
            byte[] data = new byte[4096];
            int bytesRead;
            NetworkStream strm = netClient.GetStream();
            bytesRead = 0;
                try
                {
                    
                    NetworkStream ns = netClient.GetStream();

                    string clientChar = "", s = "";
                    do
                    {
                        bytesRead = ns.Read(data, 0, (int)data.Length);
                        clientChar = Encoding.ASCII.GetString(data).Replace("\0", "");
                        s += clientChar;
                    } while (clientChar != Environment.NewLine);

                    s = s.Trim();

                    ASCIIEncoding encoder = new ASCIIEncoding();

                    if (String.IsNullOrEmpty(s))
                    {
                        data = encoder.GetBytes("There is no data in the file.");
                        Console.WriteLine("There is no data in the file.");
                    }
                    //if (sr.ToString().IndexOf("arjun", StringComparison.CurrentCultureIgnoreCase) >= 0)
                    if (sb.ToString().Contains(s))
                    {
                        data = encoder.GetBytes("It Is Present In The File.");
                    }
                    else
                    {
                        data = encoder.GetBytes("It Is Not Present In The File.");
                    }
                    strm.Write(data, 0, data.Length);
                    strm.Flush();
                    Console.WriteLine("Closing client");
                    netClient.Close();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900