C#基于Socket实现简单聊天室功能_第1页
C#基于Socket实现简单聊天室功能_第2页
C#基于Socket实现简单聊天室功能_第3页
C#基于Socket实现简单聊天室功能_第4页
C#基于Socket实现简单聊天室功能_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第C#基于Socket实现简单聊天室功能因为这段时间在学习Socket,所以就试着写了一个简单的聊天室。主要分为服务器端和多个客户端。利用服务器端作数据中转站,实现消息群发。

1、服务器端有两个类:

usingSystem.Collections.Generic;

usingSystem.Net;

usingSystem.Net.Sockets;

namespace聊天室_Socket_TCP_服务器端

classProgram

{

staticListClientclients=newListClient

staticListClientnotClients=newListClient

///summary

///广播消息

////summary

///paramname="message"/param

publicstaticvoidCastMessageTOAllConnetedClients(stringmessage)

{

foreach(varclientinclients)

{

if(client.Conneted)

{

client.CastMessage(message);

}

else

{

notClients.Add(client);

}

}

foreach(vartempinnotClients)

{

clients.Remove(temp);

}

}

staticvoidMain(string[]args)

{

SockettcpSever=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

tcpSever.Bind(newIPEndPoint(IPAddress.Parse(""),8899));

tcpSever.Listen(100);//监听是否有客户端发起连接

Console.WriteLine("Begintolisten...");

while(true)

{

SocketclientSocket=tcpSever.Accept();

if(clientSocket!=null)

{

Console.WriteLine("Aclienthasconnneted...");

Clientclient=newClient(clientSocket);//将每个新创建的连接通信放于client类做通信

clients.Add(client);

}

}

Console.ReadKey();

}

}

}

usingSystem;

usingSystem.Net.Sockets;

usingSystem.Text;

usingSystem.Threading;

namespace聊天室_Socket_TCP_服务器端

///summary

///利用该类和客户端做通信

////summary

classClient

{

publicSocketclientSocket;

privateThreadmesManageTherad;

privatebyte[]bufffer=newbyte[20];

publicClient(Socketsoc)

{

clientSocket=soc;

//由于消息是不断发送的,需要多次进行处理。这里开一个线程,专门用来处理消息。

mesManageTherad=newThread(MessageSendFromClient);

mesManageTherad.Start();

}

privatevoidMessageSendFromClient()

{

//开启的线程一直检测客户端客户端发过来的消息

while(true)

{

//判断连接是否断开,

SelectMode.SelectRead读状态模式。

//poll已断开返回true

if(clientSocket.Poll(10,SelectMode.SelectRead)==true)

{

clientSocket.Close();

break;//终止本线程

}

intbyteNum=clientSocket.Receive(bufffer);//从客户端接受消息

stringmes=Encoding.UTF8.GetString(bufffer,0,byteNum);

Console.WriteLine("客户端发送过来的消息:"+mes);

//广播消息出去给每个客户端

Program.CastMessageTOAllConnetedClients(mes);//对CastMessage的一层封装

}

}

///summary

///SendmessagestoClients

////summary

publicvoidCastMessage(stringmessage)

{

byte[]data=Encoding.UTF8.GetBytes(message);

clientSocket.Send(data);

}

///summary

///判断是否断开连接

////summary

publicboolConneted

{

get

{

returnclientSocket.Connected;

}

}

}

}

服务器端逻辑:

这里的服务器主要负责建立连接,接受客户端消息,广播客户端发来的消息。

服务器通过socket对象绑定服务器IP和相应端口号(端口号自己开,没有被其他软件占用就好),通过Listen监听和服务器socket对象的Accept方法捕捉连接到服务器的客户端socket,将捕捉到的客户端socket放入List集合中方便统一管理和后面的消息群发。

关于捕捉到的客户端socket的逻辑处理放在了Client类中统一管理。

Client类将收到客户端的消息进行接受,在Client中开启一个线程用于不断得检测是否有新消息从客户端发送过来,若有消息发送过来则通过CastMessageTOAllConnetedClients方法(对socket对象的Send方法的封装)发送给每一个客户端。

2.客户端

客户端是在Unity中使用NGUI插件简单开发的一个聊天界面。把脚本挂在NGUI控件上即可。客户端主要负责显示消息,发送消息,接收消息。

usingSystem.Net;

usingSystem.Net.Sockets;

usingSystem.Text;

usingSystem.Threading;

usingUnityEngine;

publicclassChatManager:MonoBehaviour{

privatestring_ipAdress="";

privateint_port=8899;

EndPointremotPoint;

SocketclientSocket;

publicUIInputbuttonInp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论