Hi Everybody
I am writing a socket program on a smart device.
What I am trying to write is when client connect wit server, server send the file name that cllent can download. Then client must choose one file name and send that file name to server. Then the server must send the content of that file to the client.
I can connect client with server. And server can send list of file name and client can choose the name of file that it want to download and send to server. But when the server try to send the content of file, It does not work.

It even does not show the message box. It stop at the server site.
Now I have got the headache. Please help me.
What is the error?


Here is my program.

//here is for client side

using System;

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

namespace Clientfiletransfer
{
    public partial class Form1 : Form
    {
        public delegate void ItemAdd();
        ItemAdd objDelegate;

        public delegate object Getselecteditem();
        Getselecteditem objgetselecteditem;

        string data;//read data from server
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public void AddItemtoListBox()
        {
            listBox1.Items.Add(data);
        }

        public object Readitemfromlist()
        {
            return listBox1.SelectedItem;
        }


        public Form1()
        {
            InitializeComponent();
            Text = "Client";
            objDelegate = new ItemAdd(AddItemtoListBox);
            objgetselecteditem = new Getselecteditem(Readitemfromlist);

        }

        private void connectbutton_Click(object sender, EventArgs e)
        {
            ThreadStart connect = new ThreadStart(Connectserver);
            Thread objthread = new Thread(connect);
            objthread.Start();
        }
      

        public void Connectserver()
        {
            //Socket client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            client.Connect(new IPEndPoint(IPAddress.Loopback,2000));
            
            NetworkStream ns = new NetworkStream(client);
            StreamReader reader = new StreamReader(ns);
            StreamWriter writer = new StreamWriter(ns);
            

            data = reader.ReadLine();
            while (data != null)
            {
                listBox1.Invoke(objDelegate);
                data = reader.ReadLine();
            }

            /*
            /////////////to read file content from server
            reader = new StreamReader(ns);
            data = reader.ReadToEnd();
            //listBox1.Invoke(objDelegate);

            string filename = (listBox1.Invoke(objgetselecteditem)).ToString();
            FileStream fstream = null;
            if (!File.Exists(filename))
            fstream = File.Create(filename);
            fstream = File.Open(filename, FileMode.Open, FileAccess.Write);
            writer = new StreamWriter(fstream);
            writer.Write(data);
            MessageBox.Show("download completed");*/


            
        }

        private void downloadbutton_Click(object sender, EventArgs e)
        {
            NetworkStream ns = new NetworkStream(client);
            StreamWriter writer = new StreamWriter(ns);


            data = (listBox1.Invoke(objgetselecteditem)).ToString();
            writer.AutoFlush = true;
            writer.WriteLine(data);




            /////////////to read file content from server
            StreamReader reader = new StreamReader(ns);
            data = reader.ReadToEnd();
            //listBox1.Invoke(objDelegate);

            string filename = (listBox1.Invoke(objgetselecteditem)).ToString();
            FileStream fstream = null;
            //if (!File.Exists(filename))
            //fstream = File.Create(filename);
            fstream = File.Open(filename, FileMode.CreateNew, FileAccess.Write);
            writer = new StreamWriter(fstream);
            writer.Write(data);
            MessageBox.Show("download completed");
            

        }
    }
}

//Server side

using System;

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

namespace Serverfiletransfer
{
    public partial class Form1 : Form
    {
        
      
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Startbutton_Click(object sender, EventArgs e)
        {
            ThreadStart listeningThread = new ThreadStart(Startserver);
            Thread objthread = new Thread(listeningThread);
            objthread.IsBackground = true;
            objthread.Start();

        }

        public void Startserver()
        {
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, 2000));
            serverSocket.Listen(5);
            while (true)
            {
                Socket client = serverSocket.Accept();
                //MessageBox.Show("client connected");


                NetworkStream ns = new NetworkStream(client);
                StreamWriter writer = new StreamWriter(ns);
                

                string[] directories = Directory.GetFiles("/");
                foreach (string file in directories)
                {
                    writer.AutoFlush = true;
                    writer.WriteLine(file);

                }

                
                //to know the file  client want to download
                StreamReader reader = new StreamReader(ns);
                string filename = reader.ReadLine();
                MessageBox.Show(filename);
                

                //to read&send the file content
                FileStream fstream = null;
                fstream = File.Open(filename, FileMode.Open, FileAccess.Read);
                reader = new StreamReader(fstream);
                string data = reader.ReadLine();
                while (data != null)
                {
                    writer.AutoFlush = true;
                    writer.WriteLine(data);
                    data = reader.ReadLine();
                }

                /*string data = (reader.ReadToEnd()).ToString();
                writer.AutoFlush = true;
                writer.Write(data);*/

            }

        }
    }
}

Hi!
Please tell me only one thing.
why the program stop at the server side.
At the final point, I can not show even a message box in the client side.

I've been looking at your code, and it seems like as if your trying to send and receive in the same transmission. However because the server is trying to receive when there is nothing to recieve, because the client hasnt choosen the file they want to download, and error gets thrown.

You would really benefit from using Async sockets and creating some sort of packet reader module, so that it knows what to send when. After I get off of work tonight, I can write up something really quick for you....

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.