Status
Not open for further replies.

tallboy20045

Right off the assembly line
Hi friends i just created desktop application through C#.net

i need help please

i want to transfer image file client to server. I have code and i also succeeded to do that. but main reason is that i am not getting image it is just file we can not preview of that.

in simple i send file from the client which received by client with same size but i can not view the image.

code is:
Sending from client
my format of send file is <IPADDRESS>|FILE|<file stream>
file read with :
moz-screenshot.jpg

mysend("FILE", File.ReadAllBytes(filePath));
public void mysend(string Command,byte[] fileStream)
{
try
{

IPAddress GroupAddress = IPAddress.Parse(ServerIP);

UdpClient sender = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress, 13001);

byte[] Head = Encoding.ASCII.GetBytes(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString() + "|" + Command + "|");
byte[] End = Encoding.ASCII.GetBytes("ENDFILE");
byte[] bytes = new byte[Head.Length + fileStream.Length + End.Length];
Head.CopyTo(bytes, 0);
fileStream.CopyTo(bytes, Head.Length);
End.CopyTo(bytes, (Head.Length + fileStream.Length) - 1);

sender.Send(bytes, bytes.Length, groupEP);
sender.Close();
}
catch (Exception e)
{
MessageBox.Show("ERROR \n" + e.ToString(), "Error while sending",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}

Server side receiver
private void GetImage(string ClientIp, string FileBytes)
{
if (CheckPCIp(ClientIp) == true)
{
int getElementNo= FileBytes.IndexOf("LE|", 0)+3;
int GetEndFile = FileBytes.IndexOf("ENDFILE");
string imgStr = FileBytes.Substring(getElementNo,GetEndFile-getElementNo);
//imgStr = imgStr.Replace("\0", "");
File.WriteAllText(Environment.CurrentDirectory + @"\client.png", imgStr);
/*try
{
BinaryWriter br = new BinaryWriter(File.Open("client.jpg", FileMode.OpenOrCreate));
br.Write(imgStr);
br.Close();
}
catch (Exception ex)
{
txtChatMain.AppendText(Environment.NewLine + "File Write Error :" + ex.Message);
}*/

}

}
 

QwertyManiac

Commander in Chief
Faults I noticed (Feel free to correct me, I can be wrong):

Code:
fileStream.CopyTo(bytes, [B]Head.Length[/B]);
End.CopyTo(bytes, [B](Head.Length + fileStream.Length) - 1[/B]);

-------

Why's a -1 needed? Are you replacing a byte?
Code:
string imgStr = FileBytes.Substring(getElementNo,[B]GetEndFile-getElementNo[/B]);

----------

If GetEndFile is the beginning index of ENDFILE, say its 19 (That makes the
entire size 25), and let the getElementNo be 9 (That includes +3),
then why would you want a substring of (9, [B]19-9[/B])?! You want (9, 19-2),
a.k.a no subtraction with getEndFile, use a constant.
P.s. There are better ways to send an image than doing this. You're almost converting and passing through image data as if it were to be read as ASCII. That's plain wrong.

P.p.s I don't know C#
 
OP
tallboy20045

tallboy20045

Right off the assembly line
in second code
i just fetch starting of file
if i have data send from the client like

192.168.0.1|FILE|PNG?ASDFASDfasdfasdfasdfasdf...............ENDFILE
so first ip
second says that it is file
then last file data

so i get the value (starting point of LE then i added value 3 for reach at file stream PNG?......

so

192.168.0.1|FILE|PNG?ASDFASDfasdfasdfasdfasdf...............ENDFILE

i
nt getElementNo= FileBytes.IndexOf("LE|", 0)+3; =17
int GetEndFile = FileBytes.IndexOf("ENDFILE"); =may be anything suppose 217

so final stream starts at
is it correct?
string imgStr = FileBytes.Substring(getElementNo,GetEndFile);
string imgStr = FileBytes.Substring(17,217-1);

I know this plan is wrong but we have no help for network programming.

so this is our thoughts to encode and decode more than one messages for client server

Thank you Very Much
 
Last edited:
Status
Not open for further replies.
Top Bottom