• socket通讯


    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Net.WebSockets;

    namespace WebSocketTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("start....");
    //var server = new WebSocet();
    //server.StartAccept(socket =>
    // {
    // socket.OnOpen = () => Console.WriteLine("Open!");
    // socket.OnClose = () => Console.WriteLine("Close!");
    // socket.OnMessage = message => socket.Send(message);
    // });


    //int port = 1234;
    //string host = "127.0.0.1";

    //IPAddress ip = IPAddress.Parse(host);
    //IPEndPoint ipe = new IPEndPoint(ip, port);

    //Socket sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    //sSocket.Bind(ipe);
    //sSocket.Listen(0);
    //Console.WriteLine("监听已经打开,请等待");

    ////receive message
    //Socket serverSocket = sSocket.Accept();
    //Console.WriteLine("连接已经建立");
    //string recStr = "";
    //byte[] recByte = new byte[4096];
    //int bytes = serverSocket.Receive(recByte, recByte.Length, 0);
    //recStr += Encoding.ASCII.GetString(recByte, 0, bytes);

    ////send message
    //Console.WriteLine("服务器端获得信息:{0}", recStr);
    //string sendStr = "send to client :hello";
    //byte[] sendByte = Encoding.ASCII.GetBytes(sendStr);
    //serverSocket.Send(sendByte, sendByte.Length, 0);
    //serverSocket.Close();
    //sSocket.Close();

    //int port = 1234;
    //string host = "127.0.0.1";//服务器端ip地址

    //IPAddress ip = IPAddress.Parse(host);
    //IPEndPoint ipe = new IPEndPoint(ip, port);

    // Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    //clientSocket.Connect(ipe);

    ////send message
    //string sendStr = "begin#70";
    //byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
    //clientSocket.Send(sendBytes);

    ////receive message
    //string recStr = "";
    //byte[] recBytes = new byte[4096];
    //int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
    //recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
    //Console.WriteLine(recStr);

    //clientSocket.Close();

    ////服务端:
    //var listener = new HttpListener();
    //listener.Prefixes.Add("http://*:8080/");
    //listener.Start();
    //var context = listener.GetContextAsync();
    //var wsContext = context.Result.AcceptWebSocketAsync("");
    //var ws = wsContext.Result.WebSocket;
    ////客户端:
    //var cln = new ClientWebSocket();
    //cln.ConnectAsync(new Uri("ws://127.0.0.1:8080"), CancellationToken.None);

    //4.0方式
    string url = "ws://192.168.16.134:1234/";

    try
    {
    ClientWebSocket cln = new ClientWebSocket();
    cln.ConnectAsync(new Uri(url), new CancellationToken()).Wait();

    byte[] bytess = Encoding.Default.GetBytes("begin#70");
    cln.SendAsync(new ArraySegment<byte>(bytess), WebSocketMessageType.Text, true, new CancellationToken()).Wait();
    byte[] bytes2 = new byte[1000 * 500];
    var webSocketReceiveResult = cln.ReceiveAsync(new ArraySegment<byte>(bytes2), CancellationToken.None);
    webSocketReceiveResult.GetAwaiter().GetResult();
    Thread.Sleep(10000);

    if (webSocketReceiveResult.IsCompleted)
    {
    //Console.WriteLine(webSocketReceiveResult.Result.));
    string xx = Encoding.GetEncoding("utf-8").GetString(bytes2, 0, webSocketReceiveResult.Result.Count);
    Console.WriteLine(xx);
    }
    //string xx = Encoding.Default.GetString(bytes2);
    Console.WriteLine("11111111111");

    }
    catch (Exception ex)
    {
    string ss = ex.ToString();
    Console.WriteLine(ss);
    //}
    Console.Read();
    }
    }
    // 服务端:
    //var listener = new HttpListener();
    // listener.Prefixes.Add("http://*:8080/");
    //listener.Start();
    //var context = await listener.GetContextAsync();
    //var wsContext = await context.AcceptWebSocketAsync(null);
    //var ws = wsContext.WebSocket;
    // 客户端:
    //var ws = new ClientWebSocket();
    //await ws.ConnectAsync(new Uri("ws://127.0.0.1:8080"), CancellationToken.None);

    #region MyRegion
    // 1. 使用.NET4.5 自带的WebSocket
    //服务端:
    //var listener = new HttpListener();
    // listener.Prefixes.Add("http://*:8080/");
    //listener.Start();
    //var context = await listener.GetContextAsync();
    // var wsContext = await context.AcceptWebSocketAsync(null);
    // var ws = wsContext.WebSocket;
    // 客户端:
    //var ws = new ClientWebSocket();
    // await ws.ConnectAsync(new Uri("ws://127.0.0.1:8080"), CancellationToken.None);

    //2. 使用 websocket-sharp dll提供的 WebSocket(支持.NET4.0)
    //客户端:

    //_caiNiaoWs = new WebSocket(WsUrl);
    // _caiNiaoWs.OnMessage += (sender, e) => OnMessage(e.Data);
    // _caiNiaoWs.Connect();

    // 3. 使用 Socket 类(支持.NET4.0)
    //http://blog.csdn.net/qq_29701359/article/details/53130050
    #endregion
    }
    }

  • 相关阅读:
    Android Studio插件
    android漂亮的对话框项目sweet-alert-dialog
    Android中Context详解 ---- 你所不知道的Context
    Bundle对象的使用
    Android利用Http下载文件
    文件缓存(配合JSON数组)
    android studio sqlite操作代码片段
    Android中使用ListView实现分页刷新(线程休眠模拟)(滑动加载列表)
    Android Studio 配置使用百度api (附带简单样例)
    9套Android实战经典项目资料分享给大家
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/9874858.html
Copyright © 2020-2023  润新知