国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

局域網QQ(C#版)

2024-07-21 02:26:27
字體:
來源:轉載
供稿:網友

本通訊程序沒有服務端和客戶端之分,局域網的計算機運行同一程序即可通信。
由于水平有限,目前版本還很菜,只可以實現基本的聊天功能和顯示在線用戶功能。
準備添加傳送文件、聊天日志、可選參數等功能。
  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.threading;

namespace myqq
{
 /// <summary>
 /// form1 的摘要說明。
 /// </summary>
 public class form1 : system.windows.forms.form
 {
  public bool runing=false; //標志
  public udpclient listen=new udpclient(2525);
  public ipendpoint end;
  public ipaddress groupaddress=ipaddress.parse("255.255.255.255"); //廣播地址
  public int groupport=2525;  //廣播端口
  public string machinename;
  public string machineip;
  public string romename;
  public string romeip;
  public string romecon;
 
  private system.windows.forms.listbox box;
  private system.windows.forms.label label1;
  private system.windows.forms.label label2;
  private system.windows.forms.button button1;
  private system.windows.forms.button button3;
  private system.windows.forms.label label3;
  private system.windows.forms.textbox t_rec;
  private system.windows.forms.textbox t_send;
  private system.windows.forms.button button2;
  private system.windows.forms.button newmsg;
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  private system.componentmodel.container components = null;

  public form1()
  {
   //
   // windows 窗體設計器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 調用后添加任何構造函數代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows 窗體設計器生成的代碼
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void initializecomponent()
  {
   this.box = new system.windows.forms.listbox();
   this.t_rec = new system.windows.forms.textbox();
   this.label1 = new system.windows.forms.label();
   this.label2 = new system.windows.forms.label();
   this.t_send = new system.windows.forms.textbox();
   this.button1 = new system.windows.forms.button();
   this.button3 = new system.windows.forms.button();
   this.label3 = new system.windows.forms.label();
   this.button2 = new system.windows.forms.button();
   this.newmsg = new system.windows.forms.button();
   this.suspendlayout();
   //
   // box
   //
   this.box.itemheight = 12;
   this.box.location = new system.drawing.point(0, 32);
   this.box.name = "box";
   this.box.size = new system.drawing.size(200, 352);
   this.box.tabindex = 0;
   //
   // t_rec
   //
   this.t_rec.backcolor = system.drawing.systemcolors.inactiveborder;
   this.t_rec.forecolor = system.drawing.color.fromargb(((system.byte)(192)), ((system.byte)(64)), ((system.byte)(0)));
   this.t_rec.location = new system.drawing.point(208, 56);
   this.t_rec.multiline = true;
   this.t_rec.name = "t_rec";
   this.t_rec.scrollbars = system.windows.forms.scrollbars.vertical;
   this.t_rec.size = new system.drawing.size(328, 136);
   this.t_rec.tabindex = 1;
   this.t_rec.text = "";
   //
   // label1
   //
   this.label1.location = new system.drawing.point(208, 40);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(120, 16);
   this.label1.tabindex = 2;
   this.label1.text = "接收:";
   //
   // label2
   //
   this.label2.location = new system.drawing.point(208, 200);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(120, 16);
   this.label2.tabindex = 2;
   this.label2.text = "發送:";
   //
   // t_send
   //
   this.t_send.location = new system.drawing.point(208, 216);
   this.t_send.multiline = true;
   this.t_send.name = "t_send";
   this.t_send.scrollbars = system.windows.forms.scrollbars.vertical;
   this.t_send.size = new system.drawing.size(328, 112);
   this.t_send.tabindex = 1;
   this.t_send.text = "";
   //
   // button1
   //
   this.button1.location = new system.drawing.point(440, 344);
   this.button1.name = "button1";
   this.button1.size = new system.drawing.size(75, 32);
   this.button1.tabindex = 3;
   this.button1.text = "發  送";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // button3
   //
   this.button3.flatstyle = system.windows.forms.flatstyle.popup;
   this.button3.location = new system.drawing.point(88, 3);
   this.button3.name = "button3";
   this.button3.size = new system.drawing.size(56, 23);
   this.button3.tabindex = 4;
   this.button3.text = "刷 新";
   this.button3.click += new system.eventhandler(this.button3_click);
   //
   // label3
   //
   this.label3.font = new system.drawing.font("宋體", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
   this.label3.forecolor = system.drawing.color.blue;
   this.label3.location = new system.drawing.point(8, 8);
   this.label3.name = "label3";
   this.label3.size = new system.drawing.size(80, 16);
   this.label3.tabindex = 5;
   this.label3.text = "在線用戶:";
   //
   // button2
   //
   this.button2.location = new system.drawing.point(360, 344);
   this.button2.name = "button2";
   this.button2.size = new system.drawing.size(75, 32);
   this.button2.tabindex = 6;
   this.button2.text = "清  空";
   this.button2.click += new system.eventhandler(this.button2_click);
   //
   // newmsg
   //
   this.newmsg.location = new system.drawing.point(208, 56);
   this.newmsg.name = "newmsg";
   this.newmsg.size = new system.drawing.size(328, 136);
   this.newmsg.tabindex = 7;
   this.newmsg.text = "有新消息了!";
   this.newmsg.click += new system.eventhandler(this.newmsg_click);
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(544, 389);
   this.controls.add(this.button2);
   this.controls.add(this.label3);
   this.controls.add(this.button3);
   this.controls.add(this.button1);
   this.controls.add(this.label1);
   this.controls.add(this.box);
   this.controls.add(this.label2);
   this.controls.add(this.t_send);
   this.controls.add(this.t_rec);
   this.controls.add(this.newmsg);
   this.name = "form1";
   this.text = "小y";
   this.closing += new system.componentmodel.canceleventhandler(this.form1_closing);
   this.load += new system.eventhandler(this.form1_load);
   this.resumelayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程序的主入口點。
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new form1());
  }

  private void form1_load(object sender, system.eventargs e)
  {
  
  
   end=new ipendpoint(groupaddress,groupport);
   //初始化計算機名和端口
   iphostentry myentry=dns.gethostbyname(dns.gethostname());
   ipaddress myaddress=new ipaddress(myentry.addresslist[0].address);
   machinename=dns.gethostname();
   machineip=myaddress.tostring();

   box.items.add("ip            主機名");

   //開啟監聽線程
   runing=true;
   thread mythread=new thread(new threadstart(this.listenport));
   mythread.start();

   //發送上線信息
   string sends="0&"+machineip+"&"+machinename+"&"+machineip;
   sendpack(sends);
  }

  //偵聽指定端口的廣播地址udp包
  public void listenport()
  {
   ipendpoint tempend=new ipendpoint(ipaddress.any,2525);
   while(runing)
   {
    application.doevents();
    try
    {
     byte[] recb=listen.receive(ref tempend);

     // 檢查所接收到的信息
     checkmessage(recb);
    }
    catch(exception e)
    {
     messagebox.show(e.message.tostring());
     break;
    }
   
   }
   listen.close();
   box.items.add("線程已經退出!");
   runing=false;
  }

  //循環接收包
  public void checkmessage(byte[] recbb)
  {
   string recstr=encoding.default.getstring(recbb);
   string[] rec=recstr.split('&');
   switch(rec[0])
   {
    case "0": //刷新
     if(rec[3]==machineip)
     {
      if(box.findstring(rec[1]+"  "+rec[2])<=0)
           box.items.add(rec[1]+"  "+rec[2]);
     }
     else  if(rec[1]==rec[3])
     {
      //從其他機器發送過來的刷新請求
      //返回自己的信息
      string sendstr="0&"+machineip+"&"+machinename+"&"+rec[3];
      this.sendpack(sendstr);
      //如果不存在則添加該用戶
      if(box.findstring(rec[1]+"  "+rec[2])<=0)
       box.items.add(rec[1]+"  "+rec[2]);
     }
     break;
    case "1": //發言
     if(rec[3]==machineip)
     {
      romename=rec[2];
      romeip=rec[1];
      romecon=rec[4];
      showne();
     }
     break;
    case "2": //傳文件
     break;
    case "9": //確認包
     if(rec[3]==machineip)
      messagebox.show("信息來自:rec[2]("+rec[1]+")/r/n"+rec[4],"消息");
     break;
   }
  
  }
      
  //發送數據包到廣播地址
  public void sendpack(string sendstr)
  {
  
   byte[] sendb=encoding.default.getbytes(sendstr);
   try
   {
    listen.send(sendb,sendb.length,end);
   }
   catch(exception e)
   {
    messagebox.show(e.message.tostring());
   }
  }


  //刷新
  private void button3_click(object sender, system.eventargs e)
  {
   box.items.clear();
   box.items.add("ip            主機名");
   string temp2="0&"+machineip+"&"+machinename+"&"+machineip;
   this.sendpack(temp2);
  }

  //關閉循環
  private void form1_closing(object sender, system.componentmodel.canceleventargs e)
  {
   runing=false;
   udpclient mm=new udpclient();
   ipendpoint tempipend=new ipendpoint(ipaddress.parse("127.0.0.1"),2525);
   string temps="0&0&0&0&0";
   byte[] sendb=encoding.default.getbytes(temps);
   mm.send(sendb,sendb.length,tempipend);
  }
  //發送
  private void button1_click(object sender, system.eventargs e)
  {
   if(t_send.text==null)
   {
    messagebox.show("不能發送空信息!");
    return;
   }
   if(box.selecteditem==null||box.selectedindex==0)
   {
    messagebox.show("請先選擇一個用戶!");
    return;
   }
   string send="1&"+machineip+"&"+machinename+"&"+box.selecteditem.tostring().split(' ')[0]+"&"+t_send.text;
   this.sendpack(send);
  }
  //清空
  private void button2_click(object sender, system.eventargs e)
  {
   t_send.text=string.empty;
   t_rec.text=string.empty;
  }
  //單擊顯示接收的信息并隱藏自身
  private void newmsg_click(object sender, system.eventargs e)
  {
   t_rec.text="消息來自"+romename+"("+romeip+")"+datetime.now.toshortdatestring();
   t_rec.text+="/r/n> "+romecon;
   string back="9&"+machineip+"&"+machinename+"&"+romeip+"&信息被打開!";
   this.sendpack(back);
   newmsg.sendtoback();

  }
  //當有信息時顯示提示按鈕
  public void showne()
  {
   //窗體顯示
   if(this.windowstate!=formwindowstate.normal)
   {
    this.visible=true;
    this.windowstate=formwindowstate.normal;
   }
   newmsg.bringtofront();
  }
 }
}

 

 

,歡迎訪問網頁設計愛好者web開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 射阳县| 屏南县| 巨野县| 乾安县| 明溪县| 出国| 新竹市| 枣阳市| 枞阳县| 昭平县| 淅川县| 林芝县| 长白| 田林县| 志丹县| 沂水县| 松潘县| 阜新| 托克逊县| 松滋市| 东莞市| 铅山县| 西青区| 克山县| 临潭县| 浮山县| 防城港市| 新绛县| 贞丰县| 文安县| 通州市| 清镇市| 泾源县| 山西省| 佛教| 六枝特区| 红桥区| 瓦房店市| 田阳县| 长武县| 满洲里市|