无法将客户端连接到服务器 [英] can't connect client to server

查看:116
本文介绍了无法将客户端连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我制作2个程序,一个是服务器,另一个是客户端

客户工作得很好
但服务器不接受客户端连接
我在我的代码中找不到问题
请一些帮助:(

这是我的代码

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

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

推荐答案

您好,

我自己完成了这项工作,并且知道要花多少钱才能开始工作.您可能希望看一下我的文章,特别是这篇文章:
快速网络库2 [ http://networkinglibrary.codeplex.com/ [ ^ ]

希望这会有所帮助,
埃德

P.s.确保您的服务器正在侦听IPv4地址,并且您的客户端也在尝试连接到IPv4版本,这可能是您的问题.
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.


这篇关于无法将客户端连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆