none
how to Receive/Send Data From Client on Server using C# ? RRS feed

  • Question

  • Using C# technology  I want to receive Client Data on Server PC and also Response back to To  clients  from Server  .i am using  Web socket UDP ..  i am using Server IP on client side code. 

    if i client and server on same network but diferent ip then this code work for me.but i use ip of server which is on other network.e.g i put  public ip of your PC ,then in that case i not response any.

    Note 2) Clients and server are on different Network. .not on same  router.

    note 3) Clients PC is behind Router,(mean IP change always)

    Note 4) Clients can be  more than 3 .

    My Client Side Code is here,which is console single file application.

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string ipOfServer = "xxx.xxx.xx.xx"; here i am using IP of server which is not same network. 
                int portServerIsListeningOn = 11000;

                // send data to server
                Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                IPAddress send_to_address = IPAddress.Parse(ipOfServer);
                IPEndPoint sending_end_point = new IPEndPoint(send_to_address, portServerIsListeningOn);
                sending_socket.SendTo(Encoding.ASCII.GetBytes("Test"), sending_end_point);

                // get info
                var port = sending_socket.LocalEndPoint.ToString().Split(':')[1];

                // now wait for server to send data back
                IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(port));
                byte[] buffer = new byte[1024];
                sending_socket.Receive(buffer); // <----- we can receive data now!!!!!
                Console.Write(buffer);
                System.Threading.Thread.Sleep(1000 * 4);
               
            }
        }
    }

    And on Server PC i using this code file.its also simple a console application of single file.

    namespace ConsoleApplication3
    {
        class Program
        {
            static byte[] dataToSend = new byte[] { 1, 2, 3, 4, 5 };

            static IPEndPoint GetClientInfo()
            {
               
                using (UdpClient listener = new UdpClient(11000))
                {
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 11000);
                    byte[] receive_byte_array = listener.Receive(ref groupEP);

                    return groupEP;
                }
            }

            static void Main(string[] args)
            {
                var info = GetClientInfo(); // get client info
                UdpClient newClient = ConstructUdpClient(info);

                // send data
                newClient.Send(dataToSend, dataToSend.Length);
            }

            // Construct a socket with the info received from the client
            static UdpClient ConstructUdpClient(IPEndPoint clientInfo)
            {
                var ip = clientInfo.Address.ToString();
                var port = clientInfo.Port;

                Console.Write(ip.ToString() + "    " + port.ToString());

                System.Threading.Thread.Sleep(1000 * 4);

                UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 11000));
                client.Connect(ip, port);

                return client;
            }
        }
    }

    Thanks you..


    Tuesday, November 25, 2014 5:58 PM

Answers

  • Note 1) Clients are using a windows Application and Server is using a Web application.

    Windows desktop solution, with WCF Web service over HTTP.

    Note 2) Clients and server are on different Network.not on LAN.

    It's done all of the time, no big deal and nothing new about this.

    note 3) Clients PC is behind Router,(mean IP change always)

    No that's not true. The router is using a WAN (Wide Area Network IP)/an Internet IP that is static that never changes so that the site can always be found by the client, which was assigned by an Internet Service Provider. Traffic on the Internet bewteen are are based on WAN IP(s) that are static in nature so traffic flows seemlessly.

    The LAN (Local Area Network) the IP(s) in that situation are used by machines connected to a router, which can be dynamic IP(s) issued by a DHCP server the router is using. So a machine requesting an IP from a DHCP server on the LAN can have a different IP at times. But a router knows what IP belongs to what IP at any give time the router has assigned to a machine by the computer's NIC (Network Interface Card's) MAC address. That's how the router knows how to route the traffic back to an IP a computer is using, by the IP and the MAC the computer is using. A computter can always use a static LAN IP on that never changes, which could be used by a Web server computer on the LAN so traffic to the Web server is always routed to the static IP the Web server is using on the LAN and can always be found at that IP, just like the WAN IP traffic can always be found pointing to a device on the Internet. 

    Note 4) Clients are more than 100 .

    e.g On Client PC  i am writing a string "ABC" in a TEXT BOX and click on Send button.then on Client PC i want to receive "Server Received String " and on Server PC I want Reciever " From Client PC with IP = xxxxx.xxx.xxx send "ABC" to Server IP =xxx.xx.xx.xx.".

    Ponintless, the wheel has already been invented, it's something you as a .NET programmmer should have no concern about, your job is to program the solution and have the correct programming technology in place for the business need.

    https://www.youtube.com/watch?v=ufTImT0vvU0

    http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

    http://www.codeproject.com/Articles/88390/MVP-VM-Model-View-Presenter-ViewModel-with-Data-Bi

    http://en.wikipedia.org/wiki/Multitier_architecture

    http://www.c-sharpcorner.com/UploadFile/dacca2/understand-3-tier-architecture-in-C-Sharp-net/

    Tuesday, November 25, 2014 7:40 PM

All replies

  • This question is far too broad to answer.  It completely depends upon what you're doing, any environmental restrictions you have and what components you own.

    Given your description it would sound like you are probably going to need a peer to peer system so that you can send info between client and server and have it go across the Internet.  There is too much code to post. I strongly recommend that you review the various approaches to client/server communication and identify a couple of approaches that may work for you. Then create some prototypes that test out the feasibility. We can help you with specific questions but we won't write your app for you so you're going to have to do some learning and coding on your own. There are plenty of example peer to peer system available for you to look at if you google for them.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Tuesday, November 25, 2014 6:58 PM
    Moderator
  • Note 1) Clients are using a windows Application and Server is using a Web application.

    Windows desktop solution, with WCF Web service over HTTP.

    Note 2) Clients and server are on different Network.not on LAN.

    It's done all of the time, no big deal and nothing new about this.

    note 3) Clients PC is behind Router,(mean IP change always)

    No that's not true. The router is using a WAN (Wide Area Network IP)/an Internet IP that is static that never changes so that the site can always be found by the client, which was assigned by an Internet Service Provider. Traffic on the Internet bewteen are are based on WAN IP(s) that are static in nature so traffic flows seemlessly.

    The LAN (Local Area Network) the IP(s) in that situation are used by machines connected to a router, which can be dynamic IP(s) issued by a DHCP server the router is using. So a machine requesting an IP from a DHCP server on the LAN can have a different IP at times. But a router knows what IP belongs to what IP at any give time the router has assigned to a machine by the computer's NIC (Network Interface Card's) MAC address. That's how the router knows how to route the traffic back to an IP a computer is using, by the IP and the MAC the computer is using. A computter can always use a static LAN IP on that never changes, which could be used by a Web server computer on the LAN so traffic to the Web server is always routed to the static IP the Web server is using on the LAN and can always be found at that IP, just like the WAN IP traffic can always be found pointing to a device on the Internet. 

    Note 4) Clients are more than 100 .

    e.g On Client PC  i am writing a string "ABC" in a TEXT BOX and click on Send button.then on Client PC i want to receive "Server Received String " and on Server PC I want Reciever " From Client PC with IP = xxxxx.xxx.xxx send "ABC" to Server IP =xxx.xx.xx.xx.".

    Ponintless, the wheel has already been invented, it's something you as a .NET programmmer should have no concern about, your job is to program the solution and have the correct programming technology in place for the business need.

    https://www.youtube.com/watch?v=ufTImT0vvU0

    http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

    http://www.codeproject.com/Articles/88390/MVP-VM-Model-View-Presenter-ViewModel-with-Data-Bi

    http://en.wikipedia.org/wiki/Multitier_architecture

    http://www.c-sharpcorner.com/UploadFile/dacca2/understand-3-tier-architecture-in-C-Sharp-net/

    Tuesday, November 25, 2014 7:40 PM
  • Can you just tell me how to initialize Client to push data to server (by using Server IP) then response back to Client and detect client IP TOO..just in C# i want do thats..  
    Tuesday, November 25, 2014 7:45 PM
  • Can you just tell me how to initialize Client to push data to server (by using Server IP) then response back to Client and detect client IP TOO..just in C# i want do thats.. 

    Well you have a lot to learn then, if you think someone in some forum is going to just give you a simple solution. You want the glory, but you don't want to put in the time and effort to find out how to use technology, properly? I feel sorry for your clients. No pain, and there is no gain. You need to get busy and start reading the articles and do those tutorials to even get into the ballpark of even doing things half-ass correctly let alone corectly, instead of trying to do the wild-thing and get yourself burnt. Do the right thing man.  

    Tuesday, November 25, 2014 7:57 PM
  • Well May be i will have not good explain my Question. on some point you give me right  answer but not give a exact solution of problem.

    my Question is that .I want send/recive data between server and clients using c# .I try do this job by using UDP ,socket programming . I almost done that job.i recieve Data from client and server response back to client ,but its was only work on same network PC. If i use My other network Pc as a server and client on different network then i was not able to receive/response between Server ,Client.

    should i have post my Code here ???  

     
    Tuesday, November 25, 2014 8:11 PM
  • Please Read my Question Again ,I've updated.
    Tuesday, November 25, 2014 9:00 PM
  • Please Read my Question Again,i've updated.
    Tuesday, November 25, 2014 9:01 PM
  • If i use My other network Pc as a server and client on different network then i was not able to receive/response between Server ,Client.

    The other network you are trying to make contact with is using a router or firewall applinace that is protecting the machines on the LAN (Local Area Network) the private/protexted network protected from the Internet by the router or the firewall applimace most likely. 

    Let's say your LAN has a Web server and a Web server is on the LAN using a router. The client using a browser or whatever the Web program is the client program must send traffic to make the initial contract with the Web server and the Web application runnning on the server. A router or FW allows solicted traffic through them, menaing a machine behind the  router or FW must send outbound traffic out  before router will allow inbound traffic back to the LAN and back to the machine on the LAN that sent out the outbound traiffc.

    Unsoliced traffic means that traffic is sent by a machine to the WAN IP, but a machine behind the router or FW never sent outbound traffic for the solictation. Routers and FW(s) block unsolicted inbound traffic and allow solicted traffic through them back to the machine.

    Router and FW applinaces have a feature called Port Fowarding.  It's a rule that can be setup with the router's or FW's software that tells the router or FW that if you get in unsolicted inbound traffic on port 80 and it's HTTP, route it to 192.168.1.105 to the IP of the machine on the LAN that is hosting the Web server and the Web application to make the initial contact. After the inital contact, the communcations can begin. That's the basics.

    So figure out how to use Port Forwading on the router. Also if Windows FW or something is like that in on the service machine, the open the right port on that FW too.

    I don't know if that's your problem or not, but most likely it is -- no Port Forwading.

    Tuesday, November 25, 2014 9:23 PM
  • you are right here,but what is solution if firewall there,Port forwarding? 

    Kindly tell me ,in my code where i write a code for port forwarding??so after its should be done my job.. can you please change my code and send me back with port forwarding??

    Tuesday, November 25, 2014 10:27 PM
  • If a software FW like the Windows FW, then you need to go to the FW and set a rule in the FW administration screens to open the appropriate port.
    Tuesday, November 25, 2014 10:36 PM
  • Or just turn off the FW for now.
    Tuesday, November 25, 2014 10:38 PM
  • turn off FireWall on client PC or on Server pc???? 
    Wednesday, November 26, 2014 12:36 PM
  • The pc that host the service must be initially contacted by the client. After all that has been explained as to how a FW works, I'll let you figure out what FW must be disabled on which machine.

    Thursday, November 27, 2014 10:46 PM
  • Although I am a bit late here, I thought to answer anyway, as this is a very common problem with device drivers.  

    Take a look at the TCPClient class.


    Friday, August 3, 2018 3:51 PM