Click here to Skip to main content
15,947,612 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, i am developing a client server application. how can i send data table from server to client in asynchronous socket programming in c#. Any idea or code.

Thanks in advance
Posted
Updated 26-Dec-12 23:19pm
v2

1 solution

If you gave us what you were sending I could help you out a tad more, I'm also not sure what you mean by "send gridview data from SERVER to CLIENT", server's are used to recv and client's send. I guess i can give you an example of a TcpClient and TcpServer's from another project I wrote a while ago.

TCP Server

C#
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace MyNamespace
{
    public class Server
    {
        public delegate void MessageReceived(string sender, string message, DateTime time);
        public event MessageReceived OnRecv;
        private Thread listen;
        private Thread answer;
        private TcpListener listener;
        private List<tcpclient> clients = new List<tcpclient>();
        public TcpClient[] Connections
        {
            get
            {
                return clients.ToArray();
            }
        }
        public int Port { get; set; }
        private void Answer()
        {
            int num = 0;
            while (true)
            {
                if (clients.Count == 0)
                    continue;
                if (clients.Count <= num)
                    num = 0;
                NetworkStream s = clients[num].GetStream();
                if (s.DataAvailable)
                {
                    byte[] buffer = new byte[clients[num].ReceiveBufferSize];
                    s.Read(buffer, 0, buffer.Length);
                    string recv = Encoding.ASCII.GetString(buffer).Replace("\0", "");
                    if (OnRecv != null)
                        OnRecv(clients[num].Client.RemoteEndPoint.ToString(), recv, DateTime.Now);
                    //s.Flush();
                }
                num++;
            }
        }
        private void Listen()
        {
            try
            {
                listener.Start(10);
            }
            catch { throw new Exception("PORT " + Port + " is already running!"); }
            while (true)
                clients.Add(listener.AcceptTcpClient());
        }
        public void Start()
        {
            if (listen != null && answer != null)
            {
                if (!listen.IsAlive && !answer.IsAlive)
                {
                    listen.Start();
                    answer.Start();
                }
            }
        }
        public void Stop()
        {
            if (listen != null && answer != null)
            {
                if (listen.IsAlive && answer.IsAlive)
                {
                    listen.Abort();
                    answer.Abort();
                }
            }
        }
        public Server(int port)
        {
            try
            {
                listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
                listener.AllowNatTraversal(true);
            }
            catch
            {
                throw new Exception("Unexpected server crash.");
            }
            finally
            {
                listen = new Thread(() => Listen());
                answer = new Thread(() => Answer());
                Port = port;
            }
        }
    }
}


TCP Client

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace MyNamespace
{
    public class Client
    {
        private TcpClient client;
        public void Send(IPEndPoint ep, string message)
        {
            if (client == null)
            {
                client = new TcpClient();
                client.Connect(ep);
                NetworkStream s = client.GetStream();
                byte[] buffer = Encoding.ASCII.GetBytes(message);
                s.Write(buffer, 0, buffer.Length);
            }
            else
            {
                if (!client.Connected)
                    client.Connect(ep);
                NetworkStream s = client.GetStream();
                byte[] buffer = Encoding.ASCII.GetBytes(message);
                s.Write(buffer, 0, buffer.Length);
            }
        }
        public void Send(IPEndPoint[] endpoints, string message)
        {
            foreach (IPEndPoint ep in endpoints)
                Send(ep, message);
        }
        public void Send(IPAddress[] addresses, int port, string message)
        {
            foreach (IPAddress ip in addresses)
                Send(new IPEndPoint(ip, port), message);
        }
        public Client()
        {
        }
    }
}


How to:
To use the TCPServer make sure you use the OnRecv event then use Start();
To use the TCPClient just suse the send functions, pretty easy.
 
Share this answer
 
v3
Comments
saeedazam786 23-Dec-12 22:59pm    
Thanks for your answer. i am developing a transport inquiry system in which passenger comes to bus stop and send request for available seats. the server sends him 'bus no,source, destination, date time and fare' information of all the buses on the bus stop. i did lots work but stuck on how to send all these information from server to client.
austinbox 24-Dec-12 0:17am    
Client client = new Client();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("myip"), port);
client.Send(ep, "mydata");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900