zoukankan      html  css  js  c++  java
  • websocket通讯客户端

    winform.cs代码   client端代码 见下:

     这是客户端代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Net;
      8 using System.Net.Sockets;
      9 using System.Text;
     10 using System.Threading;
     11 using System.Threading.Tasks;
     12 using System.Windows.Forms;
     13 
     14 namespace TcpMsgClient
     15 {
     16 public partial class FrmClient : Form
     17 {
     18 public FrmClient()
     19 {
     20 InitializeComponent();
     21 //关闭对文本框的非法线程操作检查
     22 TextBox.CheckForIllegalCrossThreadCalls = false;
     23 }
     24 //创建 1个客户端套接字 和1个负责监听服务端请求的线程 
     25 Socket socketClient = null;
     26 Thread threadClient = null;
     27 
     28 /// <summary>
     29 /// 连接服务端事件
     30 /// </summary>
     31 /// <param name="sender"></param>
     32 /// <param name="e"></param>
     33 private void btnListenServer_Click(object sender, EventArgs e)
     34 {
     35 //定义一个套字节监听 包含3个参数(IP4寻址协议,流式连接,TCP协议)
     36 socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     37 //需要获取文本框中的IP地址
     38 IPAddress ipaddress = IPAddress.Parse(this.txtIP.Text.Trim());
     39 //将获取的ip地址和端口号绑定到网络节点endpoint上
     40 IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(this.txtPort.Text.Trim()));
     41 //这里客户端套接字连接到网络节点(服务端)用的方法是Connect 而不是Bind
     42 try
     43 {
     44 socketClient.Connect(endpoint);
     45 this.txtMsg.AppendText("客户端连接服务端成功!" + "
    ");
     46 this.btnListenServer.Enabled = false;
     47 //创建一个线程 用于监听服务端发来的消息
     48 threadClient = new Thread(RecMsg);
     49 //将窗体线程设置为与后台同步
     50 threadClient.IsBackground = true;
     51 //启动线程
     52 threadClient.Start();
     53 }
     54 catch (Exception ex) {
     55 this.txtMsg.AppendText("远程服务端断开,连接失败!" + "
    ");
     56 } 
     57 }
     58 
     59 /// <summary>
     60 /// 接收服务端发来信息的方法
     61 /// </summary>
     62 private void RecMsg()
     63 {
     64 while (true) //持续监听服务端发来的消息
     65 {
     66 try
     67 {
     68 //定义一个1M的内存缓冲区 用于临时性存储接收到的信息
     69 byte[] arrRecMsg = new byte[1024 * 1024];
     70 //将客户端套接字接收到的数据存入内存缓冲区, 并获取其长度
     71 int length = socketClient.Receive(arrRecMsg);
     72 //将套接字获取到的字节数组转换为人可以看懂的字符串
     73 string strRecMsg = Encoding.UTF8.GetString(arrRecMsg, 0, length);
     74 //将发送的信息追加到聊天内容文本框中
     75 txtMsg.AppendText("服务端 " + GetCurrentTime() + "
    " + strRecMsg + "
    ");
     76 }
     77 catch (Exception ex) {
     78 this.txtMsg.AppendText("远程服务器已中断连接!"+"
    ");
     79 this.btnListenServer.Enabled = true;
     80 break;
     81 }
     82 }
     83 }
     84 
     85 /// <summary>
     86 /// 发送字符串信息到服务端的方法
     87 /// </summary>
     88 /// <param name="sendMsg">发送的字符串信息</param>
     89 private void ClientSendMsg(string sendMsg)
     90 {
     91 try { 
     92 //将输入的内容字符串转换为机器可以识别的字节数组
     93 byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(sendMsg);
     94 //调用客户端套接字发送字节数组
     95 socketClient.Send(arrClientSendMsg);
     96 //将发送的信息追加到聊天内容文本框中
     97 txtMsg.AppendText("天涯 " + GetCurrentTime() + "
    " + sendMsg + "
    ");
     98 }
     99 catch(Exception ex){
    100 this.txtMsg.AppendText("远程服务器已中断连接,无法发送消息!" + "
    ");
    101 }
    102 }
    103 
    104 private void btnSendMsg_Click(object sender, EventArgs e)
    105 {
    106 //调用ClientSendMsg方法 将文本框中输入的信息发送给服务端
    107 ClientSendMsg(this.txtClientSendMsg.Text.Trim());
    108 this.txtClientSendMsg.Clear();
    109 }
    110 
    111 private void txtClientSendMsg_KeyDown(object sender, KeyEventArgs e)
    112 {
    113 //当光标位于文本框时 如果用户按下了键盘上的Enter键 
    114 if (e.KeyCode == Keys.Enter)
    115 {
    116 //则调用客户端向服务端发送信息的方法
    117 ClientSendMsg(this.txtClientSendMsg.Text.Trim());
    118 this.txtClientSendMsg.Clear();
    119 }
    120 }
    121 
    122 /// <summary>
    123 /// 获取当前系统时间的方法
    124 /// </summary>
    125 /// <returns>当前时间</returns>
    126 private DateTime GetCurrentTime()
    127 {
    128 DateTime currentTime = new DateTime();
    129 currentTime = DateTime.Now;
    130 return currentTime;
    131 }
    132 }
    133 }
  • 相关阅读:
    异常
    动态链接库与静态链接库的区别
    OpenBLAS 安装与使用
    Eigen 优化技巧
    C++读取保存为二进制的 numpy 数组
    Sublime Text Windows版使用Conda环境
    Repeater 时间格式化
    C#与js的各种交互
    js 实现精确加减乘除
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/zonzi/p/12575793.html
Copyright © 2011-2022 走看看