admin 管理员组

文章数量: 887021


2023年12月22日发(作者:大话西游2源码下载)

怎么使用Socket实现局域网聊天

局域网通常是分布在一个有限地理范围内的网络系统,一般所涉及的地理范围只有几公里。局域网专用性非常强,具有比较稳定和规范的拓扑结构。这篇文章主要为大家详细介绍了C#使用Socket实现局域网聊天的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

C#代码:

servlet服务端

using System;

using c;

using g;

using ;

using ;

using s;

using ing;

using ;

using ;

using rExpressions;

namespace Server

{

public partial class MainForm : Form

{

private TcpListener listener;

private Dictionary socketList;

private bool tag = true;

private StringBuilder charList;

public MainForm()

{

InitializeComponent();

orIllegalCrossThreadCalls = false;

}

void Bu_StartClick(object sender, EventArgs e)

{

cb_();

="";

int port = 8888;

//创建服务端,并且启动

try{

listener = new TcpListener((ipAddress()),port);

();

bu_d = false;

bu_d = true;

}catch(Exception ex)

{

("服务器启动失败, 原因:"+e);

bu_d = true;

bu_d = false;

return;

}

= "服务器启动成功,访问IP:"+ipAddress()+"

端口号:"+port;

//记录住连接的客户端

socketList = new Dictionary();

charList = new StringBuilder();

//使用多线程,用于多个客户端接入

Thread th = new Thread(new ThreadStart(executeTask));

();

}

public void executeTask()

{

while(tag)

{

//等待用户连接

TcpClient client = null;

try{

client = TcpClient();

}catch(Exception)

{

}

Thread th = new Thread(executeRead);

((Object)client);

}

}

public void executeRead(Object pamars)

{

//永久监听读取客户端

TcpClient client = pamars as TcpClient;

while(tag)

{

NetworkStream ns = eam();

StreamReader sr = new StreamReader(ns);

String msg = ;

String people = ;

try {

msg = ne();

if(f("")!=-1)

{

msg = (msg,"=")[1];

cb_(msg);

(msg).Append("<@>");

(msg,client);

msg = "

欢迎【"+msg+"】光临

";

}

Text(e("

","rn"));

sendMsg(,msg);

} catch (Exception) {

//(ng());

break;

}

}

}

public void sendMsg(String target,String msg)

{

if(!=target)

{

TcpClient client = socketList[target];

StreamWriter sw = new StreamWriter(eam());

ine(msg);

();

}else{

lection keyColl = ;

foreach (String name in keyColl)

{

StreamWriter sw = new

StreamWriter(socketList[name].GetStream());

ine(msg+"<@=@>"+ng());

();

}

}

}

/*根据计算名获取IP地址*/

public String ipAddress()

{

IPAddress[] address

tAddresses(tName());

return address[2].ToString();

}

void ServerFromFormClosing(object

FormClosingEventArgs e)

{

= false;

if(tag)

tag = false;

if(listener!=null)

();

}

void Bu_stopClick(object sender, EventArgs e)

{

bu_d = true;

bu_d = false;

if(tag)

tag = false;

if(listener!=null)

();

}

}

=

sender,

}

Client客户端

using System;

using g;

using ;

using ing;

using ;

using s;

using ;

using ;

using rExpressions;

namespace Client

{

public partial class MainForm : Form

{

private closeWindowTimer;

private StreamReader sr;

private StreamWriter sw;

private TcpClient tc;

private ClientLong cl;

private bool tag = true;

public MainForm(TcpClient tcp,ClientLong clo)

{

cl = clo;

tc = tcp;

InitializeComponent();

orIllegalCrossThreadCalls = false;

bu_();

}

void ClientFromLoad(object sender, EventArgs e)

{

PiayCheckedChanged();

}

/*事件方法*/

public void PiayCheckedChanged()

{

closeWindowTimer = new ();

al = 1000;

+= new EventHandler(theout);

();

}

/*执行的事件*/

public void theout(object source, EventArgs e)

{

//这里单独开一个线程用来显示信息

try{

Thread t1 = new Thread(new ThreadStart(readMsg));

();

}catch(Exception)

{

}

}

void readMsg()

{

if(tag && tc!=null){

sr = new StreamReader(eam());

String msg = ne();

String[] address = (msg,"<@=@>");

Text(address[0].Replace("

","rn"));

address = (address[1],"<@>");

cb_();

foreach (String s in address)

{

if(!OrEmpty(s) && s != Name)

cb_(s);

}

}

}

void Button1Click(object sender, EventArgs e)

{

if(OrEmpty()){

("请输入消息");return;

}

sw = new StreamWriter(eam());

ine("

"+Name+"

MM-dd HH:mm:ss")+"

"+);

= "";

();

}

void Bu_exitClick(object sender, EventArgs e)

{

MainFormFormClosing(null,null);

}

void Button2Click(object sender, EventArgs e)

{

= "";

}

"+ng("yyyy-

void

{

MainFormFormClosing(object sender,

FormClosingEventArgs e)

();

();

tag = false;

if(sr!=null)

();

if(sw!=null)

();

}

void Bu_simpleClick(object sender, EventArgs e)

{

String selected = cb_;

if(selected==null)

{

("请选择单聊对象");

return;

}

}

}

}

补充:局域网、校园网安全维护方法

校园网络分为内网和外网,就是说他们可以上学校的内网也可以同时上互联网,大学的学生平时要玩游戏购物,学校本身有自己的服务器需要维护;

在大环境下,首先在校园网之间及其互联网接入处,需要设置防火墙设备,防止外部攻击,并且要经常更新抵御外来攻击;

由于要保护校园网所有用户的安全,我们要安全加固,除了防火墙还要增加如ips,ids等防病毒入侵检测设备对外部数据进行分析检

测,确保校园网的安全;

外面做好防护措施,内部同样要做好防护措施,因为有的学生电脑可能带回家或者在外面感染,所以内部核心交换机上要设置vlan隔离,旁挂安全设备对端口进行检测防护;

内网可能有ddos攻击或者arp病毒等传播,所以我们要对服务器或者电脑安装杀毒软件,特别是学校服务器系统等,安全正版安全软件,保护重要电脑的安全;

对服务器本身我们要安全server版系统,经常修复漏洞及更新安全软件,普通电脑一般都是拨号上网,如果有异常上层设备监测一般不影响其他电脑。做好安全防范措施,未雨绸缪。

局域网聊天


本文标签: 设备 局域网 服务器 校园网 电脑