| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 745 人关注过本帖
标题:基于Socket的点对点聊天程序
只看楼主 加入收藏
vlken
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-11-27
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
基于Socket的点对点聊天程序
namespace 聊天程序
{
    public partial class Form1 : Form
    {
        private int listenport = 5555;
        private TcpListener listener;
        private Socket clientsocket;
        private Thread processor;
        private Thread clientservice;

        private int serverport;
        private NetworkStream ns;
        private TcpClient tcpclient;
        private string serveraddress;
        public Form1()
        {
            InitializeComponent();
            serverport = 5555;
            processor = new Thread(new ThreadStart(StartListening));
            processor.Start();
        }
        private void StartListening()
        {
            listener = new TcpListener(listenport);
            listener.Start();
            while (true)
            {
                try
                {
                    Socket s = listener.AcceptSocket();
                    clientsocket = s;
                    clientservice = new Thread(new ThreadStart(ServiceClient));
                    clientservice.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }

        }
        private void ServiceClient()
        {
            Socket client = clientsocket;
            Byte[] buffer = new Byte[1024];
            client.Receive(buffer);
            string clientcommand = System.Text.Encoding.ASCII.GetString(buffer);
            listBox1.Items.Add("对方:" + DateTime.Now.ToString() + ":" + clientcommand);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
            client.Close();
            clientservice.Abort();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                serveraddress = txtIp.Text;
                tcpclient = new TcpClient(serveraddress, serverport);
                ns = tcpclient.GetStream();
                listBox1.Items.Add("已方:" + DateTime.Now.ToString() + ":" + txtpe.Text);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
                Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(txtpe.Text.ToCharArray());
                ns.Write(outbytes, 0, outbytes.Length);
                ns.Close();
                tcpclient.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not connet to Server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("你确定要退出?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (clientsocket != null)
                    clientsocket.Close();
                if (clientservice != null)
                    clientservice.Abort();
                listener.Stop();
                if (processor != null)
                    processor.Abort();
            }
            else
                e.Cancel = true;
        }
    }
}
 
这个是一个点对点的基于Socket的聊天程序,只能发英文字母,我想用StreamReader和StreamWriter改下可以改成可以用中文聊天,但是尝试过很多篇都失败了!求解下!
搜索更多相关主题的帖子: Socket 聊天 
2010-11-27 19:09
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:5 
用ASCII是不行的,尝试用gb2312编码方式读写。
2010-11-29 09:10
makebest
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:658
专家分:962
注 册:2005-3-17
收藏
得分:5 
用 Encoding.UTF8 代替 Encoding.ASCII.
2010-12-01 10:18
快速回复:基于Socket的点对点聊天程序
数据加载中...
 
   



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

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