问题描述
学习C#时,在网络编程基础那一章,课本上讲了一个使用套接字的客户端与服务器端的聊天程序。其中窗体上都含有两个richtextbox控件,就像qq一样,一个是输入信息的,另一个是发送之后显示信息和接受信息的。我现在把richtextbox2中的字体颜色和大小设置好,但是发送之后在richtextbox1中显示的字体和颜色仍然是系统默认的,不是我在richtextbox2中所设置的那样,请各位帮忙指点一下
解决方案
解决方案二:
不用数据库,只用套接字这一块的只是能不能做到?
解决方案三:
你字体颜色信息也要通过socket传输过来的
解决方案四:
贴上你的代码看看
解决方案五:
用那个能行的!
解决方案六:
代码过几天给你!有待考虑!
解决方案七:
这是我的服务器端的代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespace聊天程序{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privateIPAddressmyip=IPAddress.Parse("127.0.0.1");privateIPEndPointserver;privateSocketsesock;privateSocketresock;privateboolflag=true;privatevoidbutton1_Click(objectsender,EventArgse){try{myip=IPAddress.Parse(this.textBox1.Text);}catch{MessageBox.Show("IP地址输入有误!");}try{server=newIPEndPoint(myip,80);sesock=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);sesock.Bind(server);sesock.Listen(50);resock=sesock.Accept();Threadth=newThread(newThreadStart(tar));th.Start();}catch(Exceptionexcep){MessageBox.Show(excep.Message);}}privatevoidtar(){if(resock.Connected){while(flag){Byte[]rdata=newByte[64];resock.Receive(rdata,rdata.Length,0);stringstr=System.Text.Encoding.BigEndianUnicode.GetString(rdata);this.richTextBox1.AppendText(str+"n");}}}privatevoidbutton2_Click(objectsender,EventArgse){Byte[]sdata=newByte[64];stringss=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();}privatevoidbutton5_Click(objectsender,EventArgse){FontDialogfont=newFontDialog();font.Color=this.richTextBox2.ForeColor;if(font.ShowDialog()==DialogResult.OK){this.richTextBox2.SelectionFont=font.Font;}}privatevoidbutton6_Click(objectsender,EventArgse){ColorDialogcolor=newColorDialog();if(color.ShowDialog()==DialogResult.OK){this.richTextBox2.SelectionColor=color.Color;}}privatevoidbutton3_Click(objectsender,EventArgse){Application.Exit();}privatevoidForm1_Load(objectsender,EventArgse){stringstr=Dns.GetHostName();IPAddress[]address=Dns.GetHostAddresses(str);for(inti=0;i<address.Length;i++){stringss=address[i].ToString();MessageBox.Show(ss);}}}}usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespace聊天程序{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privateIPAddressmyip=IPAddress.Parse("127.0.0.1");privateIPEndPointserver;privateSocketsesock;privateSocketresock;privateboolflag=true;privatevoidbutton1_Click(objectsender,EventArgse){try{myip=IPAddress.Parse(this.textBox1.Text);}catch{MessageBox.Show("IP地址输入有误!");}try{server=newIPEndPoint(myip,80);sesock=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);sesock.Bind(server);sesock.Listen(50);resock=sesock.Accept();Threadth=newThread(newThreadStart(tar));th.Start();}catch(Exceptionexcep){MessageBox.Show(excep.Message);}}privatevoidtar(){if(resock.Connected){while(flag){Byte[]rdata=newByte[64];resock.Receive(rdata,rdata.Length,0);stringstr=System.Text.Encoding.BigEndianUnicode.GetString(rdata);this.richTextBox1.AppendText(str+"n");}}}privatevoidbutton2_Click(objectsender,EventArgse){Byte[]sdata=newByte[64];stringss=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();}privatevoidbutton5_Click(objectsender,EventArgse){FontDialogfont=newFontDialog();font.Color=this.richTextBox2.ForeColor;if(font.ShowDialog()==DialogResult.OK){this.richTextBox2.SelectionFont=font.Font;}}privatevoidbutton6_Click(objectsender,EventArgse){ColorDialogcolor=newColorDialog();if(color.ShowDialog()==DialogResult.OK){this.richTextBox2.SelectionColor=color.Color;}}privatevoidbutton3_Click(objectsender,EventArgse){Application.Exit();}privatevoidForm1_Load(objectsender,EventArgse){stringstr=Dns.GetHostName();IPAddress[]address=Dns.GetHostAddresses(str);for(inti=0;i<address.Length;i++){stringss=address[i].ToString();MessageBox.Show(ss);}}}}
解决方案八:
如果不连接数据库,只用sockets这一块的知识可以的话最好了,谢谢各位