an
今天自己做了一个小软件,取名叫FreeChat
主要的用途是局域网内聊天,主要用到.net 的Socket类.Thread并自己封装了数据包.
下载地址:/Files/BearOcean/FreeChat(Beta).rar
现在作为Beta版发布...也把源代码发出来.我是菜鸟.希望高手不要鄙视.因为是基于.net的,所以至少需要FrameWork1.1
使用说明如下图:
很简单...在1.0中想添加一些其他的功能.比如允许文件传送,而且觉得输入IP或者计算机名的方式有点麻烦.
原代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
namespace FreeChat
{
public class FmFreeChat : System.Windows.Forms.Form
{
const int MSGINFOLENGTH=10;
const int MSGREMOTENAME=20;
const int MAXPACKSIZE=100;
private System.Windows.Forms.Button BtSend;
private System.Windows.Forms.Button BtExit;
private System.Windows.Forms.RichTextBox RtxSend;
private System.Windows.Forms.RichTextBox RtxInfo;
private System.Windows.Forms.Label LbComName;
private System.Windows.Forms.TextBox TxtTarget;
private IPHostEntry hostentryHost;
private IPEndPoint endpointHost;
private Socket sockListener;
private Thread thWorker;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MainMenu MeunFreeChat;
private System.Windows.Forms.MenuItem menuItemSave;
private System.Windows.Forms.MenuItem menuItemSaveClear;
private System.Windows.Forms.MenuItem menuItemClear;
private System.Windows.Forms.MenuItem menuItemHelp;
private System.Windows.Forms.MenuItem menuItemAbFreeChat;
private System.Windows.Forms.MenuItem menuItemAbMe;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.ComponentModel.Container components = null;
public FmFreeChat()
{
InitializeComponent();
}
Dispose#region Dispose
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
if(sockListener!=null)
{
sockListener.Close();
}
}
base.Dispose( disposing );
}
#endregion
Windows#region Windows
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FmFreeChat));
this.RtxInfo = new System.Windows.Forms.RichTextBox();
this.TxtTarget = new System.Windows.Forms.TextBox();
this.LbComName = new System.Windows.Forms.Label();
this.RtxSend = new System.Windows.Forms.RichTextBox();
this.BtSend = new System.Windows.Forms.Button();
this.BtExit = new System.Windows.Forms.Button();
this.MeunFreeChat = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemSave = new System.Windows.Forms.MenuItem();
this.menuItemSaveClear = new System.Windows.Forms.MenuItem();
this.menuItemClear = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItemHelp = new System.Windows.Forms.MenuItem();
this.menuItemAbFreeChat = new System.Windows.Forms.MenuItem();
this.menuItemAbMe = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// RtxInfo
//
this.RtxInfo.AutoWordSelection = true;
this.RtxInfo.BackColor = System.Drawing.SystemColors.Info;
this.RtxInfo.Location = new System.Drawing.Point(10, 40);
this.RtxInfo.Name = "RtxInfo";
this.RtxInfo.ReadOnly = true;
this.RtxInfo.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
this.RtxInfo.Size = new System.Drawing.Size(320, 112);
this.RtxInfo.TabIndex = 0;
this.RtxInfo.Text = "";
this.RtxInfo.TextChanged += new System.EventHandler(this.RtxInfo_TextChanged);
//
// TxtTarget
//
this.TxtTarget.Location = new System.Drawing.Point(58, 9);
this.TxtTarget.Name = "TxtTarget";
this.TxtTarget.Size = new System.Drawing.Size(134, 21);
this.TxtTarget.TabIndex = 1;
this.TxtTarget.Text = "";
//
// LbComName
//
this.LbComName.Location = new System.Drawing.Point(0, 9);
this.LbComName.Name = "LbComName";
this.LbComName.Size = new System.Drawing.Size(58, 19);
this.LbComName.TabIndex = 2;
this.LbComName.Text = "Target:";
this.LbComName.TextAlign = System.Drawing.ContentAlignment.BottomRight;
//
// RtxSend
//
this.RtxSend.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.RtxSend.Location = new System.Drawing.Point(10, 160);
this.RtxSend.Name = "RtxSend";
this.RtxSend.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
this.RtxSend.Size = new System.Drawing.Size(320, 88);
this.RtxSend.TabIndex = 3;
this.RtxSend.Text = "";
//
// BtSend
//
this.BtSend.Location = new System.Drawing.Point(10, 256);
this.BtSend.Name = "BtSend";
this.BtSend.Size = new System.Drawing.Size(90, 27);
this.BtSend.TabIndex = 4;
this.BtSend.Text = "Send";
this.BtSend.Click += new System.EventHandler(this.BtSend_Click);
//
// BtExit
//
this.BtExit.Location = new System.Drawing.Point(134, 256);
this.BtExit.Name = "BtExit";
this.BtExit.Size = new System.Drawing.Size(90, 27);
this.BtExit.TabIndex = 5;
this.BtExit.Text = "Exit";
this.BtExit.Click += new System.EventHandler(this.BtExit_Click);
//
// MeunFreeChat
//
this.MeunFreeChat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItemHelp});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemSave,
this.menuItemSaveClear,
this.menuItemClear,
this.menuItem6});
this.menuItem1.Text = "Record";
//
// menuItemSave
//
this.menuItemSave.Index = 0;
this.menuItemSave.Text = "Save";
this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
//
// menuItemSaveClear
//
this.menuItemSaveClear.Index = 1;
this.menuItemSaveClear.Text = "SaveClear";
this.menuItemSaveClear.Click += new System.EventHandler(this.menuItemSaveClear_Click);
//
// menuItemClear
//
this.menuItemClear.Index = 2;
this.menuItemClear.Text = "Clear";
this.menuItemClear.Click += new System.EventHandler(this.menuItemClear_Click);
//
// menuItem6
//
this.menuItem6.Index = 3;
this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem7,
this.menuItem8});
this.menuItem6.Text = "AutoSave";
//
// menuItem7
//
this.menuItem7.Index = 0;
this.menuItem7.Text = "ON";
//
// menuItem8
//
this.menuItem8.Checked = true;
this.menuItem8.Index = 1;
this.menuItem8.Text = "OFF";
//
// menuItemHelp
//
this.menuItemHelp.Index = 1;
this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemAbFreeChat,
this.menuItemAbMe});
this.menuItemHelp.Text = "Help";
//
// menuItemAbFreeChat
//
this.menuItemAbFreeChat.Index = 0;
this.menuItemAbFreeChat.Text = "About FreeChat";
this.menuItemAbFreeChat.Click += new System.EventHandler(this.menuItemAbFreeChat_Click);
//
// menuItemAbMe
//
this.menuItemAbMe.Index = 1;
this.menuItemAbMe.Text = "About Me";
this.menuItemAbMe.Click += new System.EventHandler(this.menuItemAbMe_Click);
//
// FmFreeChat
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(340, 295);
this.Controls.Add(this.BtExit);
this.Controls.Add(this.BtSend);
this.Controls.Add(this.RtxSend);
this.Controls.Add(this.LbComName);
this.Controls.Add(this.TxtTarget);
this.Controls.Add(this.RtxInfo);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximumSize = new System.Drawing.Size(348, 350);
this.Menu = this.MeunFreeChat;
this.MinimumSize = new System.Drawing.Size(348, 350);
this.Name = "FmFreeChat";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FreeChat";
this.Load += new System.EventHandler(this.FmFreeChat_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
Main#region Main
static void Main()
{
Application.Run(new FmFreeChat());
}
#endregion
FreeChatInit#region FreeChatInit
private void FreeChatInit()
{
hostentryHost=Dns.GetHostByName(Dns.GetHostName());
endpointHost=new IPEndPoint(hostentryHost.AddressList[0],9898);
sockListener=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sockListener.Bind(endpointHost);
sockListener.Listen(100);
return;
}
#endregion
WorkerProc#region WorkerProc
private void WorkerProc()
{
Socket sockAcceptBuffer=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
int iReadSize;
int iLoopCount=0;
Byte[] bytMsgBuffer=new Byte[MAXPACKSIZE];
int iMsgRealSize=0;
while(true)
{
MemoryStream msMsg=new MemoryStream();
iLoopCount=0;
sockAcceptBuffer=sockListener.Accept();
string strRemoteName="";
do
{
iReadSize=sockAcceptBuffer.Receive(bytMsgBuffer);
if(iLoopCount==0)//First Loop,Get Size Information
{
Byte[] bytMsgSize=new Byte[MSGINFOLENGTH];
Byte[] bytRemoteName=new Byte[MSGREMOTENAME];
Array.Copy(bytMsgBuffer,0,bytMsgSize,0,MSGINFOLENGTH);
Array.Copy(bytMsgBuffer,MSGINFOLENGTH,bytRemoteName,0,MSGREMOTENAME);
string strMsgSize=System.Text.Encoding.UTF8.GetString(bytMsgSize);
strRemoteName=System.Text.Encoding.UTF8.GetString(bytRemoteName);
iMsgRealSize=Convert.ToInt32(strMsgSize);
if((iMsgRealSize+MSGINFOLENGTH)<=MAXPACKSIZE)//First Loop Can Get All Data
{
//MessageBox.Show(this,"1");
Byte[] bytMsg1=new Byte[iMsgRealSize];
Array.Copy(bytMsgBuffer,MSGINFOLENGTH+MSGREMOTENAME,bytMsg1,0,iMsgRealSize);
msMsg.Write(bytMsg1,0,iMsgRealSize);
break;
}
else//First Loop Cant Get All Data
{
//MessageBox.Show(this,"2");
Byte[] bytMsg2=new Byte[MAXPACKSIZE-MSGINFOLENGTH];
Array.Copy(bytMsgBuffer,MSGINFOLENGTH+MSGREMOTENAME,bytMsg2,0,MAXPACKSIZE-MSGINFOLENGTH-MSGREMOTENAME);
msMsg.Write(bytMsg2,0,MAXPACKSIZE-MSGINFOLENGTH-MSGREMOTENAME);
Byte[] test=msMsg.ToArray();
string strtest=System.Text.Encoding.UTF8.GetString(test);
MessageBox.Show(this,strtest);
iLoopCount++;
continue;
}
}
msMsg.Write(bytMsgBuffer,0,iReadSize);
iLoopCount++;
}while(msMsg.Length
然后是负责储存聊天记录的部分:
using System;
using System.Windows.Forms;
using System.IO;
namespace FreeChat
{
/**
/// Record
///
public class Record
{
public Record()
{
}
public static void RecordMessage(string strMessage)//CreateDirectory And File
{
Directory.CreateDirectory("FreeChatLogs");
DateTime dt=DateTime.Now;
string today=dt.ToString("yyyy-MM-dd");
string strRecordTime=dt.ToString("yyyy-MM-dd:HH:mm:ff");
StreamWriter swRecordFile=new StreamWriter(String.Concat("FreeChatLogs//",today,".chat"),true,System.Text.Encoding.UTF8);
swRecordFile.WriteLine(String.Concat("RecordTime:",strRecordTime));
swRecordFile.WriteLine("------------------------------------------------------------------");
swRecordFile.Write(strMessage);
swRecordFile.Write("/n/n/n/n/n");
swRecordFile.Close();
MessageBox.Show("Record Success","FreeChat");
}
}
}
代码基本上就是这些.1.0肯定会对它进行补全和修改,还有功能添加.