Reputation: 628
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();
}
}
Upvotes: 1
Views: 356
Reputation: 394054
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:
.Contains
calls in a loop will be far less efficient than using a (precompiled) regular expressionConsole.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();
}
}
Upvotes: 2