Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:36:26+00:00 2026-06-13T20:36:26+00:00

I want to sent DataTable from Server to Client using XML file in C#.

  • 0

I want to sent DataTable from Server to Client using XML file in C#. I have used following code at server side

DataSet ds = new DataSet();
ds.WriteXml(nw, XmlWriteMode.WriteSchema);

where nw is NetworkStream at Server

and following code is at Client side

DataSet ds = new DataSet();
ds.ReadXml(clientSockStream, XmlReadMode.ReadSchema);

The server is sending data but at client side the program gets halted.
Server Side:

tcpserver = new TcpListener(IPAddress.Any, 4444);
tcpserver.Start();
Socket serverSocket = tcpserver.AcceptSocket(); // accepting connection
    if (serverSocket.Connected)
    {
        serversockstream = new NetworkStream(serverSocket);
        serverStreamReader = new StreamReader(serversockstream);
        serverStreamWriter = new StreamWriter(serversockstream);
        handleClinet client = new handleClinet(); 
        client.startClient(serverStreamReader, serversockstream,serverStreamWriter);
    }

    public void startClient(StreamReader streamReader, NetworkStream ser, StreamWriter streamWriter)
    {
        this.sread = streamReader;
        this.swrite = streamWriter;
        this.nwstram = ser;
        Thread ctThread = new Thread(handleClients);
        ctThread.Start();
    }

    private void handleClients()
    {
        // Creating Dummy DataTable to send to Client...
        DataSet ds = new DataSet();
        DataTable d = new DataTable();
        d.Columns.Add("Name");
        d.Columns.Add("City");
        d.Rows.Add("John","USA");

        ds.Tables.Add(d);

        try
        {    

            while ((true))
            {
                String mystr = sread.ReadLine();
                if (mystr.Equals("sendTable"))
                    break;
            }
            swrite.WriteLine("done");
            swrite.Flush();
            if(nwstram.CanWrite)
            {
                MessageBox.Show("it can write...");
                ds.WriteXml(nwstram, XmlWriteMode.IgnoreSchema);
            }
            nwstram.Flush();
            MessageBox.Show("XML Sent");

        }

Client Side:

    tcpClient = new TcpClient("127.0.0.1", 4444);

    IPHostEntry ip = Dns.GetHostEntry("127.0.0.1");
    //get a network stream from server
    clientSockStream = tcpClient.GetStream();
    clientStreamReader = new StreamReader(clientSockStream);
    clientStreamWriter = new StreamWriter(clientSockStream);

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            clientStreamWriter.WriteLine("sendTable");
            clientStreamWriter.Flush();
            DataSet ds = new DataSet();
            String str = "";
            while (true)
            {
                str = clientStreamReader.ReadLine();
                if (str.Equals("done"))
                    break;

            }
            //MessageBox.Show("Client :" + str);
            if (clientSockStream.CanRead)
            {
                ds.ReadXml(clientSockStream, XmlReadMode.IgnoreSchema);
            }
        }
        dataGridView1.DataSource = ds.Tables[0];
    }

Assume that all catch blocks and necessary variables have declared ..
Plz reply..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:36:29+00:00Added an answer on June 13, 2026 at 8:36 pm

    I’m not sure that I am seeing all of your code, but here is an example of what I would do if I HAD to use TcpSockets instead of WCF.

    SERVER SIDE

    public void StartServer()
    {
        TcpListener listener = null;
        TcpClient client = null;
        NetworkStream nwStream = null; 
    
        try
        {
            listener = new TcpListener(IPAddress.Any, 4444);
            listener.Start();
        }
        catch(Exception ex)
        { Console.WriteLine(ex.Message); }
    
        byte[] buffer = new byte[1024];
    
        try
        {
            bool waiting = true;
    
            while(waiting)
            {
                if(listener.Pending())
                {
    
                    client = listener.AcceptTcpClient();
                    nwStream = client.GetStream();
    
                    using(var ms = new MemoryStream())
                    {
                        CreateDataSet().WriteXml(ms);
                        ms.Position = 0;
    
                        int read = 0;
                        while((read = ms.Read(buffer, 0, 1024)) != 0)
                        {
                            nwStream.Write(buffer, 0, read);
                        }
                    }
                    nwStream.Flush();
                    nwStream.Close();
                    client.Close();
                    waiting = false;
                }
            }
        }
        catch(Exception ex)
        {
           Console.WriteLine(ex.Message);
        }
    }
    
    private DataSet CreateDataSet()
    {
        //Create your dataset here and return something
    }
    

    CLIENT SIDE

    public DataSet GetDataSet()
    {
        DataSet ds = new DataSet();
        TCPClient client = null;
        NetworkStream nwStream = null;
    
        try
        {
            client = new TcpClient("127.0.0.1", 4444);
            nwStream = client.GetStream();
    
            using(var ms = new MemoryStream())
            {
                byte[] buffer = new byte[1024];
                int read = 0;
                while((read = nwStream.Read(buffer, 0, 1024)) != 0)
                {
                    ms.Write(buffer, 0, read);
                }
                ms.Position = 0;
    
                ds.ReadXml(ms);
            }
        }
        finally
        {
            nwStream.Close();
            client.Close();
        }
    
        return ds;
    }
    

    I wouldn’t say that this will work exactly as you want, but with some modification that should be a good start. However, I would highly suggest looking into WCF (I’m a huge propponent) because here is the framework of your same service in some code.

    WCF SERVER SIDE

    [ServiceContract]
    public interface IDataService
    {
        [OperationContract]
        DataSet GetData();
    }
    
    public class DataService: IDataService
    {
        public DataSet GetData()
        {
            //return some dataset here
        }
    }
    

    WCF CLIENT SIDE

    private DataSet GetDataSet()
    {
        DataSet ds;
    
        //This would be utilizing some gerated code, or you can make your own client
        var client = new DataServiceClient(someEndPoint, someBinding);
        client.Open();
    
        ds = client.GetData();
    
        client.Close();
        return ds;
    }
    

    Again, that is not totally working code, but is just a sample of why WCF is so popular over writing your own crazy through sockets. If you are looking to stay language neutral, you could even upgrade to http sockets instead and it will still work with Java, so long as it is SOAP standard (there are a few features like reliable messaging that are not compatible with anything other than .NET).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to sent very large data from Server to client, the server is
I want to sent the data from an Android device to PHP. I have
I want to sent some information through a from, executing a php file that
i want to sent SMS from my JAVA APPLICATION in the following format: NAME:
I want to sent message from my server to phone, by PHP. Here is
I have a simple code, which send from server to clients value to count.
The following is my code for adding a calendar event. I want to sent
I want to have my pdf files sent this way to my users :
I want to see the actual commands sent to g++ during a Code::Blocks build.
I want to capture the Ctrl + C ( SIGINT ) signal sent from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.