Hi,
I am trying to develop a car simulator using Unity by playing around with the Roll-a-ball example for exploration. Currently, I am experiencing difficulty sending message to a server end to received a self-defined struct with Unity LLAPIs. I am not able to receive anything from my client. To make things worse, the client will flag “wrong connection” once I start running my server.
For this project, I am using QT for my server while the client end is through Unity.
On the server end (Unity):
// Initializing the Transport Layer with no arguments (default settings)
NetworkTransport.Init();
// Configuring
connectionConfig = new ConnectionConfig();
reliableChannelId = connectionConfig.AddChannel (QosType.Reliable);
// Topology
topology = new HostTopology (connectionConfig, 10);
// Socket creation
socketId = NetworkTransport.AddHost(topology, 10000);
// Connection id creation
byte error;
connectionId = NetworkTransport.Connect(socketId, "127.0.0.1", 10001, 0, out error);
On the client side (QT):
void MainWindow::Start()
{
listener = new QUdpSocket(this);
if(listener->bind(QHostAddress::LocalHost, 10000) == true)
{
qDebug() << "UDP Connected";
connect(listener, SIGNAL(readyRead()), this, SLOT(DisplayData()));
}
else
{
qDebug() << "Error in UDP Connecting";
}
}
void MainWindow::smile:isplayData()
{
// when data comes in
QByteArray buffer;
buffer.resize(listener->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
if(listener->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort) != -1)
{
Pose_t pose;
pose.deserialize(buffer);
QString valueAsString = QString::number(pose.d_x);
ui->textBrowser->setText(valueAsString);
valueAsString = QString::number(pose.d_y);
ui->textBrowser_2->setText(valueAsString);
qDebug() << "data in ";
// ui->textBrowser->setText(QTextCodec::codecForMib(106)->toUnicode(buffer));
}
}
Thanks.
Some useful information here: NetworkTransport message signature and encoding - Unity Engine - Unity Discussions
Ultimately, until the LLAPI of unet is released as a standalone library, mixing and matching the tech used on the client and server end is a bad idea, as explained in my post above where the OP was looking to use NodeJS as his server-side.
Hopefully this helps. Good luck!
Hi,
So in the case if I wished to build a simulator in unity that works in a linux environment and uses UDP to communicate, what is the best approach I can use?
Well, one of the reasons we use unity is because of it’s portability (probably even the main reason…). One of the platforms that it supports is linux. I talk a little about my recommended way of doing it in the link in my previous response, but to go into more detail:
- have one specific “scene” in your project which will basically function as your server-side application, built in unity
- in file > build settings, select linux x86_64 as your build platform, select the server scene, and make sure to enable headless mode. Click “build” and this will build your linux executable file
- Move the executable file, as well as the *_Data folder (should be in the same location as your executable) over to your linux server and run it using a command like so: ./server_application.x86_64 -batchmode -nographics
- The “./server_application.x86_64” references the executable file, so you would run that from the directory where you place it. It simply tells linux to run it. “-batchmode” runs it in headless mode (no rendering/audio, massively reducing resource usage). I’m not 100% sure “-nographics” is necessary, as this should do the same as -batchmode but i’d stick it in anyway!
So the beauty of this is you can work on your client and server side in the same project and environment, using the same classes and so on.
Hopefully this helps, let me know if you get stuck.
Unity builds for Linux also have a special “headless” build option available. I’d recommend doing that over -batchmode -nographics, which I think can still cause issues if you don’t have all the Linux GUI packages installed.
Thanks for the prompt replies.
With regard on the server side of application, may I know if I resort to use C# UDP APIs (since I am unable to get the UNET to work) and I select the build option as linux x86_64, will this application able to be run as a server in linux?
Hmm the advice that I suggested above was in relation to using unet. It would be good to know why you are unable to use unet because I definitely recommend using it (if you are building multiplayer games in unity). So if you can let us know what issues you’re having we could try and help.
To answer your question though, yeah. If you’re using c# then you’re very likely using .net. Unity (I think?) expects you to use .net 3.5 and lower, which has support for UDP socketing.
Currently, I tried to use the LLAPI to send and receive messages within unity, it is able to capture these messages. But when I send messages through unity only and run my software written in QT, the sending in unity will failed with an error on wrong connection.
In both cases, I tried to bind the socket to the same port n ip. Not sure if it is the one causing the problem.
Hmm… yeah okay so I can go back to suggesting my original suggestion: use UNET on both sides, by creating a server and client application in unity. Your issue will have been because your QT application will not have been correctly mimicing the server-side of UNET. There’s quite a bit that goes on under-the-hood in unet when you connect to the server, such as making sure the connection config details are the same, and setting up your unique id etc.
If you absolutely want to resort to a c# UDP api (rather than unet), feel free to, but just make sure that you use the same api on both your client and server side, or apis that are compatible with each other. Otherwise, stick to my original suggestion and just use UNET on both sides.
Hopefully this helps. Good luck!