Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • Help me fix a type of variable in program send a recieve fil
AuthorQuestion
Help me fix a type of variable in program send a recieve fil
Posted on: 17 Dec 2012
Hi everybody, I have write a program send and recieve a large file , now it can transfer a file size 1.5G but when I choose  large file about 2G it fail and notify:
http://i1055.photobucket.com/albums/s505/vn_photo/Untitled-27_zpscf61b7a1.jpg

Here is Project:

http://www.mediafire.com/?p9b2xj2970d7cqb

Please read and help me, it code is short, no long, I need a help I'm nearing to send it for my teacher, I hope everyone will help me.

AuthorReply
Re: Help me fix a type of variable in program send a recieve fil
Posted on: 17 Dec 2012  
Hi Maria,

As shown in the image, you are going to Convert a Int64 to Int32. It is not possible. As I can understand you declared a variable as Int32(int), change it to Int64(long).

Re: Help me fix a type of variable in program send a recieve fil
Posted on: 18 Dec 2012  
Thank very much for instruction, I have replace and change some variable int to long as you guide but it get two error here, please repaired it for me.

http://i1055.photobucket.com/albums/s505/vn_photo/Untitled-27_zpseed8a6d3.jpg

Here is code:
namespace server
{
  public partial class Form1 : Form
  {
  public delegate void UpdateListBoxCallBack(string s);
  public delegate void UpdateLabelCallBack(string s);
  public delegate void Updatepgdstatus(long s, long p);
  private NetworkStream nwkStream = null;
  private Socket socketForClient = null;
  private TcpListener tcpListener = null;
  private Stream stmReader = null;
  private Stream stmWriter = null;
  private Thread t = null;

  public Form1()
  {
  Control.CheckForIllegalCrossThreadCalls = false;
  InitializeComponent();
  IPHostEntry ips = Dns.GetHostByName(Dns.GetHostName());
  ipServer.Text = ips.AddressList[0].ToString();
  label5.Text = null;
  }

  private void btnBrowse_Click(object sender, EventArgs e)
  {
  txtFileName.Clear();
  OpenFileDialog fileDialog = new OpenFileDialog();
  fileDialog.Title = "Choose file to send";
  fileDialog.Filter = "Text Files (*.txt)|*.txt|Word Documents" + "(*.doc)|*.doc|All Files (*.*)|*.*";
  fileDialog.Multiselect = true;
  fileDialog.ShowReadOnly = true;
  if (fileDialog.ShowDialog(this) == DialogResult.OK)
  {
  txtFileName.Text = fileDialog.FileName;
  lblMessage.Text = null;
  }
  }

  private void btnStart_Click(object sender, EventArgs e)
  {
  t = new Thread(new ThreadStart(startSever));
  txtFileName.Clear();
  lblMessage.Text = "Server have start!";
  lblprogressbar.Text = "";
  progressbar.Value = 0;
  }

  private void startSever()
  {
  try
  {
  set1("Listening");
  IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipServer.Text), int.Parse(port.Text));
  tcpListener = new TcpListener(ip);
  tcpListener.Start();
  socketForClient = tcpListener.AcceptSocket();
  if (socketForClient.Connected)
  {
  NetworkStream ns = new NetworkStream(socketForClient);
  StreamReader sr = new StreamReader(ns);
  StreamWriter sw = new StreamWriter(ns);
  string filename = txtFileName.Text;
  nwkStream = new NetworkStream(socketForClient);
  stmReader = File.OpenRead(txtFileName.Text);
  stmWriter = nwkStream;
  FileInfo flInfo = new FileInfo(txtFileName.Text);
  //int size = Convert.ToInt32(flInfo.Length);
  long size = flInfo.Length;
  byte[] buff = new byte[32 * 2048];
  int len = 0;
  long progress = 0;
  set1("Starting");
  sw.WriteLine(filename);
  sw.Flush();
  while ((len = stmReader.Read(buff, 0, 2048)) != 0)
  {
  progress += len;
  set3(size, progress);
  set(progress.ToString() + " Byte of " + size + " Byte");
  stmWriter.Write(buff, 0, len);
  stmWriter.Flush();
  }
  set1("File has sent succesfully!");
  }
  }
  catch (Exception ex)
  {

  MessageBox.Show(ex.ToString());
  }
  finally
  {
  if (socketForClient != null)
  {
  socketForClient.Close();
  nwkStream.Close();
  stmWriter.Close();
  stmReader.Close();
  tcpListener.Stop();
  }
  }
  }

  void set1(string s)
  {
  if (InvokeRequired)
  {
  object[] pList = { s };
  lblMessage.BeginInvoke(new UpdateListBoxCallBack(OnUpdateLabel), pList);
  }
  else
  {
  OnUpdateLabel(s);
  }
  }

  private void OnUpdateLabel(String s)
  {
  lblMessage.Text = s;
  }

  void set(string s)
  {
  if (InvokeRequired)
  {
  object[] pList = { s };
  lblprogressbar.BeginInvoke(new UpdateListBoxCallBack(OnUpdateLabel1), pList);
  }
  else
  {
  OnUpdateLabel1(s);
  }
  }

  private void OnUpdateLabel1(String s)
  {
  lblprogressbar.Text = s;
  }

  void set3(long s, long p)
  {
  if (InvokeRequired)
  {
  object[] pList = { s, p };
  progressbar.BeginInvoke(new Updatepgdstatus(OnUpdatepgdstatus), pList);
  }
  else
  {
  OnUpdatepgdstatus(s, p);
  }
  }

  private void OnUpdatepgdstatus(long s, long p)
  {
  progressbar.Maximum = s;
  progressbar.Value = p;
  }

  private void send_Click(object sender, EventArgs e)
  {
  t.Start();
  }

  private void sukien2(object sender, EventArgs e)
  {
  port.Clear();
  }
  private void clearPort(object sender, EventArgs e)
  {
  port.Clear();
  }

  private void ClearFile(object sender, EventArgs e)
  {
  txtFileName.Clear();
  }

  private void disconnect(object sender, EventArgs e)
  {
  label5.Text = "Connect have drop!";
  socketForClient.Close();
  tcpListener.Stop();
  }

  private void exit(object sender, EventArgs e)
  {
  Close();
  }

  private void clearfilename(object sender, EventArgs e)
  {
  txtFileName.Clear();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  Control.CheckForIllegalCrossThreadCalls = false;
  }
  }
}


Re: Help me fix a type of variable in program send a recieve fil
Posted on: 19 Dec 2012  
hi,

check the given scenario

public void SomeMethod()
{
        int a;
        long b;
        a=b;
}

here we're going to assign a long to an integer. So the error occurs as

"Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)"

It's clear that you're using visual studio. Simply double click on the error, it'll take you to the error generating line. 

Hopefully, you will get the mess solved!

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.