unity用socket(TcpLitsener)来操作sqlserver

来源:互联网 发布:科比2012赛季数据 编辑:程序博客网 时间:2024/05/19 23:16

unity用socket(TcpLitsener)来操作sqlserver

分类: unity3D初步篇 482人阅读 评论(3) 收藏 举报

好久没写博客了,我也发现我堕落了,天天看连续剧。还有就是我想在北京找份unity3d程序员的工作,随时可以去上班,有合适的话,麻烦大侠们给我介绍介绍哈。我的QQ:397319689(有点笨),呵呵~~~

最近几天都在搞unity操作Sql server,也用到了底层通信的东西,局域网测试是通过了的,要谢谢我龙哥(灰太龙),他太厉害了,呵呵~~~源代码我忘记拷贝过来了,所以大概写了一下,我发现OnGUI里面的UI在服务端不大好弄啊,我这里没有用到多线程,用到了数组。

0.看看思路:



1.安装好sqlserver还需要一些dll文件,unity本身里面就有的,在路径E:\Unity\Editor\Data\Mono\lib\mono\2.0下有I18N.dll,I18N.CJK.dll和I18N.West.dll,以及System.Data.dll。

我这里安装的是sqlserver2005开发版。http://www.cnblogs.com/icewee/articles/2019783.html


2.那就是建表咯,这个就不多说了啊。


3.服务端连接数据库。

连接数据库:

[html] view plaincopyprint?
  1. con = new SqlConnection("Data Source=WANGXF;User ID=sa;Password=sa;database=data1");  
  2.                   
  3. con.Open();  
查找读取数据库:

[html] view plaincopyprint?
  1. SqlDataReader Select(string content){  
  2.           
  3.         SqlCommand cmd=new SqlCommand(content,con);       
  4.           
  5.         SqlDataReader reader = cmd.ExecuteReader();  
  6.               
  7.         return reader;  
  8.           
  9.     }  

修改,增加:

[html] view plaincopyprint?
  1. void Reset(string content){       
  2.           
  3.         SqlCommand cmd =new SqlCommand(content,con);  
  4.           
  5.         cmd.ExecuteNonQuery();  
  6.   
  7.     }  


4.客户端和服务端同学

服务端:


[html] view plaincopyprint?
  1. using System.Net;  
  2. using System;  
  3.   
  4. public class Sender{  
  5.       
  6.     NetworkStream stream;  
  7.       
  8.     /*服务器端开启监听*/  
  9.     public TcpListener open(){  
  10.           
  11.         TcpListener server=null;  
  12.           
  13.         string ip="192.168.1.103";  
  14.           
  15.         int iport=5561;  
  16.           
  17.         IPAddress address=IPAddress.Parse(ip);  
  18.           
  19.         server=new TcpListener(address,iport);  
  20.           
  21.         server.Start();  
  22.           
  23.         return server;  
  24.           
  25.     }  
  26.     /*添加正在向服务端发送请求的客户端*/  
  27.     public TcpClient addClient(TcpListener server){  
  28.           
  29.         TcpClient client=null;  
  30.           
  31.         if(server.Pending()){  
  32.               
  33.             client = server.AcceptTcpClient();    
  34.               
  35.         }  
  36.         return client;  
  37.     }  
  38.     /*向客户端发送消息*/  
  39.     public void send(ArrayList clients,string data) {     
  40.           
  41.         byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);  
  42.           
  43.         for(int i=0;i<clients.Count;i++){  
  44.   
  45.             stream = (clients[i] as TcpClient).GetStream();  
  46.   
  47.             stream.Write(msg,0,msg.Length);   
  48.   
  49.         }     
  50.          
  51.     }  
  52.     /*接收客户端的消息*/  
  53.     public string receive (ArrayList clients) {   
  54.           
  55.         Byte[] bytes = new Byte[4096];  
  56.           
  57.         string data="";  
  58.           
  59.         int i=0;  
  60.           
  61.         for(int j=0;j<clients.Count;j++){  
  62.               
  63.             if((clients[j] as TcpClient).Available!=0){  
  64.                   
  65.                 stream = (clients[j] as TcpClient).GetStream();  
  66.                   
  67.                 if((i = stream.Read(bytes,0, bytes.Length))!=0)   
  68.                 {     
  69.                     data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);  
  70.                 }     
  71.                   
  72.                 if(data.Contains("Exit")){  
  73.                       
  74.                     (clients[j] as TcpClient).Close();  
  75.                       
  76.                     clients.RemoveAt(j);      
  77.                 }  
  78.                   
  79.             }  
  80.         }  
  81.           
  82.         return data;  
  83.     }  
  84.       
  85. }  

客户端:

[html] view plaincopyprint?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Net.Sockets;  
  4. using System.Net;  
  5. using System.Text;  
  6. using System.IO;  
  7. using System;  
  8.   
  9. public class Receiver{  
  10.   
  11.     /*连接服务器端*/  
  12.     public void connect(TcpClient client)   
  13.     {  
  14.         string ip="192.168.1.103";  
  15.           
  16.         int iport=5561;  
  17.           
  18.         client.Connect(ip, iport);                
  19.     }  
  20.       
  21.     /*接收服务器端消息*/  
  22.     public string receive (TcpClient client)   
  23.     {         
  24.         string data="";  
  25.           
  26.         int i=0;  
  27.           
  28.         Byte[] bytes = new Byte[4096];  
  29.           
  30.         NetworkStream stream = client.GetStream();  
  31.           
  32.         if(client.Available!=0){  
  33.   
  34.               
  35.             if((i = stream.Read(bytes,0, bytes.Length))!=0)   
  36.             {     
  37.                 data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);  
  38.             }  
  39.           
  40.         }  
  41.           
  42.         return data;  
  43.           
  44.     }  
  45.     /*向服务器端发送消息*/  
  46.     public void send(TcpClient client,string data)  
  47.     {  
  48.         NetworkStream stream = client.GetStream();  
  49.           
  50.         byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);  
  51.           
  52.         stream.Write(msg,0,msg.Length);  
  53.     }  
  54. }  


以上只是一些主要的内容,我把工程放到我的资源里面了。http://download.csdn.net/detail/dlnuchunge/4734166

以上内容都是我乱盖的,不足的地方往大家见谅,多多指点~~~~

原创粉丝点击