本帖最后由 大虾挂了 于 2013-10-21 12:47 编辑
先分享一下我的方法。下面是服务端的代码,颜色字体部分为服务端接收客户端发送数据的相关部分
后面附件会补上所有的程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace 窗体
{
/// <summary>
/// 服务端接收客户端信息时,该结构体作为接收线程中委托的参数
/// key用于标识当前通信套接字的远程IP和端口
/// sao即是当前与客户端进行通信的套接字
/// </summary>
public struct temp
{
public string key;
public Socket sao;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
Socket socketWatch = null;
Dictionary<string,Socket> dic=new Dictionary<string,Socket>();
Dictionary<string, Thread> threaddic = new Dictionary<string, Thread>();
private void begin_Click(object sender, EventArgs e)
{
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(textIp.Text.Trim());
IPEndPoint jiedian = new IPEndPoint(address, int.Parse(textPoint.Text.Trim()));
socketWatch.Bind(jiedian);
socketWatch.Listen(10);
Thread threadWatch = new Thread(connect);
threadWatch.IsBackground = true;
threadWatch.Start();
}
public void connect()
{
while (true)
{
Socket socketConnection = socketWatch.Accept();
//将负责通信的套接字放在dic字典里,远程IP和端口作为键
dic.Add(socketConnection.RemoteEndPoint.ToString(),socketConnection);
//将远程IP和端口放入列表中
lbOnline.Items.Add(socketConnection.RemoteEndPoint.ToString());
//信息提示哪个客户端连接成功
ShowMsg(socketConnection.RemoteEndPoint.ToString()+"连接成功");
//每次有一个新的客户端接入,就为其准备一个线程,将这个线程放入threaddic字典里,远程IP和端口作为键
threaddic.Add(socketConnection.RemoteEndPoint.ToString(), new Thread(MsgReceive));
//将作为委托参数的结构体初始化
temp tp = new temp() {key=socketConnection.RemoteEndPoint.ToString(),sao=socketConnection };
threaddic[socketConnection.RemoteEndPoint.ToString()].IsBackground = true;
//接收对应客户端信息的线程开始运行
threaddic[socketConnection.RemoteEndPoint.ToString()].Start(tp);
}
}
public void MsgReceive(Object o)
{
//将传入的参数转化为temp结构体,存入mid中
temp mid = (temp)o;
while (true)
{
byte[] MsgReceive = new byte[1024];
//调用对应套接字(mid.sao)的Receive方法
int length = mid.sao.Receive(MsgReceive);
string strMsg = System.Text.Encoding.UTF8.GetString(MsgReceive, 0, length);
textMsg.AppendText("接受了" +"\""+ strMsg+"\""+"从"+mid.key+"\n");
}
}
public void ShowMsg(string a)
{
textMsg.AppendText( a + "\r\n");
}
private void btnSend_Click(object sender, EventArgs e)
{
byte[] MsgSend = System.Text.Encoding.UTF8.GetBytes(textSend.Text.Trim());
dic[lbOnline.Text].Send(MsgSend);
textMsg.AppendText("发送了" + "\""+textSend.Text.Trim()+"\"" + "给" + lbOnline.Text+"\n"); }
}
}
客户端的代码就不发了
贴几张效果图好了
|
-
1.JPG
(76.71 KB, 下载次数: 44)
服务端开启,两个客户端开始连接
-
2.JPG
(78.92 KB, 下载次数: 48)
服务端分别向两个客户端发送信息
-
3.JPG
(79.61 KB, 下载次数: 51)
客户端轮流向服务端发送信息
-
-
客户端和服务端.rar
105.52 KB, 下载次数: 118
|