博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Socket模块
阅读量:1982 次
发布时间:2019-04-27

本文共 2756 字,大约阅读时间需要 9 分钟。

服务端

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Runtime.InteropServices;using System.Net;using System.Net.Sockets;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            //本机IP            IPAddress ip = IPAddress.Parse("192.168.0.154");            //IP地址跟端口的组合            IPEndPoint iep = new IPEndPoint(ip, 9000);            //创建Socket            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            //绑定Socket            socket.Bind(iep);            //服务器已经做好接收任何连接的准备            socket.Listen(10);            //执行accept方法            Socket Client = socket.Accept();            while (true)            {                byte[] message = new byte[1024];                NetworkStream networkStream = new NetworkStream(Client);                int len = networkStream.Read(message, 0, message.Length);                if (len > 0)                {  byte数组转换成string                //string output = System.Text.Encoding.Unicode.GetString(message);                //Console.WriteLine("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:" + output);                }            }        }    }}

客户端

using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;using System.Threading;using UnityEngine;class ClientNet{    private Socket init()    {        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);        return clientSocket;    }    public bool Connect(string host,int port)    {        if (m_socket == null)            m_socket = init();        //要连接的远程IP        IPAddress remoteHost = IPAddress.Parse(host);        //IP地址跟端口的组合        IPEndPoint iep = new IPEndPoint(remoteHost, port);        try         {            m_socket.Connect(iep);            return true;        }        catch (System.Exception ex)        {            Debug.LogError("connect error");            return false;        }    }    public void SendData(byte[] bytes)    {        NetworkStream netstream = new NetworkStream(m_socket);        netstream.Write(bytes, 0, bytes.Length);    }    public void CloseSocket()    {        m_socket.Shutdown(SocketShutdown.Both);        m_socket.Close();    }    private Socket m_socket;    private static ClientNet s_instance;    private static readonly byte[] c_staticLocker = new byte[0];    public static ClientNet Instance    {        get        {            if (s_instance == null)            {                lock (c_staticLocker)                {                    s_instance = new ClientNet();                }            }            return s_instance;        }    }}

转载地址:http://ugzvf.baihongyu.com/

你可能感兴趣的文章
【NLP学习笔记】词性标注(Part-of-speech Tagging, POS)
查看>>
【NLP学习笔记】语义角色标注 (Semantic Role Labeling, SRL)
查看>>
《知识图谱》阅读笔记(七)
查看>>
《知识图谱》阅读笔记(九)
查看>>
【超越白皮书7】你需要知道关于ETH2.0的几个事实
查看>>
超越白皮书8:穿云而过的闪电网络
查看>>
AMM做市无常损失对冲分析系列(一)—— 损益及期权对冲模型构建
查看>>
JS中document对象和window对象有什么区别
查看>>
【python练习题】遍历1
查看>>
【matlab】显示图片且下方显示文字
查看>>
关于greater<int>以及类模板的一些理解
查看>>
对于时间复杂度的通俗理解
查看>>
如何输入多组数据并输出每组数据的和?
查看>>
基于CentOS 7的Linux常用命令行命令
查看>>
行阶梯型矩阵
查看>>
信号量机制
查看>>
临界资源与临界区
查看>>
matlab中uint8,double,im2double和im2uint8的区别
查看>>
数字图像处理总复习
查看>>
图像去噪(包含修正的阿尔法均值滤波及通用滤波方法代码)
查看>>