您好,欢迎来到Unity之家!   unity.jb51.net 
  • 首 页
  • 你问我答
  • 当前位置:首页 > 学习培训 > Unity编程 >
    socket编程 tcp/ip
    时间:2015-04-07 10:25 来源:Unity之家 作者:unity.jb51.net 浏览:收藏 挑错 推荐 打印


    直接贴出代码:



    一:客户端代码


        using System;      
        using System.Collections.Generic;      
        using System.Text;      
        using System.Net;      
        using System.IO;      
        using System.Net.Sockets;      
        namespace client      
        {      
            class Program      
            {      
                static void Main(string[] args)      
                {      
                    try      
                    {      
                        //创建客户端socket,使用tcp通信协议      
                        TcpClient TcpClient = new TcpClient();      
                        TcpClient.Connect("127.0.0.1", 8888);   //指定地址与端口号      
                        Console.WriteLine("Connect Complete");      
                        Console.WriteLine("Input string:");      
                        //Connect方法使用指定的IP地址和端口号将客户端连接到远程TCP主机      
                        //读入控制台输入的文字      
                        String str = Console.ReadLine();      
                        Stream stm = TcpClient.GetStream();      
                         //Unity3D教程手册:www.unitymanual.com    
                        //定义数据流,用于发送和接收数据      
                        ASCIIEncoding AS = new ASCIIEncoding();      
                        //将字符串转换为byte类型      
                        byte[] b = AS.GetBytes(str);      
                        //Write(参数1,参数2,参数3)表示向服务端发送字符串      
                        //参数1指将此数组复制到当前流,参数2指从零开始的字节偏移量,参数3指要写入当前流的字节数(即字符串长度)      
                        stm.Write(b, 0, b.Length);      
                        Console.WriteLine("Send Complete");      
                        //Unity3D教程手册:www.unitymanual.com    
                        //stm.Read在当前流中读入服务端发来的响应信息,其参数与Write方法参数一致,k值为读入字符串的长度      
                        byte[] bb = new byte[100];      
                        int k = stm.Read(bb, 0, 100);      
                        for (int i = 0; i < k; i++)      
                            Console.Write(Convert.ToChar(bb[i]));      
                        TcpClient.Close();      
                        //释放TcpClient实例,并不关闭基础连接      
                        Console.ReadLine();      
                    }      
                    catch (Exception e)      
                    {      
                        Console.WriteLine("Error!" + e.StackTrace);      
                    }      
                }      
            }      
        }


    二:服务端代码


        using System;      
        using System.Collections.Generic;      
        using System.Text;      
        using System.Net;      
        using System.Net.Sockets;      
        namespace server      
        {      
            class Program      
            {      
                static void Main(string[] args)      
                {      
                    try      
                    {      
                        //创建listener,ip-地址,port-端口      
                        IPAddress Ip = IPAddress.Parse("127.0.0.1");          
                        TcpListener TcpList = new TcpListener(Ip, 8888);      
                        TcpList.Start();      
                        Console.WriteLine("Server start!");      
                        Console.WriteLine("Ip address:" + TcpList.LocalEndpoint);      
                        //LocalEndpoint获取服务端(即本地)地址与端口等信息      
                        Console.WriteLine("Wait");      
                        //获取监听到的socket      
                        Socket Soc = TcpList.AcceptSocket();      
                        Console.WriteLine("Received Connection:" + Soc.RemoteEndPoint);      
                        //接收到的信息    //Unity3D教程手册:www.unitymanual.com    
                        byte[] b = new byte[100];      
                        //Soc.Receive(b)从socket接收数据,将数据存入接收缓冲区列表中,k的值为该数据的长度      
                        int k = Soc.Receive(b);      
                        Console.WriteLine("Received data from client:");      
                        for (int i = 0; i < k; i++)      
                            //Convert.ToChar(b[i])将数组b转换基本数据类型为char的类型并输出      
                            Console.Write(Convert.ToChar(b[i]));      
                        //发送信息      
                        //ASCIIEncoding表示Unicode字符的ASCII字符编码类      
                        ASCIIEncoding AS = new ASCIIEncoding();      
                        //Unity3D教程手册:www.unitymanual.com    
                        //Soc.Send向客户端发送数据,AS.GetBytes()获得括号中字符串的bytes值      
                        Soc.Send(AS.GetBytes("Received data!"));      
                        //关闭连接并释放所有关联的资源      
                        Soc.Close();      
                        //关闭侦听      
                        TcpList.Stop();      
                        //等待输入,起到暂停的作用      
                        Console.ReadLine();      
                    }      
                    catch(Exception e)      
                    {      
                        Console.WriteLine("Error!" + e.StackTrace);      
                        //获取当前异常发生时调用堆栈上的帧的字符串      
                    }      
                }      
            }      
        }


    赶快测试下吧。。。

    (责任编辑:脚印)
    免责声明:Unity之家部分内容来源于互联网,如有侵权,请联系我们,本站将立即进行处理。