Witam wszystkich serdecznie.
Pożyczyłem książke 'C# Tworzenie aplikacji sieciowych. 101 gotowych projektów.' i chciałem popróbować kilku z podanych aplikacji. Problem pojawił się przy programie z tytułu - zdalnym screenshocie. Programista ze mnie żaden, chciałbym się tylko pobawić programami z książki.
Problem polega na tym, że nic sie nie dzieje. Absolutnie nic. Serwer nie wysyła rządania, klient nie odpowiada. WireShark nie wykrył żadnego ruchu na podanym porcie/IP czy było to sprawdzane na dwóch komputerach czy jednym, bez znaczenia. Poniżej kody programów. Prosiłbym o pomoc.
serwer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace screen_serwer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void SetTextCallBack(string tekst);
private void SetText(string tekst)
{
if (listBox1.InvokeRequired)
{
SetTextCallBack f = new SetTextCallBack(SetText);
this.Invoke(f, new object[] { tekst });
}
else
{
this.listBox1.Items.Add(tekst);
}
}
delegate void RemoveTextCallBack(int pozycja);
private void RemoveText(int pozycja)
{
if (listBox1.InvokeRequired)
{
RemoveTextCallBack f = new RemoveTextCallBack(RemoveText);
this.Invoke(f, new object[] { pozycja });
}
else
{
listBox1.Items.RemoveAt(pozycja);
}
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
IPEndPoint zdalnyIP = new IPEndPoint(IPAddress.Any, 0);
UdpClient klient = new UdpClient(43210);
while (true)
{
Byte[] bufor = klient.Receive(ref zdalnyIP);
string dane = Encoding.ASCII.GetString(bufor);
string[] cmd = dane.Split(new char[] { ':' });
if (cmd[1] == "HI")
{
foreach (string wpis in listBox1.Items)
if (wpis == cmd[0])
{
MessageBox.Show("Proba nawiazania polaczenia z " + cmd[0] + " odrzucona, poniewaz na liscie istnieje juz taki wpis");
return;
}
this.SetText(cmd[0]);
}
if (cmd[1] == "BYE")
{
for (int i = 0; i < listBox1.Items.Count; i++)
if (listBox1.Items[i].ToString() == cmd[0])
this.RemoveText(i);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
return;
try
{
TcpClient klient = new TcpClient(listBox1.Items[listBox1.SelectedIndex].ToString(), 1978);
NetworkStream ns = klient.GetStream();
byte[] bufor = new byte[5];
bufor = Encoding.ASCII.GetBytes("##S##");
ns.Write(bufor, 0, bufor.Length);
if (backgroundWorker1.IsBusy == false)
backgroundWorker1.RunWorkerAsync();
else
MessageBox.Show("Blad: nie mozna nawiazac polaczenia");
}
catch
{
MessageBox.Show("dkk");
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
TcpListener serwer2 = new TcpListener(IPAddress.Parse(textBox1.Text), (int)numericUpDown1.Value);
serwer2.Start();
TcpClient klient2 = serwer2.AcceptTcpClient();
NetworkStream ns = klient2.GetStream();
byte[] obrazByte;
using (BinaryReader odczytObrazu = new BinaryReader(ns))
{
int romiarObrazu = odczytObrazu.ReadInt32();
obrazByte = odczytObrazu.ReadBytes(romiarObrazu);
}
using (MemoryStream ms = new MemoryStream(obrazByte))
{
Image obraz = Image.FromStream(ms);
pictureBox1.Image = obraz;
}
serwer2.Stop();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
klient
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace screen_klient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IPHostEntry adresyIP = Dns.GetHostEntry(Dns.GetHostName());
adresLokalnyIP = adresyIP.AddressList[0].ToString();
backgroundWorker1.RunWorkerAsync();
}
private Bitmap wykonajScreenshot()
{
Bitmap bitmapa = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics screenshot = Graphics.FromImage(bitmapa);
screenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
return bitmapa;
}
private int serwerKomendPort = 1978;
private IPAddress serwerDanychIP = IPAddress.Parse("uzupełnic"); // UZUPELNIC O ip
private int serwerDanychPort = 25000;
private string adresLokalnyIP = null;
private Bitmap obraz;
delegate void SetTextCallBack(string tekst);
private void SetText(string tekst)
{
if (listBox1.InvokeRequired)
{
SetTextCallBack f = new SetTextCallBack(SetText);
this.Invoke(f, new object[] { tekst });
}
else
{
this.listBox1.Items.Add(tekst);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
TcpListener serwer = new TcpListener(IPAddress.Parse(adresLokalnyIP), serwerKomendPort);
serwer.Start();
this.SetText("What is thy bidding my Master?");
while (true)
{
TcpClient klientKomend = serwer.AcceptTcpClient();
this.SetText("Order Recieved");
NetworkStream ns = klientKomend.GetStream();
Byte[] bufor = new Byte[5];
int odczyt = ns.Read(bufor, 0, bufor.Length);
string s = Encoding.ASCII.GetString(bufor);
string wiadomosc = Encoding.ASCII.GetString(bufor);
if (wiadomosc == "##S##")
{
this.SetText("SCV go to work, sir");
obraz = wykonajScreenshot();
MemoryStream ms = new MemoryStream();
obraz.Save(ms, ImageFormat.Jpeg);
Byte[] obrazByte = ms.GetBuffer();
ms.Close();
try
{
TcpClient klient2 = new TcpClient(serwerDanychIP.ToString(), serwerDanychPort);
NetworkStream ns2 = klient2.GetStream();
this.SetText("Sending TIE Fighters");
using (BinaryWriter bw = new BinaryWriter(ns2))
{
bw.Write((int)obrazByte.Length);
bw.Write(obrazByte);
}
this.SetText("Building Ready!");
}
catch (Exception ex)
{
this.SetText("Unable to comply");
}
}
}
}
private void wyslijWiadomoscUDP(string wiadomosc)
{
UdpClient klient = new UdpClient(serwerDanychIP.ToString(), 43210);
byte[] bufor = Encoding.ASCII.GetBytes(wiadomosc);
klient.Send(bufor, bufor.Length);
klient.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
wyslijWiadomoscUDP(adresLokalnyIP + ":HI");
}
private void Form1_Load(object sender, FormClosingEventArgs e)
{
wyslijWiadomoscUDP(adresLokalnyIP + ":BYE");
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Żeby nie było, miejsce gdzie jest wpis 'uzupelnic' było uzupełniane podczas prób działania. Proszę też nie zwracać uwagi na pola wewnątrz nawiasów SetText, miało być troche humorystycznie ;-)
Bardzo proszę o pomoc w doprowadzeniu tego do działania.