使用套接字的聊天程序 你字体颜色信息也要通过socket传输过来的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我的服务器端的代码: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); } } }} 如果不连接数据库,只用sockets这一块的知识可以的话最好了,谢谢各位 "未解析成员"错误 C# 统计表用什么做好! 如何在点击listbox不出现蓝条和虚线框? 初始化SqlParameter的问题 代码调用操作系统的搜索功能可以吗?? 怎么设置DataGridView第一行第一列的显示内容?最左上角那个. C#使用相对地址 急需购买web打印控件(C#),请留言!!!!!!!!!!!!!! wince在DataGrid里如何加按钮 C#编写的 谢谢 有没有比较好用报表工具?? LineShape画的直线被TreeView覆盖了怎么解决 开发自定义控件
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);
}
}
}
}