problem with file sending program in c#.
-
venerdì 27 aprile 2012 18:12
hello,i am developing the file sending program in c#.
but the facing errors
Error from client side -> the sending parameter is null
Error from server side -> the listener problem.
Server SIDE CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace isend
{
public partial class receiver : Form
{
string inforeceiver;
byte[] b1;
string filesize;
int m;
IPAddress localAddr = IPAddress.Parse("192.168.1.16");
public receiver()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
TcpListener listen = new TcpListener(localAddr, 7);
listen.Start();
TcpClient client = listen.AcceptTcpClient();
Stream s = client.GetStream();
b1 = new byte[m];
s.Read(b1, 0, b1.Length);
File.WriteAllBytes(textBox1.Text + "\\" + inforeceiver.Substring(0, inforeceiver.LastIndexOf('.')),
b1);
listen.Stop();
client.Close();
}
}
private void Form3_Load_1(object sender, EventArgs e)
{
TcpListener listen = null;
try
{
listen = new TcpListener(localAddr, 7);
listen.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
TcpClient client = null;
client = listen.AcceptTcpClient();
MessageBox.Show("Client trying to connect");
StreamReader sr = new StreamReader(client.GetStream());
inforeceiver = sr.ReadLine();
filesize = inforeceiver.Substring(inforeceiver.LastIndexOf('.') + 1);
m = int.Parse(filesize);
listen.Stop();
client.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
CLient SIDE CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;
namespace isend
{
public partial class sender : Form
{
string fileinfo;
Byte[] b;
OpenFileDialog op;
string Server;
public sender()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
string t = textBox1.Text = op.FileName;
FileInfo finfo = new FileInfo(textBox1.Text);
fileinfo = finfo.Name + "." + finfo.Length;
label1.Text = fileinfo;
try
{
TcpClient client = new TcpClient(Server, 7);
StreamWriter sw = new StreamWriter(client.GetStream());
sw.WriteLine(fileinfo);
sw.Flush();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
TcpClient client = new TcpClient(Server, 7);
NetworkStream s = client.GetStream();
b = File.ReadAllBytes(op.FileName);
s.Write(b, 0, b.Length);
client.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)
{
Server = textBox2.Text;
}
}
}
Tutte le risposte
-
venerdì 27 aprile 2012 22:15
Receiver Problem
I found that the receiver port was bad. You used port 7 which is for ECHO messages which is UDP and you are using TCP. You have to use a TCP port number. See this webpage for list of common port numbers. I rfecommend using a port number over 10,000 which isn't reserved and can be used for any protocol.
Send Problem
The Server string wasn't initialized in either button 1 or button 2.
fix this line : string Server;
jdweng

