| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 811 人关注过本帖
标题:我要编一个跟qq差不多的程序,老师编的代码我看不懂,请前辈们帮我做一下注 ...
只看楼主 加入收藏
Clanguagenet
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-12-10
收藏
 问题点数:0 回复次数:3 
我要编一个跟qq差不多的程序,老师编的代码我看不懂,请前辈们帮我做一下注释什么的,谢谢!
这是服务器端的代码:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using System.Threading;
using System.Collections;
namespace tcpS
{
    public partial class Form1 : Form
    {
        Hashtable onLineUsers;
        TcpListener listener;
        Thread listenThread;
        DoTrans dotrans;
        public Form1()
        {
            InitializeComponent();
            onLineUsers = new Hashtable();
            listener = new TcpListener(6666);
            listenThread =
                new Thread(new ThreadStart(listen));
            dotrans = new DoTrans();
        }
        /// <summary>
        /// 监听6666端口,接受所有客户端程序的链接请求
        /// 建立通信信道
        /// </summary>
        private void listen()
        {
            listener.Start();//开始监听6666,看有人连我么
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();//阻塞
                //service(client);
                Thread serviceT =
                    new Thread(new ParameterizedThreadStart(service));
                serviceT.Start(client);
            }
        }
        /// <summary>
        /// 为C提供它所要求的服务
        /// </summary>
        /// <param name="C"></param>
        private void service(object bj)
        {
            TcpClient C = bj as TcpClient;
            NetworkStream ns = C.GetStream();
            byte[] mss=new byte[1024];
            byte[] replay;
            while (true)
            {
                ns.Read(mss, 0, 1024);
                string message =Encoding.UTF8.GetString(mss);
                string[] ms = message.Split(new char[] { '^'});
                switch (ms[0])
                {
                    case "login"://login^zjl^123^
                        DataTable dt = dotrans.SelTran("select * from t_user where userid='"+ms[1]+"'");
                        if(dt==null||dt.Rows.Count==0)
                        {//无此用户
                            replay =Encoding.UTF8.GetBytes("login^nouser^");
                        }
                        else
                        {
                            if (dt.Rows[0]["password"].ToString() == ms[2])
                            {//登录成功
                                replay = Encoding.UTF8.GetBytes("login^ok^");
                                onLineUsers.Add(ms[1], C);
                            }
                            else
                            {//密码错误
                                replay = Encoding.UTF8.GetBytes("login^pwdWrong^");
                            }
                        }
                        ns.Write(replay, 0, replay.Length);
                        ns.Flush();
                        break;
                    case "chat"://chat^zjl^awg^safsa^
                        TcpClient tempTC = onLineUsers[ms[2]] as TcpClient;
                        NetworkStream tempNS=tempTC.GetStream();
                        ns.Write(mss,0,mss.Length);
                        break;
                    case "quit":
                        onLineUsers.Remove(ms[1]);
                        break;
                    case "adduser"://adduser^zjl^123^张飞^
                        if (dotrans.DoTran("insert into t_user values('"+ms[1]+"','"+ms[3]+"','"+ms[2]+"')"))
                        {
                            replay = Encoding.UTF8.GetBytes("adduser^ok^");
                        }
                        else
                        {
                            replay = Encoding.UTF8.GetBytes("adduser^error^");
                        }
                        ns.Write(replay, 0, replay.Length);
                        ns.Flush();
                        break;
                    case "refreshU"://UserList^zjl^awg^zj^zxm^....
                        //onLineUsers的key:所有在线用户的编号
                        string replayStr = "UserList^";
                        foreach (object obj in onLineUsers.Keys)
                        {
                            replayStr += obj.ToString() + "^";
                        }
                        replay = Encoding.UTF8.GetBytes(replayStr);
                        ns.Write(replay, 0, replay.Length);
                        ns.Flush();
                        break;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            listenThread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
搜索更多相关主题的帖子: 注释 前辈 老师 代码 
2010-12-10 11:11
q410
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-5-22
收藏
得分:0 
哎没有一个高手注释,看来高手都去钓鱼了。
2011-05-22 00:44
记叙、继续
Rank: 4
等 级:业余侠客
帖 子:56
专家分:226
注 册:2011-5-17
收藏
得分:0 
额,都没有看到用户界面的语句?qq真的这么简单!
2011-05-22 12:57
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:0 
楼上不懂了吧?
——————————————————————————
这是一个C# .Net Socket程序。这个程序是最简单的通信
程序好吧?
我不给你注释。只告诉你需要去了解什么。OK?
1、C#的基础知识。这是肯定必要的。包括一些重要的类,
像String字符串的操作呀、编码方式啊等等。基础好了,
干什么不是个干。
2、C# .NET多线程编程。这个程序用到的多线程非常简单。
你可以上网搜一个C#的多线程教程学一下。
3、C# .NET SOCKET编程。.NET Framework封装这么好,而
且有现成的TCP通信方式的Socket类,了解一下Socket TCP
的通信方式即可。网上还有视频教程。
4、。我看源码里面用到的简单的数据库操作。其实
无非就是调用SQL语句。了解一下C# .NET的数据库操作即可。
用C# .NET,完全傻瓜式的数据库操作。很快能就学会了。
2011-05-25 11:53
快速回复:我要编一个跟qq差不多的程序,老师编的代码我看不懂,请前辈们帮我做一 ...
数据加载中...
 
   



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

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