设为首页 加入收藏
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 下载中心 服务专区 客服中心
服务项目
案例展示 更多 >>
·维修管理系统 2012-5-14
·园区游客服务管理系统 2012-5-10
·党风廉政信息公开平台 2012-5-10
·餐饮无线点菜系统 2012-1-11
·职工提案系统软件 2012-1-11
·电信客户资源管理系统软… 2012-1-11
·移动停电通知系统 2012-1-11
·洗衣收银软件 2012-1-11
·触摸查询系统软件 2012-1-11
·西安来电弹屏软件 2011-8-31
联系人:李先生
电  话:029-87878512
手  机:13468700578
地  址:西安市太白北路1号
在线咨询:  762176615
Email:junsoft@126.com
 
当前的位置 >> 返回首页 >> 解决方案
类似于QQ聊天技术方案
发布者:金龙国际彩票   发布时间:2012-12-3   阅读:5次


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;

namespace MyQQSer
{
    class Serv
    {
        private TcpListener listenr;
        private Thread thread;
        private bool isClose = false;    //结束程序是退出循环
        private Socket[] sock = new Socket[50];

        public Serv()
        {
            Listener();
        }

        ~Serv()
        {
            isClose = true;
            for (int i = 0; i < sock.Length; i++)
            {
                if (sock[i] != null)
                    sock[i].Close();
                listenr.Stop();
            }
        }

        /// <summary>
        /// 开始监听端口
        /// </summary>
        public void Listener()
        {
            if (listenr == null)
                listenr = new TcpListener(IPAddress.Parse("127.0.0.1"), 5000);
            listenr.Start();
            if (thread == null)
                thread = new Thread(new ThreadStart(ReciverMessage));
            thread.IsBackground = true;
            thread.Start();
        }

        /// <summary>
        /// 接收数据
        /// </summary>
        private void ReciverMessage()
        {
            int count; //可用套结字索引
            count = GetCount();
            bool loop = true;
            if (count == -1)
                loop = false;
            if (loop)
            {
                while (true)
                {
                    // 判断是否退出循环
                    if (isClose)
                        break;                   
                    sock[count] = listenr.AcceptSocket();
                    Thread t = new Thread(new ThreadStart(ReciverMessage));
                    t.IsBackground = true;
                    t.Start();
                    // 接受客户端数据
                    while (true)
                    {
                        byte[] buffs = new byte[100];
                        if (sock[count].Connected)
                        {
                            try
                            {
                                sock[count].Receive(buffs);
                                string message = Encoding.Default.GetString(buffs);
                                Console.WriteLine(message);
                                SendToAll(buffs);
                            }
                            catch (Exception ex)
                            {

                            }
                        }
                    }
                }
            }
            Thread.CurrentThread.Abort();
        }

        // 得到可用套结字索引
        private int GetCount()
        {
            for (int i = 0; i < sock.Length; i++)
                if (sock[i] == null)
                    return i;
            return -1;
        }

        // 发送消息给所有人
        private void SendToAll(byte[] buff)
        {
            for (int i = 0; i < sock.Length; i++)
            {
                if (sock[i] != null)
                {
                    if (sock[i].Connected)
                    {
                        sock[i].Send(buff);
                    }
                }
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace TestClient
{
    public partial class Form1 : Form
    {
        private TcpClient client;
        private Thread t;
        private Socket sock;
        private bool close = false;

        public Form1()
        {
            InitializeComponent();
        }

        // 连接
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if(client == null)
                client = new TcpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(txtLoalPort.Text)));

            if (t == null)
                t = new Thread(new ThreadStart(ReciverMessage));
            t.IsBackground = true;
            t.Start();
            btnSend.Enabled = true;
            btnConnect.Enabled = false;
        }
       
        // 接收消息
        private void ReciverMessage()
        {
            client.Connect(IPAddress.Parse(txtSerIP.Text), int.Parse(txtSerPort.Text));
            while (true)
            {
                if (close)
                    break;
                    //NetworkStream nws = client.GetStream();
                    //StreamReader sr = new StreamReader(nws);
                    //string message = sr.ReadToEnd();
                    //sr.Close();
                    if(sock == null)
                            sock = client.Client;
                    byte[] buff = new byte[100];
                    sock.Receive(buff);
                    string message = Encoding.Default.GetString(buff);
                    this.lstMessage.Items.Add(message);               
            }
            Thread.CurrentThread.Abort();
        }

        // 发送消息
        private void btnSend_Click(object sender, EventArgs e)
        {
            string message = this.txtInput.Text;
            byte[] buff = Encoding.Default.GetBytes(message);
            sock.Send(buff);
            txtInput.Text = "";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            close = true;
            sock.Close();
        }
    }
}


using System;
 
namespace Remotable
{
    
    public class RemotableType : MarshalByRefObject
    {
        private string _internalString = "This is the RemotableType.";
        public string StringMethod()
        {
            return _internalString;
        }
    }
 
}

 

using System;
using System.Runtime.Remoting;
 
 
namespace RemotingFirst
{
 
    public class Listener
    {
        public static void Main()
        {
            RemotingConfiguration.Configure("Listener.exe.config");
            Console.WriteLine("Listening for requests. Press Enter to exit");
            Console.ReadLine();
        }
    }
 
}


using System;
using System.Runtime.Remoting;
 
 
namespace Client
{
 
 
    public class Client
    {
 
        public static void Main()
        {
            RemotingConfiguration.Configure("Client.exe.config");
            Remotable.RemotableType remoteObject = new Remotable.RemotableType();
            Console.WriteLine(remoteObject.StringMethod());
        }
    }
 
}


Listener.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
      <application>
         <service>
            <wellknown 
               mode="Singleton" 
               type="Remotable.RemotableType, RemotableType" 
               objectUri="RemotableType.rem"
            />
         </service>
         <channels>
            <channel ref="http" port="8989"/>
         </channels>
      </application>
    </system.runtime.remoting>
</configuration>


网站首页 | 关于我们 | 售后服务 | 网站地图 | 查看留言 | 在线留言 | 客服中心
© 版权所有:app金龙彩票科技有限公司 
公司地址:西安市太白北路1号 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2009-2017 Xbwbw.com Inc. All Rights Reserved 
技术支持:app金龙彩票科技有限公司 陕ICP备 11000720号-2
<友情连结> 金龙彩票平台网址/ 八八彩票投注/ 黄金彩票官方网站/