1:服务器端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;// 包含IPAddress与IPEndPoint类
using System.Threading;
namespace MyChatRoomServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
Thread watchThread = null;
Socket watchSocket = null;
Thread connectThread = null;
private void button1_Click(object sender, EventArgs e)
{
//声明监听套接字 参数(使用IP4的寻址协议,使用流式连接,使用Tcp数据传输协议)
watchSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定端口
IPAddress ipAddress = IPAddress.Parse(this.textBox1.Text.Trim());
//创建包含ip和port的网络节点对象
IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, int.Parse(this.textBox2.Text.Trim()));
//将监听的队列绑定到唯一的ip和端口上
watchSocket.Bind(ipEndpoint);
//设置监听队列的长度
watchSocket.Listen(10);
watchThread = new Thread(watchConnection);
watchThread.IsBackground = true;
watchThread.Start();
showMsg("服务端监听成功!");
}
Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
Dictionary<string, Thread> dict1 = new Dictionary<string, Thread>();
void watchConnection()
{
while (true)//持续不断的进行监听
{
//会阻塞线程
Socket connectSocket = watchSocket.Accept();
dict.Add(connectSocket.RemoteEndPoint.ToString(), connectSocket);
listBox1.Items.Add(connectSocket.RemoteEndPoint.ToString());
showMsg("客户端连接成功! ");
ParameterizedThreadStart td=new ParameterizedThreadStart(chart);
Thread td1 = new Thread(td);
dict1.Add(connectSocket.RemoteEndPoint.ToString(),td1);
td1.Start(connectSocket);
}
}
private void showMsg(string message)
{
this.txtMsg.AppendText(message+ "\r\n");
}
private void button2_Click(object sender, EventArgs e)
{
string strMsg = this.txtMsgSend.Text.Trim();
byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
dict[listBox1.Text].Send(arrMsg);
showMsg("你已发送了:" + strMsg);
}
private void txtMsgSend_TextChanged(object sender, EventArgs e)
{
}
void chart(object socketPara)
{
Socket socket = socketPara as Socket;
while (true)
{
byte[] arrMsgRec = new byte[1024 * 1024 * 2];
int length = socket.Receive(arrMsgRec);
string myMessage = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
showMsg(myMessage);
}
}
}
}
2:客户端通信
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace MychatClient
{
public partial class FChatClient : Form
{
public FChatClient()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
Socket connectSocket = null;
Thread connectThread = null;
private void button1_Click(object sender, EventArgs e)
{
IPAddress ipAddress = IPAddress.Parse(this.textBox1.Text.Trim());
IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, int.Parse(this.textBox2.Text.Trim()));
connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectSocket.Connect(ipEndpoint);
connectThread = new Thread(chart);
connectThread.IsBackground = true;
connectThread.Start();
}
void chart()
{
while (true)
{
byte[] arrMsgRec = new byte[1024 * 1024 * 2];
int length = connectSocket.Receive(arrMsgRec);
string myMessage = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
showMsg(myMessage);
}
}
private void showMsg(string message)
{
this.txtMsg.AppendText(message + "\r\n");
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
string strMsg = this.txtMsgSend.Text.Trim();
byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
connectSocket.Send(arrMsg);
showMsg("你已发送了:" + strMsg);
}
}
}