标题:socket大神麻烦看看,实在找不出问题!!
只看楼主
张钊1993
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-7-6
 问题点数:0 回复次数:2 
socket大神麻烦看看,实在找不出问题!!
namespace SocketServer
{
    class Program
    {
        private static byte[] result = new byte[1024];
        private static int myProt = 9000;   //端口
        static Socket serverSocket;
        static List<Socket> socketlist = new List<Socket>();
        static void Main(string[] args)
        {
            //绑定IP地址:端口
            IPAddress ip = IPAddress.Any;
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(ip, myProt));  
            serverSocket.Listen(10);   
            Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
            //通过Clientsoket发送数据
            Thread myThread = new Thread(ListenClientConnect);
            myThread.Start();
            Console.ReadLine();
        }
        /// <summary>
        /// 监听客户端连接
        /// </summary>
        private static void ListenClientConnect()
        {
            while (true)
            {
                Socket clientSocket = serverSocket.Accept();
                socketlist.Add(clientSocket);
                clientSocket.Send(Encoding.Default.GetBytes("你已经连接上服务器"));
                Thread receiveThread = new Thread(ReceiveMessage);
                receiveThread.Start(clientSocket);
            }
        }

        /// 接收消息
        private static void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                try
                {
                    //通过clientSocket接收数据
                    int receiveNumber = myClientSocket.Receive(result);
                    temp=Encoding.Default.GetString(result, 0, receiveNumber);
                    //给客户端群发消息,为什么不对,应该怎么修改????求求大神帮忙看看,不胜感激
                    foreach (Socket sl in socketlist)
                    {
                        sl.Send(Encoding.Default.GetBytes(temp));
                    }
                    Console.WriteLine("接收消息为:{0}", temp);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }
        }
    }
}
搜索更多相关主题的帖子: socket private static new Thread 
2018-07-06 22:50
Jo_Kang816
Rank: 1
等 级:新手上路
威 望:1
帖 子:4
专家分:0
注 册:2018-6-5
得分:0 
接收的消息没有添加到 socketlist 中,接收地址未知
2018-09-07 12:01
Brends
Rank: 2
等 级:论坛游民
威 望:1
帖 子:9
专家分:40
注 册:2014-3-1
得分:0 
接收的消息没有添加到 socketlist 中  这个集合是用来存放socket客户端对象的而不是消息
我的建议是写异步的接受方法
public void StartServer()
{
  IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(iPEndPoint);
            socket.Listen(20);
            socket.BeginAccept(new AsyncCallback(AcceptClient), socket);
        }

        public void AcceptClient(IAsyncResult result)
        {
            Socket server = result.AsyncState as Socket;
            Socket client = server.EndAccept(result);
            while (true)
            {
                try
                {
                    client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMsg), client);
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }
            }
        }
        public void ReceiveMsg(IAsyncResult result)
        {
            Socket client = result.AsyncState as Socket;
            client.EndReceive(result);
            string msg = System.Text.Encoding.Default.GetString(buffer);
            this.BeginInvoke(new Action(() =>
            {
                textBoxServer.Text += msg + "/r/n";
            }));
        }
2018-09-10 16:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-488438-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056959 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved