Below is the code of Server

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using System.Net.Sockets;
using System.Threading;
using timerss = System.Timers.Timer;

namespace Magic_Client
{
    class Program
    {
        static TcpListener server = new TcpListener(4296);
        static TcpClient client = new TcpClient();
        private static Thread thread;
        private static timerss timersss;

        private static void Main(string[] args)
        {
            server.Start();

            while (true)
            {
                client = server.AcceptTcpClient();
                Console.WriteLine("Connected");
                thread = new Thread(new ThreadStart(Read));
                thread.Start();
            }
        }

        private static void Read()
        {
            NetworkStream Nw = new NetworkStream(client.Client);
            int thisRead = 0;
            int Blocksize = 1024;
            Byte[] dataByte = new byte[Blocksize];
            Stream strm = File.OpenWrite(@"asdfg.txt");
            while (true)
            {
                thisRead = Nw.Read(dataByte, 0, Blocksize);
                strm.Write(dataByte, 0, thisRead);
                if (thisRead == 0){}
                break;
            }
            strm.Close();
            Console.WriteLine("Finish");
        }
    }
}

Below is the code of Client

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TcpClient client = new TcpClient();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            client.Connect("127.0.0.1", 4296);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Stream Fs = File.OpenRead(@"die.txt");
                Byte[] buffer = new Byte[Fs.Length];
                Fs.Read(buffer, 0, buffer.Length);
                NetworkStream nw = client.GetStream();
                nw.Write(buffer, 0, buffer.Length);
                nw.Close();
            }
            catch (Exception ex11)
            {
                MessageBox.Show(ex11.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                client.Connect("127.0.0.1", 4296);
            }
            catch (Exception ex11)
            {
                MessageBox.Show(ex11.Message);
            }
        }
    }
}

I just simply trying to make a file transfer. But after connecting I press button1, then file transfer OK. But if I pressed it again It gave me an error. Which is "The Operation is not allowed on non-connected...". So I made the button2 to connect again, but after pressing it it says i'm already connected. I read many articles regarding the error, but non of things worked for me. I even tried as admin but no work.
Please help me.

If you close your socket you need to rebuild it all from scratch again. If you want to re-use your socket, don't close it.

@Ketsuekiame
I tried with using Flush , but after using it I can get the first one, but if I pressed again it doesn't do anything nor give an error.

What are you expecting it to do? It should just keep writing to the same file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.