Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I would like to to create a server application that listens to a remote client via TCPlistener and output the message to a form. What I've done so far is : 1) in the infrastructure class

    public class TcpListenerAndClient
    {
        int port;
        private Socket tcpSocket = null;
        public TcpListener peerListener ; 
        public TcpListenerAndClient(int Port)
        {
            this.port = Port;
        }
        public void OpenTcpListener()
        {
            peerListener = new TcpListener(IPAddress.Any, port);
            peerListener.Start();
        }  
        public void AcceptSocket()
        {
            tcpSocket = peerListener.AcceptSocket();
        }
    }

2) in the domain class

    public class Peer2PeerCom
    {
        public Thread ListenerThread ;
        int port = 0;
        public infraStructure.TcpListenerAndClient TraficPerfServer;
        public Peer2PeerCom(int Port)
        {
            this.port = Port;
            TraficPerfServer = new infraStructure.TcpListenerAndClient(port);
        }
        public void CreateListenr_InAnotherThread()
        {
            ListenerThread = new Thread(new ThreadStart(socketListener));
            ListenerThread.Name = "Listener Thread";
            ListenerThread.Priority = ThreadPriority.AboveNormal;
            ListenerThread.Start();

        }



        //public infraStructure.TcpListenerAndClient Tcptool = new infraStructure.TcpListenerAndClient(port);
        /// <summary>
        /// void listener to be used in thread 
        /// </summary>
        public void socketListener()
        {
            TraficPerfServer.OpenTcpListener();
            while (true)
            {
                if (!TraficPerfServer.peerListener.Pending())
                {
                    Thread.Sleep(500);
                    continue;
                }
                TraficPerfServer.AcceptSocket();

                    //once we get here , there is message to collect -> need to show it in GUI 
            }
        }

3) in the control class

        public void ConnectToIP(int port , string client/**IPAdd**/)
        {
            StartListenr = new domain.Peer2PeerCom(port);

            // set up a Listener in another THread 
            if (!isConnected)
            {
                isConnected = true;
                StartListenr.CreateListenr_InAnotherThread();
            }
        }

Now I have to make the GUI fetch the result. My initial idea is to use this design layer in the design layer (GUI->control->domain->infra).

I know I need some kind of data structure that will store the message in the control(?) and an EVENT in the GUI class that will look for the change, but I can't wrap my head around it.

share|improve this question

closed as off-topic by MrSmith42, Glenn Rogers, syb0rg, palacsint, Vogel612 Apr 30 at 14:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Glenn Rogers, syb0rg, palacsint, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

Browse other questions tagged or ask your own question.