Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem with socket programing for image transformation

problem with socket programing for image transformation

Scheduled Pinned Locked Moved C#
helpdatabaseperformance
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mehrdad333
    wrote on last edited by
    #1

    hi at first excuse me because of my poor english i have written a program for sending an image or for video conference via internet with sockets and i am using tcp protocols my problem is that when i test program on my computer it works fine but when i test it over internet with between two pcs the reciver cant recieve image correctly minwhile i am using dial up connection i dont know where is the problem perhaps it is because of low speed of internet i changed the buffer size to 512 1024 4096 and it didnt change. or i maybe should compress or encode image i dont know .please please help me with this problem. :(( here is the code for sending an image : private void button2_Click(object sender, EventArgs e) { byte[] bytes; using (MemoryStream ms = new MemoryStream()) { pictureBox1.Image.Save(ms,ImageFormat.Jpeg); pictureBox2.Image = pictureBox1.Image; bytes = ms.ToArray(); ms.Close(); } startcapturing(bytes); } private void startcapturing(byte[] bytes) { int i = 0; byte[] dataBuffer = new byte[128]; int index = 0; byte[] loop = new byte[128]; loop = System.Text.Encoding.ASCII.GetBytes(((bytes.Length / 128) + 1).ToString()); label4.Text = ((bytes.Length / 128) + 1).ToString(); if (serverSocket != null) { try { rc = clientSocket.Send(loop, SocketFlags.None); while(i= bytes.Length) { dataBuffer[index] = 0; } else { dataBuffer[index] = bytes[i]; i++; } } rc = lientSocket.Send(dataBuffer,SocketFlags.None); } } catch (Exception err) { MessageBox.Show(err.Message.ToString()+"here"); } }

    P 1 Reply Last reply
    0
    • M mehrdad333

      hi at first excuse me because of my poor english i have written a program for sending an image or for video conference via internet with sockets and i am using tcp protocols my problem is that when i test program on my computer it works fine but when i test it over internet with between two pcs the reciver cant recieve image correctly minwhile i am using dial up connection i dont know where is the problem perhaps it is because of low speed of internet i changed the buffer size to 512 1024 4096 and it didnt change. or i maybe should compress or encode image i dont know .please please help me with this problem. :(( here is the code for sending an image : private void button2_Click(object sender, EventArgs e) { byte[] bytes; using (MemoryStream ms = new MemoryStream()) { pictureBox1.Image.Save(ms,ImageFormat.Jpeg); pictureBox2.Image = pictureBox1.Image; bytes = ms.ToArray(); ms.Close(); } startcapturing(bytes); } private void startcapturing(byte[] bytes) { int i = 0; byte[] dataBuffer = new byte[128]; int index = 0; byte[] loop = new byte[128]; loop = System.Text.Encoding.ASCII.GetBytes(((bytes.Length / 128) + 1).ToString()); label4.Text = ((bytes.Length / 128) + 1).ToString(); if (serverSocket != null) { try { rc = clientSocket.Send(loop, SocketFlags.None); while(i= bytes.Length) { dataBuffer[index] = 0; } else { dataBuffer[index] = bytes[i]; i++; } } rc = lientSocket.Send(dataBuffer,SocketFlags.None); } } catch (Exception err) { MessageBox.Show(err.Message.ToString()+"here"); } }

      P Offline
      P Offline
      Paulo Zemek
      wrote on last edited by
      #2

      I don't understand the full code, but I must say the problem is probably with the send or the Receive. I am not sure if the Send can send partial data, but receive surelly do. For example: You send 128 bytes. The connection is local, you get 128 bytes. But, if the connection is remote, you can: Receive 10 bytes. Then, receive 20 bytes. + 10 bytes... until you finally receive all the buffer (the 128 you expected). So, you must check this. Also, I think a better approach is: Instead of writing everything to a memorystream and them sending the memorystream buffer, get the socket stream (socket.GetStream()) and save the image directly to the socket stream. Then, on the other side, call Image.FromStream(socket.GetStream())

      M 1 Reply Last reply
      0
      • P Paulo Zemek

        I don't understand the full code, but I must say the problem is probably with the send or the Receive. I am not sure if the Send can send partial data, but receive surelly do. For example: You send 128 bytes. The connection is local, you get 128 bytes. But, if the connection is remote, you can: Receive 10 bytes. Then, receive 20 bytes. + 10 bytes... until you finally receive all the buffer (the 128 you expected). So, you must check this. Also, I think a better approach is: Instead of writing everything to a memorystream and them sending the memorystream buffer, get the socket stream (socket.GetStream()) and save the image directly to the socket stream. Then, on the other side, call Image.FromStream(socket.GetStream())

        M Offline
        M Offline
        mehrdad333
        wrote on last edited by
        #3

        thanks sockets doesnt have Getstream() methode only tcp client has it please if you guide me write more cause i am new to c# thanks in advance

        P 1 Reply Last reply
        0
        • M mehrdad333

          thanks sockets doesnt have Getstream() methode only tcp client has it please if you guide me write more cause i am new to c# thanks in advance

          P Offline
          P Offline
          Paulo Zemek
          wrote on last edited by
          #4

          Well... I usually use sockets by TcpClient in C#... but, the same way, I am not sure about send, but when reading, you must do something like this: Instead of read(buffer, 0, 128) you set: int totalRead = 0 int count = 128; while(count > 0) { int actualRead = socket.Read(buffer, totalRead, count); if (actualRead <= 0) throw new Exception("Connection lost"); totalRead += actualRead; count -= actualRead; }

          M 1 Reply Last reply
          0
          • P Paulo Zemek

            Well... I usually use sockets by TcpClient in C#... but, the same way, I am not sure about send, but when reading, you must do something like this: Instead of read(buffer, 0, 128) you set: int totalRead = 0 int count = 128; while(count > 0) { int actualRead = socket.Read(buffer, totalRead, count); if (actualRead <= 0) throw new Exception("Connection lost"); totalRead += actualRead; count -= actualRead; }

            M Offline
            M Offline
            mehrdad333
            wrote on last edited by
            #5

            ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh :omg: :(( :(( :(( oh my god you solved my problem oh my god thank you thank you thanks please please let me kiss your foot :(( :(( i wont forget your name paulo zemek i wont forget you thanks thanks a milion thanks a bunch let me let me kiss your foot :(( :(( :(( :(( :(( :(( i am really crying here :(( :(( :(( :(( thanks thanks .................................. ................................................ ................................................. .................................................. ...................................................

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups