资源简介
这是一个模仿QQ的局域网即时通讯软件,采用C#编写,主要用的到技术有 多线程 Socket通讯 代码很完整,易懂,其中包含一个数据库文件和项目文件,建议.NET4.5以上版本使用
![](http://www.nz998.com/pic/42724.jpg)
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Threading;
namespace CharServer
{
class Program
{
//static Thread Th;
static Hashtable _ht = new Hashtable();
static void Main(string[] args)
{
IPAddress[] _ips = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress _ip=null;
byte[] _buff=new byte[64*1024];
foreach (IPAddress item in _ips)
{
if(item.AddressFamily.Equals(AddressFamily.InterNetwork))
{
_ip=item;
break;
}
}
SqlHelper.ExecuteNonQuery(“insert into _IPAddress values (@ipdefault)“ new SqlParameter[]
{
new SqlParameter(“@ip“_ip.ToString())
});
TcpListener _tl=new TcpListener(_ip8888);
_tl.Start();//侦听开始
Console.WriteLine(“程序开始监听...“);
Console.WriteLine(“服务器ip为“+_ip.ToString()+“端口为:8888“);
while (true)
{
_buff = new byte[90];
Socket sk = _tl.AcceptSocket();
sk.Receive(_buff);
string id = Encoding.Unicode.GetString(_buff).TrimEnd(‘\0‘);
if (!_ht.ContainsKey(id))
{
sk.Send(Encoding.Unicode.GetBytes(“success“));
_ht.Add(id sk);
Console.WriteLine(“QQ号为:“ + id + “的客户登录了,当前客户数量:“ +_ht.Count);
Thread th = new Thread(new ParameterizedThreadStart(ReciveMes));;
th.Start(id);
}
else
{
sk.Send(Encoding.Unicode.GetBytes(“fail“));
}
}
}
private static void ReciveMes(object obj)
{
string id = obj as string;
Socket sk = _ht[id] as Socket;
while (true)
{
try
{
byte[] opbyte = new byte[6];
byte[] buff = new byte[64 * 1024];
byte[] client = new byte[60];
sk.Receive(opbyte);
string op = Encoding.Unicode.GetString(opbyte).TrimEnd(‘\0‘);
//1.接受单聊
//2.退出
//3.更新
switch (op)
{
case “1“:
sk.Receive(client);
string clientid = Encoding.Unicode.GetString(client).TrimEnd(‘\0‘);
sk.Receive(buff);
Socket skClient = _ht[clientid] as Socket;
if (skClient != null)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 272 2013-05-31 12:32 CharServer\App.config
文件 9216 2013-05-28 00:44 CharServer\bin\Debug\CharServer.exe
文件 314 2013-05-27 10:48 CharServer\bin\Debug\CharServer.exe.config
文件 19968 2013-05-28 00:44 CharServer\bin\Debug\CharServer.pdb
文件 21464 2013-04-24 17:30 CharServer\bin\Debug\CharServer.vshost.exe
文件 272 2013-04-24 15:49 CharServer\bin\Debug\CharServer.vshost.exe.config
文件 490 2012-06-02 22:34 CharServer\bin\Debug\CharServer.vshost.exe.manifest
文件 2597 2013-05-31 12:32 CharServer\ChatServer.csproj
文件 3069 2013-04-25 23:38 CharServer\obj\Debug\CharServer.csproj.FileListAbsolute.txt
文件 9216 2013-05-28 00:44 CharServer\obj\Debug\CharServer.exe
文件 19968 2013-05-28 00:44 CharServer\obj\Debug\CharServer.pdb
文件 568 2013-05-28 00:44 CharServer\obj\Debug\ChatServer.csproj.FileListAbsolute.txt
文件 5800 2013-05-28 00:44 CharServer\obj\Debug\ChatServer.csprojResolveAssemblyReference.cache
文件 6181 2013-05-31 12:32 CharServer\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 0 2013-04-19 09:30 CharServer\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2013-04-19 09:30 CharServer\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2013-04-19 09:30 CharServer\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
文件 5942 2013-05-16 02:31 CharServer\Program.cs
文件 1340 2013-04-19 09:30 CharServer\Properties\AssemblyInfo.cs
文件 2858 2013-05-16 02:35 CharServer\SqlHelper.cs
文件 389 2013-05-31 12:32 ChartClient\App.config
文件 0 2013-04-23 21:04 ChartClient\bin\Debug\App.exe.manifest
文件 635904 2013-05-31 12:33 ChartClient\bin\Debug\ChartClient.exe
文件 389 2013-05-31 12:32 ChartClient\bin\Debug\ChartClient.exe.config
文件 95744 2013-05-31 12:33 ChartClient\bin\Debug\ChartClient.pdb
文件 21464 2013-06-07 22:27 ChartClient\bin\Debug\ChartClient.vshost.exe
文件 389 2013-05-31 12:32 ChartClient\bin\Debug\ChartClient.vshost.exe.config
文件 490 2012-06-02 22:34 ChartClient\bin\Debug\ChartClient.vshost.exe.manifest
文件 52224 2013-05-31 12:33 ChartClient\bin\Debug\Computer.exe
文件 58880 2013-05-31 12:33 ChartClient\bin\Debug\Computer.pdb
............此处省略203个文件信息
评论
共有 条评论