Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
I make 2 program one is server and the another is the client

the client is work fine
but the server do not accept the client connection
I cant find the problem in my code
please some help :(

this is my code
C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {


        IPEndPoint ServerIPE;
        IPAddress ServerIP;
        int ServerPort;
        Thread ListenThread;
        String remoteIP;
        String remotePort;
        
        TcpClient strr;

        private TcpListener TcpSer;
      
        private TcpClient TcpCli;


        delegate void RecieveMsgDel(string msg, TcpClient Client);
        RecieveMsgDel rMsgDel;
    
        
        private ArrayList client = new ArrayList();
        private ArrayList thrdArry = new ArrayList();

        public delegate void ChangedEventHandler(object sender, EventArgs e);
        public event ChangedEventHandler Changed;
        public delegate void SetListBoxItem(String str, String type);



        public Form1()
        {
            InitializeComponent();

            Changed += new ChangedEventHandler(ClientAdded);

            TreeNode node;
            node = treeView1.Nodes.Add("Connected Clients");
               
        }


        class MyEventArgs : EventArgs
        {
            private TcpClient sock;
            public TcpClient clientSock
            {
                get { return sock; }
                set { sock = value; }
            }

            public MyEventArgs(TcpClient tcpClient)
            {
                sock = tcpClient;
            }


        }


        public void NewClient(Object obj)
        {
            ClientAdded(this, new MyEventArgs((TcpClient)obj));
        }


        public void ClientAdded(object sender, EventArgs e)
        {
            TcpCli = ((MyEventArgs)e).clientSock;
            remoteIP = ((IPEndPoint)TcpCli.Client.RemoteEndPoint).Address.ToString();
            remotePort = ((IPEndPoint)TcpCli.Client.RemoteEndPoint).Port.ToString();

            strr = new TcpClient(remoteIP,int.Parse(remotePort))  ;
            // Call Delegate Function to update Tree View
            UpdateClientList(remoteIP + " : " + remotePort, "Add");
            


            thrdArry.Add(Thread.CurrentThread);

            if(true)
            {
            Thread clientThread = new Thread(new ParameterizedThreadStart(RemotClient));
            clientThread.Start(); 
            }

        }



        private void RemotClient(object client)
        {
            if (client.Equals(strr))
            {
                //remote client object from RemoteClient class 
                RemoteClient remote = new RemoteClient();

                //client.Equals(str);
                // error follow
                remote.SendByte(TcpCli.Client,strr);

            }
            
        }


        private void UpdateClientList(string str, string type)
        {
            // InvokeRequired required compares the thread I
            { }
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.treeView1.InvokeRequired)
            {
                SetListBoxItem d = new SetListBoxItem(UpdateClientList);
                this.Invoke(d, new object[] { str, type });
            }
            else
            {
                // If type is Add, the add Client info in Tree View
                if (type.Equals("Add"))
                {
                    client.Add(str);
                    this.treeView1.Nodes[0].Nodes.Add(str);
                }
                // Else delete Client information from Tree View
                else
                {
                    foreach (TreeNode n in this.treeView1.Nodes[0].Nodes)
                    {
                        if (n.Text.Equals(str))
                        {
                            this.treeView1.Nodes.Remove(n);
                            client.Remove(str);
                        }

                    }
                }

            }
        }


        public void StopServer()
        {

            if (TcpSer != null)
            {

                // Close all Socket connection
                //foreach (ChatDialog c in frmArry)
                //{
                //    c.connectedClient.Client.Close();
                //}

                // Abort All Running Threads
                foreach (Thread t in thrdArry)
                {
                    t.Abort();
                }

                // Clear all ArrayList
                //thrdArry.Clear();
                client.Clear();
                treeView1.Nodes[0].Nodes.Clear();

                // Abort Listening Thread and Stop listening
                ListenThread.Abort();
                TcpSer.Stop();
            }
            Port.Enabled = true;
        }





        private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
        {
            StopServer();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            ServerIP = IPAddress.Parse("127.1.1.1");
            ServerPort = int.Parse(Port.Text);
            ServerIPE = new IPEndPoint(ServerIP, ServerPort);
            TcpSer = new TcpListener(ServerIPE);
            ListenThread = new Thread(new ThreadStart(ListenForClients));
            ListenThread.Start();
            ConnBotn.Enabled = false;


        }



         private void ListenForClients()
        {
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            TcpSer = new TcpListener(localAddr, Int32.Parse(Port.Text));
            TcpSer.Start();

            
                // Keep on accepting Client Connection
                while (true)
                {

                    // New Client connected, call Event to handle it.
                    Thread t = new Thread(new ParameterizedThreadStart(NewClient));

                    TcpCli = TcpSer.AcceptTcpClient();
                    t.Start(TcpCli);

                }


                

            
        }

the tree view must print all client connect to the server
but nothing printed

thanks in advance
Posted
Comments
SASS_Shooter 18-May-12 11:43am    
Just wondering....why are you doing all of the networking manually? If you just build your server as a WCF service you save yourself a ton of network programming and the client connection will just work!
maxpower12345 18-May-12 11:46am    
I cant install WCF ,, so I dont know how to deal with it
R. Giskard Reventlov 18-May-12 11:49am    
So, because you won't take the time to learn to do it the right way you want us to help you figure out how to do it the wrong way? Hilarious.
Ed Nutting 18-May-12 11:57am    
I'm sorry but "the right way"? What are you on about? Using System.Net.Sockets is perfectly fine if the OP's application requires it - WCF certainly isn't right for say, an online game. The OP may wish to take a look at my articles that I ave written on a networking library that I have written. Your attitude I feel is totally wrong and just disregarding such a large thing as using Sockets is totally wrong... I think your claims are "hilarious".

Ed
R. Giskard Reventlov 18-May-12 12:36pm    
Was trying to be a little light-hearted. Never mind: feel free to patronise.

1 solution

Hi there,

I have done this myself and know how much of a pin it s to get working. You may wish to take a look at my articles, in particular this one:
Fast Networking Library 2[^]

or take a look at my latest version of it on CodePlex:
http://networkinglibrary.codeplex.com/[^]

Hope this helps,
Ed

P.s. Make sure your server is listeneing on the IPv4 address and that your client is also trying to connect to the IPv4 version, this may be your problem.
 
Share this answer
 
v2

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