socket只能收到一次数据,第二次就没有反应了。求指导!!
namespace test3{
using System;
using System .Net .Sockets ;
using System .Net ;
using System .Text ;
using System .Threading ;
using System.Collections;
using System .IO ;
using UnityEngine;
public class Socketfz
{
public static Socket soc =null ;
private static Thread nthread = null;
private static Thread thread = null;
//private static byte[] buffer = new byte[1024];
//private static byte[] buf = new byte[1024];
public static string data ;
public static string cd;
public static string dingwei;
public static string speed;
public static string angle;
public static string fx;
public Socketfz ()
{
}
public void SocketConnection(int Lport){
soc = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint (IPAddress.Any, Lport);
soc.Bind (ipep);
soc.Listen (10);
thread = new Thread (new ThreadStart (ListenC));
thread .Start ();
}
static void ListenC(){
while(true){
try{
Socket s = soc.Accept();//监听
s.Send(Encoding.ASCII.GetBytes("success!"));
nthread = new Thread(Redata);
nthread.Start(s);
}catch(Exception e ){
MonoBehaviour .print (e.ToString());
}
}
}
static void Redata(object socket){
Socket socket1 = (Socket)socket;
byte[] b2 = new byte[19];
int len = 0;
while (true) {
try{
int r = socket1.Receive( b2 , len , b2.Length - len ,0);
if(r == 0){
break ;
}
len += r;
if(len < 19)
continue ;
if(b2[0]==0xFF & b2[1]== 0xFF& b2[2]==0xFF){//判断数据头
dingwei = b2[5].ToString("X2") + b2[4].ToString("X2") + b2[3].ToString("X2") ;
speed = b2[6].ToString("X2");
angle = b2[8].ToString("X2")+ b2[7].ToString("X2");
fx = b2[9].ToString("X2");
MonoBehaviour .print (dingwei +" " +speed +" " +angle +" "+fx);
}
}catch (Exception e){
MonoBehaviour .print (e.ToString());
//sw.Close();
//sw.Dispose();
//socket1 .Shutdown(SocketShutdown.Both);
//socket1 .Close();
break;
}
}
}
}
}
while (true) {
try{
int r = socket1.Receive( b2 , 0 , b2.Length ,0);
if(r == 0){
continue ;
}
//len += r;
//if(len < 19)
//continue ;
if(b2[0]==0xFF & b2[1]== 0xFF& b2[2]==0xFF){
dingwei = b2[5].ToString("X2") + b2[4].ToString("X2") + b2[3].ToString("X2") ;
speed = b2[6].ToString("X2");
angle = b2[8].ToString("X2")+ b2[7].ToString("X2");
fx = b2[9].ToString("X2");
MonoBehaviour .print (dingwei +" " +speed +" " +angle +" "+fx);
}
}catch (Exception e){
MonoBehaviour .print (e.ToString());
//sw.Close();
//sw.Dispose();
socket1 .Shutdown(SocketShutdown.Both);
socket1 .Close();
break;
}