I've a problem with my server in tcp/ip, because after received one message doesn't work. I don't know if use a while true, and how to put it. Any suggestion?
Thank you
my method()
sdk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sdk.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8052));
sdk.Listen(10);
Socket accepted = sdk.Accept();
Buffer = new byte[accepted.SendBufferSize];
int bytesRead = accepted.Receive(Buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
}
Debug.Log("\t Server" + "\n");
string stradata = Encoding.ASCII.GetString(formatted);
Debug.Log("-->" + "" + stradata + "\n\n");
testo = stradata;
//sdk.Close(); tried to uncomment
//accepted.Close(); tried to uncomment
You need to read from the socket continually, until you have read all the data that you want
sdk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sdk.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8052));
sdk.Listen(10);
Socket accepted = sdk.Accept();
Buffer = new byte[accepted.SendBufferSize];
while (true)
{
// begin reading continually from the socket
// Socket::Receive() will block until it gets some data
// Exception handling required here for when client closes connection
int bytesRead = accepted.Receive(Buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
}
Debug.Log("\t Server" + "\n");
string stradata = Encoding.ASCII.GetString(formatted);
Debug.Log("-->" + "" + stradata + "\n\n");
// Need to add some exit logic here eg,
if(stradata.Contains("EXIT"))
break;
}
sdk.Close();
accepted.Close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.