| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1838 人关注过本帖
标题:c# 彩笔向大家请教一个多线程socket 问题
取消只看楼主 加入收藏
Free2010
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-17
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
c# 彩笔向大家请教一个多线程socket 问题

使用CMD telnet 登陆到 多线程服务端程序发生异常,描述如下 : 在执行此操作前必须先调用 Listen 方法


代码如下:

using System;

using

using

using System.Text;

using System.Threading;


namespace socket_demo2
{

    class Server
    {

        Thread thread;

        ServerThread serverThread;

        Socket socket;

        IPAddress ip;

        IPEndPoint ipep;

        byte[] byteMessage;

        public Server()
        {
            Console.WriteLine("Server...............");
            init();
           
        }


        public void init()
        {
            ip = IPAddress.Parse("127.0.0.1");

            ipep = new IPEndPoint(ip,12);

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(ipep);

            socket.Listen(5);
            Console.WriteLine("listen...............");
            Console.WriteLine("Wait for connect");

            thread = new Thread(new ThreadStart(this.accept));

            thread.Start();

        }

        public void accept()
        {
           
            while (true)
            {

               

                socket = socket.Accept();

                Console.WriteLine("accept..............." + socket.RemoteEndPoint.ToString());

                serverThread = new ServerThread(socket);

            }
        }


        static void Main(string[] args)
        {

            Server s = new Server();
        }

    }



}




using System;

using

using

using System.Text;

using System.Threading;

namespace socket_demo2
{
    class ServerThread
    {
        Thread thread;

        Socket socket;

        byte[] byteMessage  = new byte[1024];

        public ServerThread()
        {
               
        }

        public ServerThread(Socket socket)
        {


            Console.WriteLine("ServerThread  init ............. ");

            this.socket = socket;

            thread = new Thread(new ThreadStart(this.run));

            thread.Start();

        }

        public void run()
        {

            Console.WriteLine("ServerThread  run ............. ");


            socket.Receive(byteMessage);

            string time = DateTime.Now.ToShortTimeString();

            string msg = time + ":" + "Message from:";

            msg += socket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);

            Console.WriteLine(msg + "\n");

            this.socket.SendTo(Encoding.ASCII.GetBytes(msg), this.socket.RemoteEndPoint);


        }

    }
}


麻烦各位哥帮忙看一下  
搜索更多相关主题的帖子: 彩笔 线程 socket 
2010-03-17 10:54
Free2010
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-17
收藏
得分:0 
socket = socket.Accept();  运行到这一句报错
2010-03-17 15:02
Free2010
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-17
收藏
得分:0 
Thread thread = new Thread(new ThreadStart(this.accept));
 

  改成这样了 ,不过问题还是没解决哦 !
2010-03-17 15:05
Free2010
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-17
收藏
得分:0 
  谢谢 Socket s = socket.Accept();  之前的定义确实有问题   .   
2010-03-18 14:42
快速回复:c# 彩笔向大家请教一个多线程socket 问题
数据加载中...
 
   



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

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