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();
}
}
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.IntersectWithto 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 (
candymatches bothcandand). You might want to fix thatThe parts that were inefficient in the original ‘grep’ snippet:
.Containscalls in a loop will be far less efficient than using a (precompiled) regular expressionConsole.Out..