内网聊天工具FreeChat Beta

今天自己做了一个小软件,取名叫FreeChat
主要的用途是局域网内聊天,主要用到.net  的Socket类.Thread并自己封装了数据包.

下载地址:/Files/BearOcean/FreeChat(Beta).rar

现在作为Beta版发布...也把源代码发出来.我是菜鸟.希望高手不要鄙视.因为是基于.net的,所以至少需要FrameWork1.1

使用说明如下图:

FreeChat1.0.gif

很简单...在1.0中想添加一些其他的功能.比如允许文件传送,而且觉得输入IP或者计算机名的方式有点麻烦.

原代码如下:

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
using  System.Net;
None.gif
using  System.Net.Sockets;
None.gif
using  System.Text;
None.gif
using  System.IO;
None.gif
using  System.Threading;
None.gif
None.gif
namespace  FreeChat
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
InBlock.gif    
InBlock.gif    
public class FmFreeChat : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
const int MSGINFOLENGTH=10;
InBlock.gif        
const int MSGREMOTENAME=20;
InBlock.gif        
const int MAXPACKSIZE=100;
InBlock.gif
InBlock.gif        
private System.Windows.Forms.Button BtSend;
InBlock.gif        
private System.Windows.Forms.Button BtExit;
InBlock.gif        
private System.Windows.Forms.RichTextBox RtxSend;
InBlock.gif        
private System.Windows.Forms.RichTextBox RtxInfo;
InBlock.gif        
private System.Windows.Forms.Label LbComName;
InBlock.gif        
private System.Windows.Forms.TextBox TxtTarget;
InBlock.gif
InBlock.gif        
private IPHostEntry hostentryHost;
InBlock.gif        
private IPEndPoint endpointHost;
InBlock.gif        
private Socket sockListener;
InBlock.gif        
private Thread thWorker;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItem1;
InBlock.gif        
private System.Windows.Forms.MainMenu MeunFreeChat;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemSave;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemSaveClear;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemClear;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemHelp;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemAbFreeChat;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemAbMe;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItem6;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItem7;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItem8;
InBlock.gif        
InBlock.gif        
private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public FmFreeChat()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();        
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Dispose#region Dispose
InBlock.gif        
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(sockListener!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sockListener.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows#region Windows 
InBlock.gif        
private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Resources.ResourceManager resources 
= new System.Resources.ResourceManager(typeof(FmFreeChat));
InBlock.gif            
this.RtxInfo = new System.Windows.Forms.RichTextBox();
InBlock.gif            
this.TxtTarget = new System.Windows.Forms.TextBox();
InBlock.gif            
this.LbComName = new System.Windows.Forms.Label();
InBlock.gif            
this.RtxSend = new System.Windows.Forms.RichTextBox();
InBlock.gif            
this.BtSend = new System.Windows.Forms.Button();
InBlock.gif            
this.BtExit = new System.Windows.Forms.Button();
InBlock.gif            
this.MeunFreeChat = new System.Windows.Forms.MainMenu();
InBlock.gif            
this.menuItem1 = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemSave = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemSaveClear = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemClear = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItem6 = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItem7 = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItem8 = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemHelp = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemAbFreeChat = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemAbMe = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// RtxInfo
InBlock.gif            
// 
InBlock.gif
            this.RtxInfo.AutoWordSelection = true;
InBlock.gif            
this.RtxInfo.BackColor = System.Drawing.SystemColors.Info;
InBlock.gif            
this.RtxInfo.Location = new System.Drawing.Point(1040);
InBlock.gif            
this.RtxInfo.Name = "RtxInfo";
InBlock.gif            
this.RtxInfo.ReadOnly = true;
InBlock.gif            
this.RtxInfo.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
InBlock.gif            
this.RtxInfo.Size = new System.Drawing.Size(320112);
InBlock.gif            
this.RtxInfo.TabIndex = 0;
InBlock.gif            
this.RtxInfo.Text = "";
InBlock.gif            
this.RtxInfo.TextChanged += new System.EventHandler(this.RtxInfo_TextChanged);
InBlock.gif            
// 
InBlock.gif            
// TxtTarget
InBlock.gif            
// 
InBlock.gif
            this.TxtTarget.Location = new System.Drawing.Point(589);
InBlock.gif            
this.TxtTarget.Name = "TxtTarget";
InBlock.gif            
this.TxtTarget.Size = new System.Drawing.Size(13421);
InBlock.gif            
this.TxtTarget.TabIndex = 1;
InBlock.gif            
this.TxtTarget.Text = "";
InBlock.gif            
// 
InBlock.gif            
// LbComName
InBlock.gif            
// 
InBlock.gif
            this.LbComName.Location = new System.Drawing.Point(09);
InBlock.gif            
this.LbComName.Name = "LbComName";
InBlock.gif            
this.LbComName.Size = new System.Drawing.Size(5819);
InBlock.gif            
this.LbComName.TabIndex = 2;
InBlock.gif            
this.LbComName.Text = "Target:";
InBlock.gif            
this.LbComName.TextAlign = System.Drawing.ContentAlignment.BottomRight;
InBlock.gif            
// 
InBlock.gif            
// RtxSend
InBlock.gif            
// 
InBlock.gif
            this.RtxSend.BackColor = System.Drawing.SystemColors.ControlLightLight;
InBlock.gif            
this.RtxSend.Location = new System.Drawing.Point(10160);
InBlock.gif            
this.RtxSend.Name = "RtxSend";
InBlock.gif            
this.RtxSend.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
InBlock.gif            
this.RtxSend.Size = new System.Drawing.Size(32088);
InBlock.gif            
this.RtxSend.TabIndex = 3;
InBlock.gif            
this.RtxSend.Text = "";
InBlock.gif            
// 
InBlock.gif            
// BtSend
InBlock.gif            
// 
InBlock.gif
            this.BtSend.Location = new System.Drawing.Point(10256);
InBlock.gif            
this.BtSend.Name = "BtSend";
InBlock.gif            
this.BtSend.Size = new System.Drawing.Size(9027);
InBlock.gif            
this.BtSend.TabIndex = 4;
InBlock.gif            
this.BtSend.Text = "Send";
InBlock.gif            
this.BtSend.Click += new System.EventHandler(this.BtSend_Click);
InBlock.gif            
// 
InBlock.gif            
// BtExit
InBlock.gif            
// 
InBlock.gif
            this.BtExit.Location = new System.Drawing.Point(134256);
InBlock.gif            
this.BtExit.Name = "BtExit";
InBlock.gif            
this.BtExit.Size = new System.Drawing.Size(9027);
InBlock.gif            
this.BtExit.TabIndex = 5;
InBlock.gif            
this.BtExit.Text = "Exit";
InBlock.gif            
this.BtExit.Click += new System.EventHandler(this.BtExit_Click);
InBlock.gif            
// 
InBlock.gif            
// MeunFreeChat
InBlock.gif            
// 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            this.MeunFreeChat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                         
this.menuItem1,
ExpandedSubBlockEnd.gif                                                                                         
this.menuItemHelp}
);
InBlock.gif            
// 
InBlock.gif            
// menuItem1
InBlock.gif            
// 
InBlock.gif
            this.menuItem1.Index = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                      
this.menuItemSave,
InBlock.gif                                                                                      
this.menuItemSaveClear,
InBlock.gif                                                                                      
this.menuItemClear,
ExpandedSubBlockEnd.gif                                                                                      
this.menuItem6}
);
InBlock.gif            
this.menuItem1.Text = "Record";
InBlock.gif            
// 
InBlock.gif            
// menuItemSave
InBlock.gif            
// 
InBlock.gif
            this.menuItemSave.Index = 0;
InBlock.gif            
this.menuItemSave.Text = "Save";
InBlock.gif            
this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemSaveClear
InBlock.gif            
// 
InBlock.gif
            this.menuItemSaveClear.Index = 1;
InBlock.gif            
this.menuItemSaveClear.Text = "SaveClear";
InBlock.gif            
this.menuItemSaveClear.Click += new System.EventHandler(this.menuItemSaveClear_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemClear
InBlock.gif            
// 
InBlock.gif
            this.menuItemClear.Index = 2;
InBlock.gif            
this.menuItemClear.Text = "Clear";
InBlock.gif            
this.menuItemClear.Click += new System.EventHandler(this.menuItemClear_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItem6
InBlock.gif            
// 
InBlock.gif
            this.menuItem6.Index = 3;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                      
this.menuItem7,
ExpandedSubBlockEnd.gif                                                                                      
this.menuItem8}
);
InBlock.gif            
this.menuItem6.Text = "AutoSave";
InBlock.gif            
// 
InBlock.gif            
// menuItem7
InBlock.gif            
// 
InBlock.gif
            this.menuItem7.Index = 0;
InBlock.gif            
this.menuItem7.Text = "ON";
InBlock.gif            
// 
InBlock.gif            
// menuItem8
InBlock.gif            
// 
InBlock.gif
            this.menuItem8.Checked = true;
InBlock.gif            
this.menuItem8.Index = 1;
InBlock.gif            
this.menuItem8.Text = "OFF";
InBlock.gif            
// 
InBlock.gif            
// menuItemHelp
InBlock.gif            
// 
InBlock.gif
            this.menuItemHelp.Index = 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                         
this.menuItemAbFreeChat,
ExpandedSubBlockEnd.gif                                                                                         
this.menuItemAbMe}
);
InBlock.gif            
this.menuItemHelp.Text = "Help";
InBlock.gif            
// 
InBlock.gif            
// menuItemAbFreeChat
InBlock.gif            
// 
InBlock.gif
            this.menuItemAbFreeChat.Index = 0;
InBlock.gif            
this.menuItemAbFreeChat.Text = "About FreeChat";
InBlock.gif            
this.menuItemAbFreeChat.Click += new System.EventHandler(this.menuItemAbFreeChat_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemAbMe
InBlock.gif            
// 
InBlock.gif
            this.menuItemAbMe.Index = 1;
InBlock.gif            
this.menuItemAbMe.Text = "About Me";
InBlock.gif            
this.menuItemAbMe.Click += new System.EventHandler(this.menuItemAbMe_Click);
InBlock.gif            
// 
InBlock.gif            
// FmFreeChat
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(340295);
InBlock.gif            
this.Controls.Add(this.BtExit);
InBlock.gif            
this.Controls.Add(this.BtSend);
InBlock.gif            
this.Controls.Add(this.RtxSend);
InBlock.gif            
this.Controls.Add(this.LbComName);
InBlock.gif            
this.Controls.Add(this.TxtTarget);
InBlock.gif            
this.Controls.Add(this.RtxInfo);
InBlock.gif            
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
InBlock.gif            
this.MaximumSize = new System.Drawing.Size(348350);
InBlock.gif            
this.Menu = this.MeunFreeChat;
InBlock.gif            
this.MinimumSize = new System.Drawing.Size(348350);
InBlock.gif            
this.Name = "FmFreeChat";
InBlock.gif            
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
InBlock.gif            
this.Text = "FreeChat";
InBlock.gif            
this.Load += new System.EventHandler(this.FmFreeChat_Load);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
InBlock.gif        [STAThread]
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Main#region Main
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new FmFreeChat());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
FreeChatInit#region FreeChatInit
InBlock.gif        
private void FreeChatInit()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            hostentryHost
=Dns.GetHostByName(Dns.GetHostName());
InBlock.gif            endpointHost
=new IPEndPoint(hostentryHost.AddressList[0],9898);
InBlock.gif
InBlock.gif            sockListener
=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
InBlock.gif            sockListener.Bind(endpointHost);
InBlock.gif            sockListener.Listen(
100);
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
WorkerProc#region WorkerProc
InBlock.gif        
private void WorkerProc()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Socket sockAcceptBuffer
=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
InBlock.gif            
int iReadSize;
InBlock.gif            
int iLoopCount=0;
InBlock.gif            Byte[] bytMsgBuffer
=new Byte[MAXPACKSIZE];
InBlock.gif            
int iMsgRealSize=0;
InBlock.gif            
InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MemoryStream msMsg
=new MemoryStream();
InBlock.gif                iLoopCount
=0;
InBlock.gif                sockAcceptBuffer
=sockListener.Accept();
InBlock.gif                
string strRemoteName="";
InBlock.gif
InBlock.gif                    
do
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        iReadSize
=sockAcceptBuffer.Receive(bytMsgBuffer);
InBlock.gif                        
if(iLoopCount==0)//First Loop,Get Size Information
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            Byte[] bytMsgSize
=new Byte[MSGINFOLENGTH];
InBlock.gif                            Byte[] bytRemoteName
=new Byte[MSGREMOTENAME];
InBlock.gif
InBlock.gif                            Array.Copy(bytMsgBuffer,
0,bytMsgSize,0,MSGINFOLENGTH);
InBlock.gif                            Array.Copy(bytMsgBuffer,MSGINFOLENGTH,bytRemoteName,
0,MSGREMOTENAME);
InBlock.gif
InBlock.gif                            
string strMsgSize=System.Text.Encoding.UTF8.GetString(bytMsgSize);
InBlock.gif                            strRemoteName
=System.Text.Encoding.UTF8.GetString(bytRemoteName);
InBlock.gif
InBlock.gif                            iMsgRealSize
=Convert.ToInt32(strMsgSize);
InBlock.gif                            
InBlock.gif                    
InBlock.gif                            
if((iMsgRealSize+MSGINFOLENGTH)<=MAXPACKSIZE)//First Loop Can Get All Data
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                
//MessageBox.Show(this,"1");
InBlock.gif
                                Byte[] bytMsg1=new Byte[iMsgRealSize];
InBlock.gif                                Array.Copy(bytMsgBuffer,MSGINFOLENGTH
+MSGREMOTENAME,bytMsg1,0,iMsgRealSize);
InBlock.gif                                msMsg.Write(bytMsg1,
0,iMsgRealSize);
InBlock.gif                                
break;
InBlock.gif                                
ExpandedSubBlockEnd.gif                            }

InBlock.gif
InBlock.gif                            
else//First Loop Cant Get All Data
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                
//MessageBox.Show(this,"2");
InBlock.gif
                                Byte[] bytMsg2=new Byte[MAXPACKSIZE-MSGINFOLENGTH];
InBlock.gif                                Array.Copy(bytMsgBuffer,MSGINFOLENGTH
+MSGREMOTENAME,bytMsg2,0,MAXPACKSIZE-MSGINFOLENGTH-MSGREMOTENAME);
InBlock.gif                                msMsg.Write(bytMsg2,
0,MAXPACKSIZE-MSGINFOLENGTH-MSGREMOTENAME);
InBlock.gif                                Byte[] test
=msMsg.ToArray();
InBlock.gif                                
string strtest=System.Text.Encoding.UTF8.GetString(test);
InBlock.gif                                MessageBox.Show(
this,strtest);
InBlock.gif                                iLoopCount
++;
InBlock.gif                                
continue;
ExpandedSubBlockEnd.gif                            }
                
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        msMsg.Write(bytMsgBuffer,
0,iReadSize);
InBlock.gif                        iLoopCount
++;
ExpandedSubBlockEnd.gif                    }
while(msMsg.Length<iMsgRealSize);
InBlock.gif
InBlock.gif                    
//                if(msMsg.Length!=iMsgRealSize)
InBlock.gif                    
//                {
InBlock.gif                    
//                    MessageBox.Show(this,"Tansfer Error");
InBlock.gif                    
//                    break;
InBlock.gif                    
//                }
InBlock.gif
                    Byte[] bytRealMsg=msMsg.ToArray();
InBlock.gif                    
string strMsg=System.Text.Encoding.UTF8.GetString(bytRealMsg);
InBlock.gif                    
InBlock.gif
InBlock.gif                    
InBlock.gif                    RtxInfo.AppendText(strRemoteName);
InBlock.gif                    RtxInfo.AppendText(
":");
InBlock.gif                    RtxInfo.AppendText(
"\n");
InBlock.gif                    RtxInfo.AppendText(strMsg);
InBlock.gif                    RtxInfo.AppendText(
"\n");
InBlock.gif
InBlock.gif                    RtxInfo.Focus();
InBlock.gif                    RtxInfo.ScrollToCaret();
InBlock.gif    
InBlock.gif                    
continue;
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
FmFreeChat_Load#region FmFreeChat_Load
InBlock.gif        
private void FmFreeChat_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Listen and bind
InBlock.gif
            FreeChatInit();
InBlock.gif            thWorker
=new Thread(new ThreadStart(WorkerProc));
InBlock.gif            thWorker.Start();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
BtExit_Click#region BtExit_Click
InBlock.gif        
private void BtExit_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Dispose(true);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
BtSend_Click#region BtSend_Click
InBlock.gif        
private void BtSend_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strMyName=Dns.GetHostName();
InBlock.gif            IPHostEntry hostentryTarget;
InBlock.gif
InBlock.gif            
if(TxtTarget.Text=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
this,"Please Input Computer name");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(RtxSend.Text=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
this,"Please Input Your Message");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
string strTarget=TxtTarget.Text;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hostentryTarget
=Dns.Resolve(strTarget);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
this,String.Concat("No Such Computer Exist.Message:",ex.Message));
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IPEndPoint endpointTarget
=new IPEndPoint(hostentryTarget.AddressList[0],9898);
InBlock.gif            
InBlock.gif            Socket sockTarget
=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
InBlock.gif            sockTarget.Connect(endpointTarget);
InBlock.gif
InBlock.gif            
string strMessage=RtxSend.Text;
InBlock.gif            
InBlock.gif            Byte[] bytRealMessage
=System.Text.Encoding.UTF8.GetBytes(strMessage);
InBlock.gif
InBlock.gif            
int iRealMsgSize=bytRealMessage.Length;
InBlock.gif            
string strRealMsgSize=Convert.ToString(iRealMsgSize);
InBlock.gif            Byte[] bytMsgInfoBuffer
=System.Text.Encoding.UTF8.GetBytes(strRealMsgSize);
InBlock.gif            Byte[] bytMsgInfo
=new Byte[MSGINFOLENGTH];
InBlock.gif            Array.Copy(bytMsgInfoBuffer,
0,bytMsgInfo,0,bytMsgInfoBuffer.Length);
InBlock.gif
InBlock.gif            Byte[] bytRemoteNameBuffer
=System.Text.Encoding.UTF8.GetBytes(strMyName);
InBlock.gif            Byte[] bytRemoteName
=new Byte[MSGREMOTENAME];
InBlock.gif            Array.Copy(bytRemoteNameBuffer,
0,bytRemoteName,0,bytRemoteNameBuffer.Length);
InBlock.gif
InBlock.gif            
InBlock.gif
InBlock.gif            Byte[] bytTotalMsg
=new Byte[MSGINFOLENGTH+MSGREMOTENAME+iRealMsgSize];
InBlock.gif            Array.Copy(bytMsgInfo,
0,bytTotalMsg,0,MSGINFOLENGTH);
InBlock.gif            Array.Copy(bytRemoteName,
0,bytTotalMsg,MSGINFOLENGTH,MSGREMOTENAME);
InBlock.gif            Array.Copy(bytRealMessage,
0,bytTotalMsg,MSGINFOLENGTH+MSGREMOTENAME,iRealMsgSize);
InBlock.gif            sockTarget.Send(bytTotalMsg);
InBlock.gif
InBlock.gif
//            System.Collections.IEnumerator myEnumerator = bytTotalMsg.GetEnumerator();
InBlock.gif
//            while ( myEnumerator.MoveNext() )  
InBlock.gif
//            {    
InBlock.gif
//                MessageBox.Show(this,myEnumerator.Current.ToString());
InBlock.gif
//            }
InBlock.gif
            
InBlock.gif
InBlock.gif            
InBlock.gif            
string strText=String.Concat(strMyName,":","\n",strMessage,"\n");
InBlock.gif            RtxInfo.AppendText(strText);
InBlock.gif            RtxInfo.Focus();
InBlock.gif            RtxInfo.ScrollToCaret();
InBlock.gif
InBlock.gif            sockTarget.Shutdown(SocketShutdown.Both);
InBlock.gif            sockTarget.Close();
InBlock.gif            RtxSend.Clear();
InBlock.gif
InBlock.gif               
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
RtxInfo_TextChanged#region RtxInfo_TextChanged
InBlock.gif        
private void RtxInfo_TextChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
menuItemClear#region menuItemClear
InBlock.gif        
private void menuItemClear_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RtxInfo.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
RecordSave#region RecordSave
InBlock.gif        
private void RecordSave()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(RtxInfo.Text=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
this,"No Message");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strRecord=RtxInfo.Text;
InBlock.gif                Record.RecordMessage(strRecord);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
menuItemSave#region menuItemSave
InBlock.gif        
private void menuItemSave_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RecordSave();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
menuItemSaveClear#region menuItemSaveClear
InBlock.gif        
private void menuItemSaveClear_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RecordSave();
InBlock.gif            RtxInfo.Clear();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
menuItemAbFreeChat#region menuItemAbFreeChat
InBlock.gif        
private void menuItemAbFreeChat_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageBox.Show(
"FreeChat Version:Beta\n-------------------------------------------------------\nThis SoftWare Is Completely For Free\nAny One Who Use It Can Change the Code of FreeChat\n-------------------------------------------------------\nThe CopyRight Belong To BearOcean","About FreeChat");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
menuItemAbMe#region menuItemAbMe
InBlock.gif        
private void menuItemAbMe_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageBox.Show(
"BearOcean: I am the author of FreeChat.\nJust a student who is very interested in Software Tec\nEmail:bearocean@163.com\nBlog:http://bearocean.cnblogs.com","About Me");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

然后是负责储存聊天记录的部分:

 

None.gif using  System;
None.gif
using  System.Windows.Forms;
None.gif
using  System.IO;
None.gif
None.gif
namespace  FreeChat
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Record 
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Record
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Record()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void RecordMessage(string strMessage)//CreateDirectory And File
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Directory.CreateDirectory(
"FreeChatLogs");
InBlock.gif            DateTime dt
=DateTime.Now;
InBlock.gif            
string today=dt.ToString("yyyy-MM-dd");
InBlock.gif            
string strRecordTime=dt.ToString("yyyy-MM-dd:HH:mm:ff");
InBlock.gif
InBlock.gif            StreamWriter swRecordFile
=new StreamWriter(String.Concat("FreeChatLogs\\",today,".chat"),true,System.Text.Encoding.UTF8);
InBlock.gif            swRecordFile.WriteLine(String.Concat(
"RecordTime:",strRecordTime));
InBlock.gif            swRecordFile.WriteLine(
"------------------------------------------------------------------");
InBlock.gif            swRecordFile.Write(strMessage);
InBlock.gif            swRecordFile.Write(
"\n\n\n\n\n");
InBlock.gif            swRecordFile.Close();
InBlock.gif
InBlock.gif            MessageBox.Show(
"Record Success","FreeChat");
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


代码基本上就是这些.1.0肯定会对它进行补全和修改,还有功能添加.

 

转载于:https://www.cnblogs.com/BearOcean/archive/2006/01/18/319310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值