Search code examples
c#socketsscreenshot

c# Sending screenshot in socket


I'm trying since a week to make screenshot from the clients pc and send it bij network to the server.

Well it is working a little bit now, but not completely. I tried several scripts i found on the internet but none of them worked as i wanted so i changed them, but with my little knowledge about socket i cant get it right. Could you guys help me?

The screenshot function works correct but when i send the stream to my server it only receives the top of the image(for example: the original screenshot made is 150kb but the file i receive is only 40kb) Try several other file sending scripts but by the most i only receive 6 bytes.

When i try to use a While function it sends me nothing or 1kb so i know I'm doing something wrong but what?

here's my Server code:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sock.Bind(ipEnd);
            sock.Listen(100);
            Socket clientSock = sock.Accept();

            byte[] clientData = new byte[1024 * 5000];
            string receivedPath = "C:/";

            int receivedBytesLen = clientSock.Receive(clientData);

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            konjo = "Client: " + clientSock.RemoteEndPoint + "connected & File: " + fileName + " started received.";

            BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            status = "File: " + fileName + "Received & Saved at path: " + receivedPath;

            bWrite.Close();
            clientSock.Close();
        }
        catch (Exception ex)
        {
            status = "Failed: " + ex.Message;
        }
    }

    private void bgWorker_Complete(object sender, RunWorkerCompletedEventArgs e)
    {
        label1.Text = status;

    }

Here's my client code:

        private void bgsender_work(object sender, DoWorkEventArgs e)
    {
        try
        {
            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));
            MemoryStream test = new MemoryStream();
            bmpScreenShot.Save(test, ImageFormat.Jpeg);
            bmpScreenShot.Save("C:\\Clienttest.jpeg", ImageFormat.Jpeg);

            IPAddress[] ipAddress = Dns.GetHostAddresses(Dns.GetHostName());
            IPEndPoint ipEnd = new IPEndPoint(ipAddress[2], 5656);
            Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


            string fileName = "screenshot.Jpeg";
            string filePath = "Your File Path";
            byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
            byte[] Data = test.ToArray();
            byte[] clientData = new byte[4 + fileNameByte.Length + Data.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            fileNameLen.CopyTo(clientData, 0);
            fileNameByte.CopyTo(clientData, 4);
            Data.CopyTo(clientData, 4 + fileNameByte.Length);

            clientSock.Connect("192.168.1.15", 5656);
            clientSock.Send(clientData);

            status = "File: " + fileName + " has been sent.";
            clientSock.Close();
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            status = "It failed: " + ex.Message;
        }

    }

    private void bgsender_complete(object sender, RunWorkerCompletedEventArgs e)
    {
        label1.Text = status;
    }

Solution

  • I wrote some sample code for a client-server application that seems to do what you require as an answer to another question.

    Take a look at: https://stackoverflow.com/a/5575287/381588 as it works and can likely be easily adapted to your requirements.