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 7698725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:18:33+00:00 2026-05-31T22:18:33+00:00

I have to write a program to check if any random string is present

  • 0

I have to write a program to check if any random string is present in a file. And i did so.. But now i was asked to use sockets.send and receive method. I’ve created a connection and written the code till here.. How do i proceed further? I’m not able to figure it out.. The first program is my try at server side program. And the second is my actual program to search for a string from the file. Could someone help me with the code on how to use the sockets in my actual program? Thanks alot.. 🙂

class Program
{
    static void Main(string[] args)
    {
        TcpListener serversocket = new TcpListener(8888);
        int requestcount = 0;
        TcpClient clientsocket = default(TcpClient);
        serversocket.Start();
        Console.WriteLine(">> Server Started");
        clientsocket = serversocket.AcceptTcpClient();
        Console.WriteLine("Accept Connection From Client");
        requestcount = 0;


        while ((true))
        {
            try
            {
                requestcount = requestcount + 1;
                NetworkStream networkstream = clientsocket.GetStream();
                byte[] bytesFrom = new byte[10025];
                networkstream.Read(bytesFrom, 0, (int)clientsocket.ReceiveBufferSize);
                string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                Console.WriteLine(" >> Data from client - " + dataFromClient);
                string serverResponse = "Server response " + Convert.ToString(requestcount);
                Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                networkstream.Write(sendBytes, 0, sendBytes.Length);
                networkstream.Flush();
                Console.WriteLine(" >> " + serverResponse);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        clientsocket.Close();
        serversocket.Stop();
        Console.WriteLine(" >> exit");
        Console.ReadLine();
       }
  } 

This is the program i want to use in the above program.

class Program
{

    static void Main(string[] args)
    {
        if (File.Exists("C://myfile2.txt"))
        {
            var text = File.ReadAllText("C://myfile2.txt");


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

}
  • 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-05-31T22:18:35+00:00Added an answer on May 31, 2026 at 10:18 pm

    Here is a quick and dirty idea that I wrote without an IDE (—I haven’t tested it— Edit just tested it with netcat, and it works fine):

    • Note it uses a regular expression. If the lookup table for words grows sufficiently large, you’d be better of storing the words in a HashSet<string> and splitting the input into words. You can then efficiently do a .IntersectWith to see whether any of the words are matched.

    • Note that the socket’s constructor is deprecated (you are supposed to explicitely specify and IPAddress to bind to)

    • Your original code doesn’t require matches to be separate words (candy matches both c and and). You might want to fix that

    • The parts that were inefficient in the original ‘grep’ snippet:

      • ReadAllText (won’t scale for large files)
      • Doing multiple .Contains calls in a loop will be far less efficient than using a (precompiled) regular expression
      • Why on earth are threads being created there? This will literally only add runtime overhead and potentially lead to problems due to the unsynchronized access to Console.Out.

    .

    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Net.Sockets;
    
    class Program
    {
        private static Regex _regex = new Regex("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", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
    
        static void Main(string[] args)
        {
            TcpListener serversocket = new TcpListener(8888);
            TcpClient clientsocket = default(TcpClient);
            serversocket.Start();
            Console.WriteLine(">> Server Started");
            clientsocket = serversocket.AcceptTcpClient();
            Console.WriteLine("Accept Connection From Client");
    
            try
            {
                using (var reader = new StreamReader(clientsocket.GetStream()))
                {
                    string line;
                    int lineNumber = 0;
                    while (null != (line = reader.ReadLine()))
                    {
                        lineNumber += 1;
                        foreach (Match match in _regex.Matches(line))
                        {
                            Console.WriteLine("Line {0} matches {1}", lineNumber, match.Value);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }
    
            clientsocket.Close();
            serversocket.Stop();
            Console.WriteLine(" >> exit");
            Console.ReadLine();
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple stock check program, and I have a Table
I have to write a program that read from a file that contains the
I have been asked to write a program using python for an assignment. I
I have been asked to write a java program on linux platform. According to
I have a program that wants to check if a file has been modified.
I have a bash program that will write to an output file. This file
I have to use write a program in which the dictionary should be used
I have been asked to write a (very) simple program for a set of
I have to write a program that sniffs network packets (part1-the simple part). And
I have been assigned wit the task to write a program that takes a

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.