'How to activate a clients background worker from a server
So I am writing a C# program where the server will send a signal to the client to run a certain background worker that is in the client. If the user types g on the server it will launch the google search background worker, if y will launch the yahoo search banckground worker, etc. I am wondering how to code so that if the server sends "G" it will iniate the clients googlesearch background worker?
Client code:
Public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SL_Click(object sender, EventArgs e)
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
MessageBox.Show("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));
googlesearch.RunWorkerAsync();
tcpclnt.Close();
}
catch (Exception)
{
MessageBox.Show("Could not find server computer.");
}
}
private void RecieveIP_TextChanged(object sender, EventArgs e)
{
}
private void googlesearch_DoWork(object sender, DoWorkEventArgs e)
{
{
MessageBox.Show("Random google search will be conducted roughly every 1 minute");
int seconds = 1 * 60000;
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
aTimer.Interval = seconds;
aTimer.Enabled = true;
}
static void TimerMethod(object source, ElapsedEventArgs e)
{
string uriString = "http://www.google.com/search";
WebClient webClient = new WebClient();
Random r = new Random();
string[] words = { "man", "rat", "cow", "chicken", "confuse", "cool", "space news", "science", "holiday", "chickens", "travel", "europoe", "USA", "president", "cool stuff", "world news", "donald trump", "politics", "space", "astronomy", "radio", "cool stuff", "USA News", "tell me a funny joke", "do a barrel rool", "mario and luigi", "radio", "abcdefghijklomnopqrstuvwxyz", "popular computer games", "graphics cards", "performance", "sports news", };
Console.WriteLine(words[r.Next(0, words.Length)]);
string word = words[r.Next(0, words.Length)];
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection.Add(uriString, word);
webClient.QueryString.Add(nameValueCollection);
var googlesearch = new ProcessStartInfo
{
FileName = "https://www.google.com/search?q=" + word,
UseShellExecute = true
};
Process.Start(googlesearch);
}
}
Server code:
public partial class HiveMind : Form
{
public HiveMind()
{
InitializeComponent();
}
private void Connect_Click(object sender, EventArgs e)
{
{
try
{
IPAddress ipAd = IPAddress.Parse(ipv4.Text); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
MessageBox.Show("The server is running at port 8001...");
MessageBox.Show("The local End point is :" + myList.LocalEndpoint);
MessageBox.Show("Looking for other computer");
Socket s = myList.AcceptSocket();
Console.WriteLine("Found buddy " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception)
{
MessageBox.Show("Could not find other computer.");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|