Home > other >  Problems about unity socket to receive data
Problems about unity socket to receive data

Time:11-04

Excuse me everybody code on receipt of the great god, don't know why I can only receive a server returned data can not accept the second time!!!!!!!!!! The along while also didn't understand, the HTML code is as follows, registered function;

Public UIInput username;
Public UIInput password;

Public class StateObject
{
//the Client socket.
Public Socket workSocket=null;
//the Size of the receive buffer.
Public const int BufferSize=1024;
//the Receive buffer.
Public byte [] buffer=new byte [BufferSize];
}

Void the Start () {

Try
{
IPAddress IP=IPAddress. Parse (" 127.0.0.1 ");
The EndPoint EndPoint=new IPEndPoint (IP, 5222);
Clientsocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
Clientsocket. BeginConnect (the endPoint, new AsyncCallback (connectCallback), clientsocket);
ConnectDone. WaitOne ();
The receive (clientsocket);
}
The catch (Exception e)
{
The Debug Log (" shibai ");
return;
}
Public void connectCallback IAsyncResult (ar)
{
Try
{
The Socket backSocket=(Socket) ar. AsyncState;
BackSocket. EndConnect (ar);
ConnectDone. Set ();
The Debug Log (" on connected ");
}
The catch (Exception e)
{
Console. WriteLine (e. oString ());
}
}
Void the Update () {
}
Void OnApplicationQuit ()
{
Clientsocket. Close ();
}
Public void lick ()//send
{
Logintesttest. Builder subrequest=Logintesttest. CreateBuilder ();
Subrequest. SetUsername (username. Value);
Subrequest. SetPassword (password. The value);
Subrequest. SetHeader (103);
Logintesttest send=subrequest. The Build ();
Byte [] buf=send. ToByteArray ();
Byte [] data=https://bbs.csdn.net/topics/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. The value;

}
Public void the receive (Socket Socket)//accept
{
Try
{
StateObject so=new StateObject ();
So. WorkSocket=socket;
//read the data for the first time the total length of the
Socket. BeginReceive (so. The buffer, 0, prefixSize, 0, new AsyncCallback (receivedCallback), so);
The Debug Log (" success3 first re ");
//socket. BeginAccept (new AsyncCallback (receivedCallback), so);
//test: data within 1024, one-time read out
//socket. BeginReceive (so buffer, 0, StateObject. BufferSize, 0, new AsyncCallback (simpleCallback), so);
}
The catch (Exception e)
{
Console. WriteLine (e. oString ());
}
}
Public MemoryStream receiveData=https://bbs.csdn.net/topics/new MemoryStream ();
Private bool isPresix=true;
Public int curPrefix=0; The total length//need to read data
Public void receivedCallback IAsyncResult (ar)
{
Try
{
StateObject so=(StateObject) ar. AsyncState;
The Socket client=so. WorkSocket;
Int readSize=client. EndReceive (ar);//end read
ReceiveData. Write (so. The buffer, 0, readSize);
ReceiveData. Position=0;
If ((int) receiveData. Length & gt;=prefixSize & amp; & 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 & lt; (long) curPrefix)
{
ReceiveData. Position=receiveData. Length;
}
The else
{
ReceiveData. Position=prefixSize;
//read the data
Byte [] datas=new byte [curPrefix];
ReceiveData. Read (datas, 0, datas. Length);
String STR=Encoding. UTF8. Get String (datas);
The Debug Log (" the finally the message is: "+ STR);
Reciecedmessage. Text=(" the message: "+ STR);
Getreceived. Text=reciecedmessage. Text;
}
//repeat read
Client. BeginReceive (so. The buffer, 0, StateObject. BufferSize, 0, new AsyncCallback (receivedCallback), so);

The Debug Log (" success finally re ");
}
The catch (Exception e)
{
Console. WriteLine (e. oString ());
}
}

}
Is roughly like this, there is no intercept all, start the inside began to accept data asynchronously, receivecallback performed in the last line call beginreceive again, but just can't receive the second time, don't know why, server-side Java write, later return a data connection, can be received, can show on the label, don't get registered successfully returns a data, connect successfully returns the server modification and deletion of data
, click on the button to get back to successful data received!!!!!! Exactly why!!!!!! Is my client is there a problem? How to change!!!!!!!!!! Counsel, genuflect is begged light spray!!!!!!

CodePudding user response:

Don't sink sink!!!!! Wait for a great god!!!!!!!!!!
  • Related