Right now I only need to be able to send a 100kb JPEG photo. I tried Google and found a couple of things but their code only threw exceptions so maybe what they've done is not appropriate in my situation.
This is an attempt at a remote screen viewer. If you're sharing your screen, I start a TcpListener on a random port and display the IP the viewer must use to connect to you. If you're a viewer, you type in the IP and port you want and it sends a request to connect by TcpClient.
That's all I've got to. Here's some code.
Connecting to a client:
private TcpClient MasterConnectionClient = new TcpClient();
private void ConnectButton_Click(object sender, EventArgs e)
{
string[] ConnectionInfo = ConnectToIPAddressBox.Text.Split(':');
try
{
MasterConnectionClient.Connect(IPAddress.Parse(ConnectionInfo[0]), int.Parse(ConnectionInfo[1]));
ViewportForm VPF = new ViewportForm();
this.Hide();
VPF.ShowDialog(); //Shows the form that will display the screenshot
}
catch (Exception E)
{
MessageBox.Show(E.Message, E.GetType().ToString());
}
Client waiting for connection
int Port = new Random().Next(1, 65535);
ClientSide.MasterListener = new TcpListener(IPAddress.Any, Port);
ConnectionAddressLabel.Text = GetExternalIP() + ":" + Port.ToString();
private void ShareButton_Click(object sender, EventArgs e)
{
CurrentOperationLabel.Text = "Waiting for connection...";
new Thread(new ThreadStart(
delegate
{
ClientSide.MasterListener.Start();
ClientSide.MasterSocket = ClientSide.MasterListener.AcceptSocket();
ScreenViewerIPLabel.Invoke(new ThreadStart(
delegate
{
ScreenViewerIPLabel.Text = ClientSide.MasterSocket.RemoteEndPoint.ToString();
}));
})).Start();
}
And this is what I've picked out from Google on getting and sending the screenshot. I don't understand line 12 and down. It throws an exception that says "A request to send or receive data was disallowed because the socket was not connected." ???
public static void SendScreenshot()
{
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save(@"C:\WINDOWS\Temp\RV\screentest.jpeg", ImageFormat.Jpeg);
byte[] fileNameByte = Encoding.ASCII.GetBytes("screentest.jpeG");
byte[] fileData = File.ReadAllBytes(@"C:\WINDOWS\Temp\RV\screentest.jpeg");
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
MasterSocket.Send(clientData);
}