你字体颜色信息也要通过socket传输过来的

解决方案 »

  1.   

    这是我的服务器端的代码: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;
    using System.Net.Sockets;
    using System.Threading;namespace 聊天程序
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private IPAddress myip = IPAddress.Parse("127.0.0.1");
            private IPEndPoint server;
            private Socket sesock;
            private Socket resock;
            private bool flag = true;        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    myip = IPAddress.Parse(this.textBox1.Text);            }
                catch
                {
                    MessageBox.Show("IP地址输入有误!");
                }
                try
                {
                    server = new IPEndPoint(myip, 80);
                    sesock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                    sesock.Bind(server);
                    sesock.Listen(50);
                    resock = sesock.Accept();
                    Thread th = new Thread(new ThreadStart(tar));
                    th.Start();
                }
                catch(Exception excep)
                {
                    MessageBox.Show(excep.Message);
                }
            }
            private void tar()
            {
                if (resock.Connected)
                {
                    while (flag)
                    {
                        Byte[] rdata=new Byte[64];
                        resock.Receive(rdata,rdata.Length,0);
                        string str = System.Text.Encoding.BigEndianUnicode.GetString(rdata);
                        this.richTextBox1.AppendText(str+"\n");
                    }
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                    Byte[] sdata = new Byte[64];
                    string ss = this.textBox3.Text + " " + DateTime.Now.ToString() + "\n" + this.richTextBox2.Text+"\n";
                    sdata = System.Text.Encoding.BigEndianUnicode.GetBytes(ss.ToCharArray());
                    resock.Send(sdata, sdata.Length, 0);
                    this.richTextBox1.AppendText(ss);
                    this.richTextBox2.Clear();
            }        private void button5_Click(object sender, EventArgs e)
            {
                FontDialog font = new FontDialog();
                font.Color = this.richTextBox2.ForeColor;
                if (font.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox2.SelectionFont = font.Font;
                }
            }        private void button6_Click(object sender, EventArgs e)
            {
                ColorDialog color = new ColorDialog();
                if (color.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox2.SelectionColor = color.Color;
                }
            }        private void button3_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                string str=Dns.GetHostName();
                IPAddress[] address = Dns.GetHostAddresses(str);
                for (int i = 0; i < address.Length; i++)
                {
                    string ss = address[i].ToString();
                    MessageBox.Show(ss);
                }  
            }
        }
    }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;
    using System.Net.Sockets;
    using System.Threading;namespace 聊天程序
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private IPAddress myip = IPAddress.Parse("127.0.0.1");
            private IPEndPoint server;
            private Socket sesock;
            private Socket resock;
            private bool flag = true;        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    myip = IPAddress.Parse(this.textBox1.Text);            }
                catch
                {
                    MessageBox.Show("IP地址输入有误!");
                }
                try
                {
                    server = new IPEndPoint(myip, 80);
                    sesock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                    sesock.Bind(server);
                    sesock.Listen(50);
                    resock = sesock.Accept();
                    Thread th = new Thread(new ThreadStart(tar));
                    th.Start();
                }
                catch(Exception excep)
                {
                    MessageBox.Show(excep.Message);
                }
            }
            private void tar()
            {
                if (resock.Connected)
                {
                    while (flag)
                    {
                        Byte[] rdata=new Byte[64];
                        resock.Receive(rdata,rdata.Length,0);
                        string str = System.Text.Encoding.BigEndianUnicode.GetString(rdata);
                        this.richTextBox1.AppendText(str+"\n");
                    }
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                    Byte[] sdata = new Byte[64];
                    string ss = this.textBox3.Text + " " + DateTime.Now.ToString() + "\n" + this.richTextBox2.Text+"\n";
                    sdata = System.Text.Encoding.BigEndianUnicode.GetBytes(ss.ToCharArray());
                    resock.Send(sdata, sdata.Length, 0);
                    this.richTextBox1.AppendText(ss);
                    this.richTextBox2.Clear();
            }        private void button5_Click(object sender, EventArgs e)
            {
                FontDialog font = new FontDialog();
                font.Color = this.richTextBox2.ForeColor;
                if (font.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox2.SelectionFont = font.Font;
                }
            }        private void button6_Click(object sender, EventArgs e)
            {
                ColorDialog color = new ColorDialog();
                if (color.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox2.SelectionColor = color.Color;
                }
            }        private void button3_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                string str=Dns.GetHostName();
                IPAddress[] address = Dns.GetHostAddresses(str);
                for (int i = 0; i < address.Length; i++)
                {
                    string ss = address[i].ToString();
                    MessageBox.Show(ss);
                }  
            }
        }
    }
      

  2.   

    如果不连接数据库,只用sockets这一块的知识可以的话最好了,谢谢各位