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 :
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);
}*/
}
}
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 :

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);
}*/
}
}