請問一下各位大神代碼就收據的問題,不知道為什么 只能接收一次服務端回傳的資料 無法接受第二次!!!!整了半天也沒弄懂,代碼如下,注冊功能;
public UIInput username;
public UIInput password;
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
}
void Start() {
try
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
EndPoint endPoint = new IPEndPoint(ip, 5222);
clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientsocket.BeginConnect(endPoint, new AsyncCallback(connectCallback), clientsocket);
connectDone.WaitOne();
receive(clientsocket);
}
catch (Exception e)
{
Debug.Log("shibai");
return;
}
public void connectCallback(IAsyncResult ar)
{
try
{
Socket backSocket = (Socket)ar.AsyncState;
backSocket.EndConnect(ar);
connectDone.Set();
Debug.Log("on connected");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
void Update () {
}
void OnApplicationQuit()
{
clientsocket.Close();
}
public void lick() //發送
{
Logintesttest.Builder subrequest = Logintesttest.CreateBuilder();
subrequest.SetUsername(username.value);
subrequest.SetPassword(password.value);
subrequest.SetHeader(103);
Logintesttest send = subrequest.Build();
byte[] buf = send.ToByteArray();
byte[] data = new byte[4 + buf.Length];
IntToBytes(buf.Length).CopyTo(data, 0);
buf.CopyTo(data, 4);
//Thread.Sleep(1000);
clientsocket.Send(data);
string user = username.value;
string pass = password.value;
}
public void receive(Socket socket)//接受
{
try
{
StateObject so = new StateObject();
so.workSocket = socket;
//第一次讀取資料的總長度
socket.BeginReceive(so.buffer, 0, prefixSize, 0, new AsyncCallback(receivedCallback), so);
Debug.Log("success3 first re");
//socket.BeginAccept(new AsyncCallback(receivedCallback), so);
//測驗用:資料在1024以內的資料,一次性讀取出來
//socket.BeginReceive(so.buffer,0,StateObject.BufferSize,0,new AsyncCallback(simpleCallback),so);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public MemoryStream receiveData = new MemoryStream();
private bool isPresix = true;
public int curPrefix = 0;//需要讀取的資料總長度
public void receivedCallback(IAsyncResult ar)
{
try
{
StateObject so = (StateObject)ar.AsyncState;
Socket client = so.workSocket;
int readSize = client.EndReceive(ar);//結束讀
receiveData.Write(so.buffer, 0, readSize);
receiveData.Position = 0;
if ((int)receiveData.Length >= prefixSize && isPresix)
{
byte[] presixBytes = new byte[prefixSize];
receiveData.Read(presixBytes, 0, prefixSize);
Array.Reverse(presixBytes);
curPrefix = BitConverter.ToInt32(presixBytes, 0);
isPresix = false;
}
if (receiveData.Length - (long)prefixSize < (long)curPrefix)
{
receiveData.Position = receiveData.Length;
}
else
{
receiveData.Position = prefixSize;
//讀取資料
byte[] datas = new byte[curPrefix];
receiveData.Read(datas, 0, datas.Length);
String str = Encoding.UTF8.GetString(datas);
Debug.Log("the finally message is : " + str);
reciecedmessage.text = ("message: " + str);
getreceived.text = reciecedmessage.text;
}
//重復讀取
client.BeginReceive(so.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(receivedCallback), so);
Debug.Log("success finally re");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
大致是這個樣子,沒有截取全,start里面開始異步接受資料,receivecallback里面執行完最后一行重新呼叫beginreceive,不過就是不能接收第二次,不知道為什么,服務端java寫的,連接上了以后回傳一條資料,能接到,label上能顯示,注冊成功回傳一條資料接不到,服務端修改洗掉連接成功回傳的資料以后
,點擊按鈕能接到回傳成功的資料!!!到底是為什么!!!是我的客戶端有問題嗎?怎么改!!!!跪求指教,輕噴!!!
uj5u.com熱心網友回復:
別沉別沉!!坐等大神!!!!轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/148065.html
標籤:網絡通信
