unity用socket(TcpLitsener)来操作sqlserver

unity用socket(TcpLitsener)来操作sqlserver

最近几天都在搞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.服务端连接数据库。

连接数据库:

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

 

 

  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.     }  
SqlDataReader Select(string content){
		
		SqlCommand cmd=new SqlCommand(content,con);		
		
	    SqlDataReader reader = cmd.ExecuteReader();
			
		return reader;
		
	}

修改,增加:

 

 

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

	}


 

4.客户端和服务端同学

服务端:

 

 

  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. }  
using System.Net;
using System;

public class Sender{
	
	NetworkStream stream;
	
	/*服务器端开启监听*/
	public TcpListener open(){
		
		TcpListener server=null;
		
		string ip="192.168.1.103";
		
	    int iport=5561;
		
		IPAddress address=IPAddress.Parse(ip);
		
	    server=new TcpListener(address,iport);
		
		server.Start();
		
		return server;
		
	}
	/*添加正在向服务端发送请求的客户端*/
	public TcpClient addClient(TcpListener server){
		
		TcpClient client=null;
		
		if(server.Pending()){
			
		    client = server.AcceptTcpClient();	
			
		}
		return client;
	}
	/*向客户端发送消息*/
	public void send(ArrayList clients,string data) {	
		
		byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
		
		for(int i=0;i<clients.Count;i++){

			stream = (clients[i] as TcpClient).GetStream();

			stream.Write(msg,0,msg.Length);	

		}	
       
	}
	/*接收客户端的消息*/
	public string receive (ArrayList clients) {	
		
		Byte[] bytes = new Byte[4096];
		
		string data="";
		
		int i=0;
		
		for(int j=0;j<clients.Count;j++){
			
			if((clients[j] as TcpClient).Available!=0){
				
				stream = (clients[j] as TcpClient).GetStream();
				
				if((i = stream.Read(bytes,0, bytes.Length))!=0) 
		        {   
				    data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
			    }	
				
				if(data.Contains("Exit")){
					
					(clients[j] as TcpClient).Close();
					
				    clients.RemoveAt(j);	
				}
				
			}
		}
		
		return data;
	}
	
}

客户端:

 

 

  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. }  
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.IO;
using System;

public class Receiver{

	/*连接服务器端*/
	public void connect(TcpClient client) 
	{
		string ip="192.168.1.103";
		
	    int iport=5561;
		
        client.Connect(ip, iport);				
	}
	
	/*接收服务器端消息*/
	public string receive (TcpClient client) 
	{		
		string data="";
		
		int i=0;
		
	    Byte[] bytes = new Byte[4096];
		
	    NetworkStream stream = client.GetStream();
		
		if(client.Available!=0){

			
			if((i = stream.Read(bytes,0, bytes.Length))!=0) 
	        {   
				data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
			}
		
		}
        
		return data;
		
	}
	/*向服务器端发送消息*/
	public void send(TcpClient client,string data)
	{
		NetworkStream stream = client.GetStream();
		
		byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
		
		stream.Write(msg,0,msg.Length);
	}
}



 

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

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

 

 

posted @ 2013-03-23 14:35  小薇林  阅读(562)  评论(0编辑  收藏  举报