Exemplo n.º 1
0
        private void UpdateEmoji(String sendside, int emojinumber)//ui刷新函数,传入值为发送者
        {
            //this.richTextBox1.Update(); //.BeginUpdate();//开始刷新
            RichTextBox lvi       = new RichTextBox();
            Image       tempemoji = Properties.Resources.emoji;

            //string temptext = sendside + ":\n\n";
            lvi.Text = "\n" + sendside + "\n";//设置item对象文本值
            try
            {
                ResourceManager rm = new ResourceManager(typeof(Properties.Resources));//读取Resource文件中嵌入的图片文件
                tempemoji = (Image)rm.GetObject("bq__" + emojinumber.ToString() + "_");
            }
            catch (Exception e)
            {
                MessageBox.Show("异常" + e.Message);
            }
            //tempemoji = LikeResourceManager.LoadEmoji(emojinumber);
            richTextBox1.AppendText(lvi.Text);
            richTextBox1.AppendText(":\n");
            StaticTools.InsertImage(this.richTextBox1, tempemoji);
            //this.richTextBox1.AppendText(lvi.Text);//Add(lvi);//添加item对象
            //this.richrichTextBox2. //.EndUpdate();//停止刷新
            //this.richrichTextBox2.Focus();
            richTextBox1.AppendText("\n");
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.ScrollToCaret();
        }
Exemplo n.º 2
0
        //public static string defaultpicpath = @"./savepicture";
        //public static string defaultfilepath = @"./savefile";
        public static string ReceiveFile(int rect, string sendside, string fileext, byte[] data, Socket tsocket)
        {
            FileStream wrtr; //文件读写类
            //server.Listen(10); //监听
            //Socket s = server.Accept(); //当有客户端与服务器进行连接,Accept方法返回socket对象,通过该对象可以获取客户端发送的消息
            //byte[] data = new byte[4];
            //int rect = tsocket.Receive(data, 2, 4, 0); //用来接收图片字节流长度
            int size     = BitConverter.ToInt32(data, 2); //16进制转成int型
            int dataleft = size;

            data = new byte[size];  //创建byte组
            string filepath = @"./savefile/newfile in ";
            string foldpath = Path.GetDirectoryName(filepath);

            Directory.CreateDirectory(foldpath);                                      //如果没有文件夹,则创建
            //filepath = StaticTools.AppendTimeStamp(filepath);
            filepath = Path.Combine(foldpath, StaticTools.AppendTimeStamp(filepath)); //加入时间戳,并与前置路径连接
            filepath = Path.ChangeExtension(filepath, fileext);
            wrtr     = new FileStream(filepath, FileMode.Create);
            //创建新文件"new.jpg",strPath是路径
            //data = new byte[2048];
            int total = 0;

            while (total < size)                                       //当接收长度小于总长度时继续执行
            {
                rect      = tsocket.Receive(data, total, dataleft, 0); //接收字节流,receive方法返回int获取已接收字节个数,第一个参数是需要写入的字节组,第二个参数是起始位置,第三个参数是接收字节的长度
                total    += rect;                                      //已接收个数-下一次从当前个数开始接收
                dataleft -= rect;                                      //剩下的字节长度
            }
            wrtr.Write(data, 0, data.Length);                          //输出文件
            wrtr.Flush();                                              //强制输出
            wrtr.Close();                                              //关闭文件流对象
            //File.Delete(strpath);
            return(filepath);
        }
Exemplo n.º 3
0
 private void Form2_FormClosing(object sender, FormClosingEventArgs e)                                                                                            //点击窗口x调用该函数
 {
     if (netflag == 0)                                                                                                                                            //判断网络标志是否为0,也就是是否成功连接到网络。
     {
         newtcpclient.Send(StaticTools.CombomBinaryArray(BitConverter.GetBytes(firstflag), BitConverter.GetBytes(nextflag), Encoding.UTF8.GetBytes("exitthis"))); //设置字符编码为UTF-8并发送退出消息
         newtcpclient.Shutdown(SocketShutdown.Both);                                                                                                              //停止socket连接
         newtcpclient.Close();                                                                                                                                    //关闭socket连接
     }
     thread.Abort();                                                                                                                                              //关闭子线程
     f1.Show();                                                                                                                                                   //显示登录窗口form1
 }
Exemplo n.º 4
0
 public void button1_Click(object sender, EventArgs e) //点击发送按钮调用该函数
 {
     if (!richTextBox2.Text.Equals(""))                //判断输入的消息是否为空
     {
         if (netflag == 0)                             //判断网络连接是否成功
         {
             byte[] tosend = Encoding.UTF8.GetBytes("\n" + name + ":\n" + richTextBox2.Text + "\n");
             byte[] temp   = StaticTools.CombomBinaryArray(BitConverter.GetBytes(firstflag), BitConverter.GetBytes(nextflag), tosend);
             newtcpclient.Send(temp); //发送socket消息,并编码UTF-8
             richTextBox2.Text = "";  //清空发送栏
             pictobyte.Clear();       //清空表情转字节流的缓存区
             richTextBox2.Focus();    //将光标聚焦在输入框
         }
     }
 }
Exemplo n.º 5
0
        private void UpdateFile(String sendside, string filepath) //ui刷新函数,传入值为消息
        {
            this.richTextBox1.Update();                           //.BeginUpdate();//开始刷新
            RichTextBox lvi = new RichTextBox();
            PictureBox  pic = new PictureBox();

            lvi.Text = "\n" + sendside + "\n";      //设置item对象文本值
            this.richTextBox1.AppendText(lvi.Text); //Add(lvi);//添加item对象
            richTextBox1.AppendText(":\n");
            //this.richrichTextBox2. //.EndUpdate();//停止刷新
            //this.richrichTextBox2.Focus();
            if (Path.GetExtension(filepath).Contains(".png") || Path.GetExtension(filepath).Contains(".jpg") || Path.GetExtension(filepath).Contains(".jpeg"))
            {
                StaticTools.InsertImage(richTextBox1, Image.FromFile(filepath));
            }
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("文件保存于 " + Path.GetFullPath(filepath));
            richTextBox1.AppendText("\n");
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.ScrollToCaret();
        }
Exemplo n.º 6
0
        private void button4_Click(object sender, EventArgs e)
        {
            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();

            //fileDialog.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg";
            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(fileDialog.FileName);
                //声明允许的后缀名
                //获取用户选择的文件,并判断文件大小不能超过2000K,fileInfo.Length是以字节为单位的
                FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                if (fileInfo.Length > 2048000)
                {
                    MessageBox.Show("上传的文件不能大于2000K");
                }
                else    //在这里就可以写获取到正确文件后的代码了
                {
                    string filepath = Path.GetFullPath(fileDialog.FileName);
                    byte[] bytes    = StaticTools.GetFileBytes(filepath);  //图片路径,转成字节流

                    MessageBox.Show("创建字节流成功" + filepath);
                    byte[] fileext = new byte[4];
                    fileext = Encoding.UTF8.GetBytes(Path.GetExtension(filepath));
                    //client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//实例化Socket对象
                    //client.Connect(iep);//与该ip地址进行连接
                    byte[] datasize = new byte[4];
                    datasize = BitConverter.GetBytes(bytes.Length);        //把长度作为16进制数放在datasize中
                    byte[] temp = new byte[6];
                    temp = StaticTools.CombomBinaryArray(BitConverter.GetBytes(true), BitConverter.GetBytes(false), datasize, fileext);
                    newtcpclient.Send(temp);                                  //发送字节流长度给服务器
                    newtcpclient.Send(bytes, bytes.Length, SocketFlags.None); //发送图片字节
                }
                //}
            }
        }
Exemplo n.º 7
0
        private void pictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pic = sender as PictureBox;//定义鼠标当前点击picureBox的行为

            if (pic == null)
            {
                return;
            }
            string emojinumber = ((sender) as PictureBox).Tag.ToString();
            Image  clickemoji  = pic.Image;

            MessageBox.Show(string.Format("选中{0}号表情", emojinumber));
            //pictobyte.Add(StaticTools.BmpConvertByte(clickemoji));
            //Clipboard.SetDataObject(clickemoji);
            //richTextBox2.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
            //StaticTools.InsertImage(this.richTextBox1,clickemoji);
            //Clipboard.Clear();
            //byte[] tempemoji = new byte[2048];
            //byte[] tempemoji=StaticTools.BmpConvertByte(clickemoji);
            //int tempsize = tempemoji.Length;
            byte[] size      = new byte[4];
            byte[] selectidx = new byte[4];
            bool   createidxofemoji;

            if (!(createidxofemoji = StaticTools.ConvertIntToByteArray(int.Parse(emojinumber), ref selectidx)))
            {
                return;
            }

            nextflag = true;
            newtcpclient.Send(StaticTools.CombomBinaryArray(BitConverter.GetBytes(firstflag), BitConverter.GetBytes(nextflag), selectidx));
            //newtcpclient.Send(tempemoji);
            //richTextBox2.AppendText(clickemoji.ToString());
            ////string tag = pic.Tag.ToString();
            //string tag = pic.Created.ToString();
            //MessageBox.Show(tag);//显示每一个图片位置编号,其他的功能根据自己需要扩展
            ////this.groupBox.Refresh();
        }
Exemplo n.º 8
0
        private void recv()                                                                             //子线程
        {
            byte[] data = new byte[2048];                                                               //byte数据类型,用于保存接受的socket数据
            newtcpclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //实例化socket对象
            try
            {
                IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));//设置ip地址与端口号
                try
                {
                    newtcpclient.Connect(ie);//开始连接
                }
                catch (SocketException err)
                {
                    Updatetext("与服务器无法建立连接!请确保服务端已经开启,且IP与端口都输入无误\n自动退出中......"); //抛出异常
                    //Updatetext(err.ToString());
                    netflag = 1;                                                  //设置网络标志位1,也就是无法连接到网络
                    Thread.Sleep(3000);
                    this.Dispose();
                    this.Close();
                    f1.Show();
                    return;
                }
            }
            catch (FormatException)
            {
                try
                {
                    ipdomain = Dns.GetHostAddresses(ip)[0];                    //DNS解析域名
                    IPEndPoint ie = new IPEndPoint(ipdomain, int.Parse(port)); //设置ip地址与端口号
                    //try
                    //{
                    newtcpclient.Connect(ie);//开始连接
                    //}
                }
                catch (SocketException err)
                {
                    Updatetext("与服务器无法建立连接!请确保服务端已经开启,且IP与端口都输入无误\n自动退出中......"); //抛出异常
                    //MessageBox.Show(err.ToString());
                    netflag = 1;                                                  //设置网络标志位1,也就是无法连接到网络
                    Thread.Sleep(3000);
                    this.Dispose();
                    this.Close();
                    f1.Show();
                    return;
                }
            }
            byte[] temp;
            //temp = StaticTools.CombomBinaryArray(BitConverter.GetBytes(StaticTools.OSHelper.IsUnix), Encoding.UTF8.GetBytes(name.ToCharArray()));
            temp = Encoding.UTF8.GetBytes(name.ToCharArray());
            newtcpclient.Send(temp);                                    //将判断是否为unix系统的结果和用户名发送给服务端
            int recv = newtcpclient.Receive(data);                      //接收服务器上线数据
            //MessageBox.Show(recv.ToString());
            string stringdata = Encoding.UTF8.GetString(data, 0, recv); //将byte数据转化为字符类型

            Updatetext(stringdata);                                     //传入ui数据并刷新
            this.richTextBox1.AppendText("\n");
            while (true)
            {
                byte[] temppattern = new byte[2048]; //byte数据类型,用于保存接受的【传输模式+长度+发送者】
                data = new byte[2048];               //byte数据类型,用于保存接受的socket数据
                string sendside;
                try
                {
                    recv = newtcpclient.Receive(temppattern);
                    if (Convert.ToBoolean(temppattern[0]))
                    {
                        if (Convert.ToBoolean(temppattern[1]))//闲置
                        {
                        }
                        else//文件
                        {
                            sendside = Encoding.UTF8.GetString(temppattern, 10, recv);
                            MessageBox.Show("prepare to receive the file " + sendside);
                            string fileext         = Encoding.UTF8.GetString(temppattern, 6, 4);
                            string DefaultFilePath = StaticTools.ReceiveFile(recv, sendside, fileext, temppattern, newtcpclient);
                            MessageBox.Show("Receive File Success");
                            UpdateFile(sendside, DefaultFilePath);
                            firstflag   = false;
                            temppattern = new byte[2048];
                        }
                    }
                    else
                    {
                        if (Convert.ToBoolean(temppattern[1]))//表情
                        {
                            //MessageBox.Show("Receiving Emoji and recv is " + recv.ToString());
                            sendside = Encoding.UTF8.GetString(temppattern, 6, recv);
                            int transemoji = BitConverter.ToInt32(temppattern, 2);
                            UpdateEmoji(sendside, transemoji);//打印表情
                            nextflag    = false;
                            temppattern = new byte[2048];
                        }
                        else//字(默认)
                        {
                            //MessageBox.Show("Receiving Text and recv is "+recv.ToString());
                            //recv = newtcpclient.Receive(data);//接收消息数据
                            //MessageBox.Show(recv.ToString())

                            stringdata = Encoding.UTF8.GetString(temppattern, 2, recv); //将byte数据转化为字符类型
                            //MessageBox.Show("stringdata is"+stringdata);
                            Updatetext(stringdata);                                     //传入ui数据刷新
                        }
                    }
                    //recv = newtcpclient.Receive(data);//接收消息数据
                    ////MessageBox.Show(recv.ToString());

                    //stringdata = Encoding.UTF8.GetString(data, 0, recv);//将byte数据转化为字符类型
                    //Updatetext(stringdata);//传入ui数据刷新
                }
                catch (SocketException s) {
                    this.Dispose();
                    this.Close();
                    //MessageBox.Show(thread.IsAlive.ToString());
                    f1.Show();
                    break;
                }
            }
        }