Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 25-11-2008, 04:20 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: May 2008
Location: Navi Mumbai
Posts: 3
Exclamation C#


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 :[IMG]file:///C:/DOCUME%7E1/ADMINI%7E1/LOCALS%7E1/Temp/moz-screenshot.jpg[/IMG]
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.GetHo stName()).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);
}*/

}

}
tallboy20045 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-11-2008, 07:02 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: C#

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

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

-------

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

----------

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, 19-9)?! 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#
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 01-12-2008, 05:37 PM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: May 2008
Location: Navi Mumbai
Posts: 3
Default Re: C#

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...............ENDFI LE
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...............ENDFI LE

i
Quote:
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?
Quote:
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 by tallboy20045; 01-12-2008 at 05:43 PM. Reason: Automerged Doublepost
tallboy20045 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:00 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2