Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:02:55+00:00 2026-06-02T13:02:55+00:00

I have written this code to check for a particular string from a file.

  • 0

I have written this code to check for a particular string from a file. Right now it checks for the string. But how can I send the reply back saying “it is present” to the client? The server side program should have all the codes. It also accepts multiple clients.

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 send it to the server side. And this server side program 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”.

The search string(“hello”) is in this program. How can I enable the client to search for it from client side(telnet)?
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.

Thank you.

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();
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:02:59+00:00Added an answer on June 2, 2026 at 1:02 pm

    I solved it. 🙂 This is how I did it. Any suggestions on improving it?

    namespace ServerSideApplication
    {
        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:\\test.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 (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());
                }
        }
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code: public void reloadDefConfig(File filepath, String fileInJar) { File configFile
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
In my code I have written this but it fails to compile: In Class1.h:
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have written below code to check for blank value in my textbox but
I have written this code to fire a http request through a proxy. But
I have written this code in javascript however I have to make it work
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written grammar for a language (sample code below) //this is a procedure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.