由于我们技术方面大牛王以正同学的要求,我们将开发语言改为c#(软粉的要求啊)。之前的开发工作需要推倒重来T_T。不过程序框架、设计思路大体不变。软件正式命名为OpenTalk。

  目前用户端代码如下:

  

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Linq;
  5 using System.Net;
  6 using System.Net.Sockets;
  7 using System.Text;
  8 
  9 namespace OpenTalk
 10 {
 11     class Client
 12     {
 13         private Socket socket;
 14         private SocketAsyncEventArgs receiveEvent;
 15         private volatile bool closed = true;
 16         private string name;
 17 
 18         public Client()
 19         {
 20             
 21         }
 22 
 23         public void Connect(string name, IPAddress address, bool pseudoClient)
 24         {
 25             this.name = name;
 26             SocketAsyncEventArgs e = new SocketAsyncEventArgs();
 27             e.RemoteEndPoint = new IPEndPoint(address, 8880);
 28             e.Completed += (sender, args) =>
 29             { //连接服务器
 30                 if (args.SocketError == SocketError.Success)
 31                 {
 32                     closed = false;
 33                     MainForm.INSTANCE.PrintInfo("连接成功", "");
 34                     if (!pseudoClient)
 35                         MainForm.INSTANCE.SetClientControls();
 36                     socket.Send(Encoding.Default.GetBytes(name)); //发送用户姓名
 37                     receiveEvent = new SocketAsyncEventArgs();
 38                     receiveEvent.SetBuffer(new byte[1024], 0, 1024);
 39                     receiveEvent.Completed += ReceiveEventOnCompleted;
 40                     socket.ReceiveAsync(receiveEvent);
 41                 }
 42                 else
 43                 {
 44                     MainForm.INSTANCE.PrintInfo("连接失败", "连接错误:" + args.SocketError);
 45                     MainForm.INSTANCE.FreeControls();
 46                 }
 47             };
 48             socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
 49             socket.ConnectAsync(e);
 50         }
 51 
 52         //处理接受到的服务器信息
 53         private void ReceiveEventOnCompleted(object sender, SocketAsyncEventArgs socketAsyncEventArgs)
 54         {
 55             if (socketAsyncEventArgs.BytesTransferred > 0)
 56             {
 57                 byte[] info = socketAsyncEventArgs.Buffer;
 58                 string s = Encoding.Default.GetString(info, 1, info.Length - 1);
 59                 switch (info[0])
 60                 {
 61                     case 0: //如果是聊天信息
 62                         string sdr = s.Substring(0, s.IndexOf(':'));
 63                         string message = s.Substring(s.IndexOf(':') + 1);
 64                         MainForm.INSTANCE.PrintInfo(sdr, message,
 65                             sdr.Equals(name) ? Color.Green : Color.Blue);
 66                         break;
 67                     case 1: //如果是用户列表
 68                         string[] users = s.Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);
 69                         MainForm.INSTANCE.ShowUsers(users);
 70                         break;
 71                 }
 72                 Array.Clear(socketAsyncEventArgs.Buffer, 0, socketAsyncEventArgs.Buffer.Length);
 73                 socket.ReceiveAsync(receiveEvent);
 74             }
 75             else
 76             {
 77                 if (!closed)
 78                 {
 79                     closed = true;
 80                     MainForm.INSTANCE.PrintInfo("远端主机已关闭", "");   
 81                 }
 82                 socket.Close();
 83                 MainForm.INSTANCE.FreeControls();
 84             }
 85         }
 86 
 87         public void Send(string s)
 88         {
 89             var sendEvent = new SocketAsyncEventArgs();
 90             if (s.Length == 0)
 91                 s = " ";
 92             byte[] info = Encoding.Default.GetBytes(s);
 93             sendEvent.SetBuffer(info, 0, info.Length);
 94             socket.SendAsync(sendEvent);
 95         }
 96 
 97         public void Disconnect()
 98         {
 99             if (!closed)
100             {
101                 closed = true;
102                 MainForm.INSTANCE.PrintInfo("已同主机断开连接", "");
103             }
104             socket.Send(new byte[] {0});
105             socket.Close();
106             MainForm.INSTANCE.FreeControls();
107         }
108     }
109 }

 

posted on 2016-06-20 23:48  13070017张哲远  阅读(123)  评论(0编辑  收藏  举报