0

There are two C# projects: one project is for the client, the other one is for the server. First step is to run the server , then to choose a target folder, after that to run the client project, to choose some text.txt to send to the server's target folder. Only client can send files to the server

Demo:

1.choosing file target                       2.client sends
   +------------+                                
   | tar folder |          <----------------       text.txt 
   +------------+

My problem: there isn't compile errors or syntax errors in both projects, the only problem is that the server doesn't receives the .txt file.

Client:

First I designed a form for the client such as:

enter image description here

And placed an OpenFileDialog from the ToolBox-> Dialogs-> OpenFileDialog control.

Full code:

namespace SFileTransfer
{
    public partial class Form1 : Form
    {
        string n;
        byte[] b1;
        OpenFileDialog op;

        private void button1_Click(object sender, EventArgs e) //browse btn
        {
            op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                string t = textBox1.Text;
                t = op.FileName;
                FileInfo fi = new FileInfo(textBox1.Text = op.FileName);
                n = fi.Name + "." + fi.Length;
                TcpClient client = new TcpClient("127.0.0.1", 8100);//"127.0.0.1", 5055
                StreamWriter sw = new StreamWriter(client.GetStream());
                sw.WriteLine(n);
                sw.Flush();
               // label2.Text = "File Transferred....";
            }
        }
        private void button2_Click(object sender, EventArgs e) //send btn
        {
            TcpClient client = new TcpClient("127.0.0.1", 8100);//5050
            Stream s = client.GetStream();
            b1 = File.ReadAllBytes(op.FileName);
            s.Write(b1, 0, b1.Length);
            client.Close();
           // label2.Text = "File Transferred....";
        }
    }
}

Server:

Created and designed a Form for Server like:

enter image description here

Then Placed a folderBrowserDialog from the ToolBox->Dialogs-> folderBrowserDialog.

Full code:

namespace SFileTransferServer
{
    public partial class Form1 : Form
    {
        string rd;
        byte[] b1;
        string v;
        int m=1;
        TcpListener list;
        TcpClient client;
        Int32 port = 8100;//5050
        Int32 port1 = 8100;//5055
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");

        private void button1_Click(object sender, EventArgs e) //browse button
        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;

                list = new TcpListener(localAddr, port1);
                list.Start();

                client = list.AcceptTcpClient();
                MessageBox.Show("ggggggg" + m.ToString());
                Stream s = client.GetStream();
                b1 = new byte[m];

                s.Read(b1, 0, b1.Length);
                MessageBox.Show(textBox1.Text);
                File.WriteAllBytes(textBox1.Text+ "\\" + rd.Substring(0, rd.LastIndexOf('.')), b1);
                list.Stop();
                client.Close();
                label1.Text = "File Received......";
            }
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            TcpListener list = new TcpListener(localAddr, port);
            list.Start();

            TcpClient client = list.AcceptTcpClient();
            MessageBox.Show("Client trying to connect");
            StreamReader sr = new StreamReader(client.GetStream());
            rd = sr.ReadLine();
            v = rd.Substring(rd.LastIndexOf('.') + 1);
            m = int.Parse(v);
            list.Stop();
            client.Close();
        }
    }
}

Based on this page

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Error 404
  • 427
  • 5
  • 25
  • Your port, and port1 are the same port. Is this on purpose? – Jamin Dec 10 '17 at 23:09
  • I tried to play with ports, but with no succeed – Error 404 Dec 10 '17 at 23:10
  • Are you going to need multiple connections/multithreading? – Kickass Dec 10 '17 at 23:25
  • 1
    No need multiple connections, only two computers, **not needed** multithreading – Error 404 Dec 10 '17 at 23:28
  • @Error404 If 2 separate computers are needed why are you using a loopback address on both the server and the client? I thought this was a single computer being the server and client. Your problem may come from the client looping back to itself rather than going to the server. Possibly changing your 127.0.0.1 to the server address? Or is this just to not show your own ip. Genuinely curious. To me it seems the connection you are trying to make is client->server. But instead you are getting client -> client. – Jamin Dec 11 '17 at 02:46
  • Now both projects are on the same computer, this is way 127.0.0.1 – Error 404 Dec 11 '17 at 08:33
  • @Jamin....I was curious at first too but, i believe he used a loopback address only to test the code first. And this should not create a problem assuming the connection is only opened once, as soon as the TcpListener is decalred twice with the same port it should throw an exception becasue the port is already in use. – Kickass Dec 11 '17 at 11:03

1 Answers1

0

I'm assuming you are a new coder because I'm seeing a lot of inefficient code.

On the Client App: Don't redeclare the TcpClient, instead declare it in the global scope and just reuse it.

Then On the server side: Always use the correct datatype IF AT ALL possible

Int16 port = 8100;
//same as
int port = 8100;

Then on to the question: First you are only reading a single byte from the received data

int m=1;
byte[] b1 = new byte[m];
s.Read(b1, 0, b1.Length);
//Then if you know the length of the byte, why do the computation of b1.Length, just use
s.Read(b1, 0, 1);

I see now you are also opeing the connection on the Load event. But that variable is not in the global scope so you are essentially creating a variable in the Load event then after the Load event finishes, sending it to the garbage collection and then declaring a variable with the same name in the button click event.

So try declaring the TcpListener object in the global scope then assign it in the load event and start listening.

Now the AcceptTcpClient() method is a blocking method so it will block your thead until it receives a connection attempt at which point it will return the client object. So try to use this instead: https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.accepttcpclient(v=vs.110).aspx

using System.Threading;
TcpClient client;
TcpListener server = new TcpListener("127.0.0.1",8100)
Thread incoming_connection = new Thread(ic);
incoming_connection.Start();

private void ic() {
client = server.AcceptTcpClient();
Stream s = client.GetStream();
//And then all the other code
}

Or you can use the Pending method as suggested by MSDN (on the ref page).

EDIT: using threading like this is 'not safe', so if you don't understand threading go read up on it first, this post doesn't cover how to use multi-threading.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kickass
  • 1,114
  • 9
  • 16