Click here to Skip to main content
15,895,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: Saving selected option into database Pin
Brisingr Aerowing12-Jan-13 13:49
professionalBrisingr Aerowing12-Jan-13 13:49 
GeneralRe: Saving selected option into database Pin
PIEBALDconsult12-Jan-13 15:22
mvePIEBALDconsult12-Jan-13 15:22 
QuestionBinary Reader Pin
dxtrx12-Jan-13 10:38
dxtrx12-Jan-13 10:38 
AnswerRe: Binary Reader Pin
Jibesh12-Jan-13 13:01
professionalJibesh12-Jan-13 13:01 
GeneralRe: Binary Reader Pin
dxtrx12-Jan-13 13:17
dxtrx12-Jan-13 13:17 
GeneralRe: Binary Reader Pin
Brisingr Aerowing12-Jan-13 13:48
professionalBrisingr Aerowing12-Jan-13 13:48 
QuestionSocket programming- Response to all client Pin
mohammadkaab12-Jan-13 7:36
mohammadkaab12-Jan-13 7:36 
AnswerRe: Socket programming- Response to all client Pin
pt140112-Jan-13 8:21
pt140112-Jan-13 8:21 
My advice is the same as I offer in response to most socket-related programming questions: don't roll your own, there's no point. Socket code is hard to write well and even harder to test thoroughly.

Instead, spend a bit of time getting your head around ZeroMQ[^]. Trust me, it will be time well spent. What you are trying to do is easily achieved with ZeroMQ. Look at the dealer-router & publish-subscribe patterns. I think this is the sort of thing you're after (taken from the ZeroMQ Guide) :-

https://github.com/imatix/zguide/raw/master/images/fig59.png[^]

Just to show how easy things can be with ZeroMQ, here's the complete code for a bare-bones chat client & server console app that does basically what you're after.

First, the server:-
using System;
using System.Text;
using ZeroMQ;

namespace ChatServer
{
    class Server
    {
        // Call with ChatServer.exe <router_ep> <publish_ep>
        // eg. ChatServer tcp://127.0.0.1:5000 tcp://127.0.0.1:5001
        static void Main(string[] args)
        {
            ZmqDealerSocket dealer = new ZmqDealerSocket();
            dealer.Bind(args[0]);

            ZmqPublishSocket publisher = new ZmqPublishSocket();
            publisher.Bind(args[1]);

            dealer.OnReceived += delegate(Byte[] bytes, bool multipart)
            {
                Console.WriteLine("Received from client: " + Encoding.UTF8.GetString(bytes));
                publisher.Send(bytes);
            };

            while (true)
                System.Threading.Thread.Sleep(1000);
        }
    }
}


...and the Client :-
using System;
using System.Text;
using ZeroMQ;

namespace ChatClient
{
    class Client
    {
        // Call with ChatClient.exe <dealer_ep> <subscribe_ep> <name>
        // eg. ChatClient tcp://127.0.0.1:5000 tcp://127.0.0.1:5001 Dave
        static void Main(string[] args)
        {
            ZmqDealerSocket dealer = new ZmqDealerSocket();
            dealer.Connect(args[0]);

            ZmqSubscribeSocket subscriber = new ZmqSubscribeSocket();
            subscriber.Connect(args[1]);
            subscriber.Subscribe("");

            subscriber.OnReceived += delegate(byte[] bytes, bool multipart)
            {
                string[] msg = Encoding.UTF8.GetString(bytes).Split('|');

                Console.WriteLine(String.Format("{0}: {1}", msg[0], msg[1]));
            };

            while (true)
                dealer.Send(args[2] + "|" + Console.ReadLine());
        }
    }
}


Finally, a batch file, test.bat, to demo it...
start "Server" cmd /T:8F /k ChatServer.exe tcp://127.0.0.1:5000 tcp://127.0.0.1:5001
start "Jim" cmd /T:8E /k ChatClient.exe tcp://127.0.0.1:5000 tcp://127.0.0.1:5001 Jim
start "Dave" cmd /T:8E /k ChatClient.exe tcp://127.0.0.1:5000 tcp://127.0.0.1:5001 Dave
start "Pete" cmd /T:8E /k ChatClient.exe tcp://127.0.0.1:5000 tcp://127.0.0.1:5001 Pete


How easy could it be??? Big Grin | :-D
Let me know if you want the VS2010 solution zip & ZeroMQ wrapper DLL I used...

modified 13-Jan-13 7:30am.

GeneralRe: Socket programming- Response to all client Pin
jschell14-Jan-13 8:26
jschell14-Jan-13 8:26 
GeneralRe: Socket programming- Response to all client Pin
pt140114-Jan-13 19:51
pt140114-Jan-13 19:51 
GeneralRe: Socket programming- Response to all client Pin
jschell16-Jan-13 8:53
jschell16-Jan-13 8:53 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab15-Jan-13 3:15
mohammadkaab15-Jan-13 3:15 
GeneralRe: Socket programming- Response to all client Pin
pt140115-Jan-13 4:01
pt140115-Jan-13 4:01 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab15-Jan-13 7:37
mohammadkaab15-Jan-13 7:37 
GeneralRe: Socket programming- Response to all client Pin
pt140115-Jan-13 8:37
pt140115-Jan-13 8:37 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab16-Jan-13 1:38
mohammadkaab16-Jan-13 1:38 
GeneralRe: Socket programming- Response to all client Pin
pt140116-Jan-13 1:52
pt140116-Jan-13 1:52 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab16-Jan-13 6:14
mohammadkaab16-Jan-13 6:14 
GeneralRe: Socket programming- Response to all client Pin
pt140116-Jan-13 9:32
pt140116-Jan-13 9:32 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab16-Jan-13 10:17
mohammadkaab16-Jan-13 10:17 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab17-Jan-13 11:23
mohammadkaab17-Jan-13 11:23 
GeneralRe: Socket programming- Response to all client Pin
pt140117-Jan-13 19:30
pt140117-Jan-13 19:30 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab17-Jan-13 22:14
mohammadkaab17-Jan-13 22:14 
GeneralRe: Socket programming- Response to all client Pin
mohammadkaab19-Jan-13 7:36
mohammadkaab19-Jan-13 7:36 
GeneralRe: Socket programming- Response to all client Pin
pt140119-Jan-13 8:05
pt140119-Jan-13 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.