Send image


  • Hello people, I currently got a piece of code that send an image through tcp, this seems to work on any pc that is connected to my own network but when people connect to my real IP nothing seems to happen, here is the code.


     public Form1()
          var t = new Thread(startListening);
          t.IsBackground = true;
        public void startListening()
          check = true; 
            while (i < 1)
              data = new byte[1024];
              ipep = new IPEndPoint(IPAddress.Any, 9050);
              newsock = new Socket(AddressFamily.InterNetwork,
                      SocketType.Stream, ProtocolType.Tcp);
              client = newsock.Accept();
              IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
              i = 1;
            while (check)
              data = ReceiveVarData(client);
              MemoryStream ms = new MemoryStream(data);
              Image bmp = Image.FromStream(ms);
              pictureBox1.Image = bmp;
          catch { }
        private byte[] ReceiveVarData(Socket s)
          int total = 0;
          int recv;
          byte[] datasize = new byte[4];
          recv = s.Receive(datasize, 0, 4, 0);
          int size = BitConverter.ToInt32(datasize, 0);
          int dataleft = size;
          byte[] data = new byte[size];
          while (total < size)
            recv = s.Receive(data, total, dataleft, 0);
            if (recv == 0)
            total += recv;
            dataleft -= recv;
          if (recv == total)
            check = false;
            i = 0;
          return data;


    private static int SendVarData(Socket s, byte[] data)
          int total = 0;
          int size = data.Length;
          int dataleft = size;
          int sent;
          byte[] datasize = new byte[4];
          datasize = BitConverter.GetBytes(size);
          sent = s.Send(datasize);
          while (total < size)
            sent = s.Send(data, total, dataleft, SocketFlags.None);
            total += sent;
            dataleft -= sent;
          return total;
        public bool ThumbnailCallback()
          return true;
        private void timer1_Tick(object sender, EventArgs e)
            //int getimageint = 0;
            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:\\mytest.jpg", ImageFormat.Jpeg);
            //Image OriginalImage;
            //OriginalImage = System.Drawing.Image.FromFile(@"c:\\mytest" + getimageint + ".jpg");
            //Image thumbnailImage;
            // thumbnailImage = OriginalImage.GetThumbnailImage(800, 600, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            //thumbnailImage.Save("c:\\mytest" +(getimageint+1) +".jpg");
            byte[] data = new byte[1024];
            int sent;
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(""), 9050);
            Socket server = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
            Bitmap bmp = new Bitmap(@"c:\\mytest.jpg");
            MemoryStream ms = new MemoryStream();
            // Save to memory using the Jpeg format
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            // read to end
            byte[] bmpBytes = ms.ToArray();
            sent = SendVarData(server, bmpBytes);
          catch { }

    Ive asked the person I tested it with to forward the port that the application uses on his router and supposedly there is no firewall blocking the connection either, any ideas?


    Thursday, September 23, 2010 10:29 PM

All replies

  • TCP/IP debugging fundamentals suggest that you put a network sniffer onto the network.  The reason is this (given the following).

    Client---->RouterA---> [Internet] <---RouterB<----Host

    You have these possbile scenarios:

    There could be client restrictions on client machine, RouterA could be a part of a Firewall/Restricted environment,  The Internet itself could be the issue (no proper DNS entries, or improper routing type issues), then this is all mirrored on the Host side.

    Because you say "it worked intranet"  you problable had this topology..

    ClientA ---------> ClientB  (on same Class C network, involving no routers).  This proves to you that your code works!!!! Which is the good news. 

    Now you have to find the problem with the "Network" you put your code into.  Download Wireshark and start sniffing the network.  This is the only sure way to pinpoint the EXACT cause.

    Friday, September 24, 2010 2:28 PM
  • Thanks, ill check that out and report back here.
    Friday, September 24, 2010 4:11 PM
  • It's pretty simple once you get Wireshark into place you're simply looking for the start of an outbound request

    ClientA ------> Syn

    Then you should see a repsonse from Host

    Syn Ack <-------- Host

    Followed by a client ackknowledgement

    Client ACK --------> Host

    If these three things don't happen you won't be able to go further.

    I am guessing that you are not seeing the Syn Ack from the host.

    Friday, September 24, 2010 8:55 PM
  • If what you say is indeed the case, what do you mean i cant go further. Certainly it must be possible to send a screenshot through a tcp connection, how would programs like teamviewer work then for example?



    Friday, September 24, 2010 9:54 PM
  • You cannot go farther if nothing is making it to destination, what does trace show?
    Friday, September 24, 2010 10:42 PM
  • Unfortunately I wont have the oppurtunity to check until tomorrow since its 1 am here. On another note, would it be possible to do this with udp?
    Friday, September 24, 2010 11:07 PM