المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : problem with socket programing for image transformation



C# Programming
11-12-2009, 05:50 AM
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. http://www.barakasoft.com/script/Forums/Images/smiley_cry.gif
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
{
for (index = 0; index < dataBuffer.Length; index++)
{
if (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");

}
}


}
here is the code for client to recieve image:
if (clientSocket != null)
{
while(true)
{
while (true)
{
int counter = 0;
msr = new MemoryStream();
byte[] recv = new byte[128];
try
{
rc = clientSocket.Receive(recv, SocketFlags.None);
try
{
counter = Convert.ToInt32(System.Text.Encoding.ASCII.GetString(recv, 0, recv.Length));
}
catch { }
while (counter > 0)
{

rc = clientSocket.Receive(recv, SocketFlags.None);
counter--;
msr.Write(recv, 0, recv.Length);

}
}
catch (Exception e)
{
}
try
{
pictureBox1.Image = Image.FromStream(msr);
msr.Close();
}
catch(Exception e)
{
}
break;
}
}
}
else
{
StatusLabel1.Text = "Unable to establish connection to server!";
BtnDC_Click(null, null);
}

}
i beg you help me http://www.barakasoft.com/script/Forums/Images/smiley_cry.gif http://www.barakasoft.com/script/Forums/Images/smiley_sigh.gif :(