分享

unity学习Socket(一)

 3dC 2013-09-30

我之前写博客是为了写而写,不管质量都乱写,有时间得去清理一下。

说来感觉自己好悲哀啊,出去实习没做过网游,也几乎没用Socket,所以现在在学校没事做必须多了解一些网络通信这类的东西,从头开始学吧,呵呵。下面这个例子第一个很简单,大家别笑我哈,我很菜的。这个例子是用Socket的TCP协议做的,当然也可以用UDP和TCPListener来做。也没用到多线程啊,呵呵,其实就是为了看看里面的一些函数而已。

Server.cs:

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Net;  
  4. using System.IO;  
  5. using System.Net.Sockets;  
  6. using System.Text;  
  7.   
  8. public class Server : MonoBehaviour {  
  9.   
  10.     void Start () {  
  11.        OpenServer();  
  12.     }  
  13.       
  14.     void OpenServer()  
  15.     {  
  16.         IPAddress ipAdr = IPAddress.Parse("10.56.03.32");  
  17.         IPEndPoint ipEp = new IPEndPoint(ipAdr , 1234);  
  18.         Socket serverScoket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);  
  19.         serverScoket.Bind (ipEp);  
  20.         serverScoket.Listen(20);  
  21.         while(true)  
  22.         {  
  23.             Socket client = serverScoket.Accept();  
  24.             byte[] request = new byte[512];  
  25.             int bytesRead = client.Receive(request);  
  26.             string input = Encoding.UTF8.GetString(request,0,bytesRead);  
  27.             print("server request:"+input);  
  28.             string output = "连接服务器成功~~~~";  
  29.             byte[] concent = Encoding.UTF8.GetBytes(output);  
  30.             client.Send(concent);  
  31.             client.Shutdown(SocketShutdown.Both);  
  32.             client.Close();  
  33.         }  
  34.     }  
  35. }  


 

 

Client.cs:

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Text;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.IO;  
  7.   
  8.   
  9. public class Client : MonoBehaviour {  
  10.   
  11.     void Start () {  
  12.         ConncetServer();  
  13.     }  
  14.   
  15.     void ConncetServer()  
  16.     {  
  17.         IPAddress ipAdr = IPAddress.Parse("10.56.03.32");  
  18.         IPEndPoint ipEp = new IPEndPoint(ipAdr , 1234);  
  19.         Socket clientScoket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);  
  20.         clientScoket.Connect(ipEp);  
  21.         string output = "客户端请求连接~~~";  
  22.         byte[] concent = Encoding.UTF8.GetBytes(output);          
  23.             clientScoket.Send(concent);  
  24.         byte[] response = new byte[1024];  
  25.         int bytesRead = clientScoket.Receive(response);  
  26.         string input = Encoding.UTF8.GetString(response,0,bytesRead);  
  27.         print("Client request:"+input);  
  28.         clientScoket.Shutdown(SocketShutdown.Both);  
  29.         clientScoket.Close();  
  30.     }  
  31. }  


服务端:

1).用Socket()获得一个Socket描述

2).用Bind()j将Socket绑定到一个网络地址(一般都是本机的IP地址)

3).用Listen()开始在某个端口监听

4).Accept()等待客户连接,如果客户端调用Connect()函数连接服务器时Accept()会获得该客户端(Socket)。

5).Receive()接收数据

6).Send()发送数据

 

 

客户端:

1).用Socket()获取一个Socket描述

2).Connect()连接到远程主机

3).Send()发送数据

4).Receive()接收数据

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多