Andy02
FPS Strategist
LEVEL 1
500 XP
Introduction:
This tutorial is the second in my Java Network Programming using KryoNet series in which we are creating a test server.
Previous:
In the previous tutorial we downloaded the KryoNet files and set up our projects.
Test Server:
So first, create a new file called Main (can be whatever you like) and add the following imports...
Next we are going to create the basic Java Application main method to create a new instance of the current class once the application is ran...
Now we need the constructor. This is where we will create the test server. First we create a new Server object named "server"...
Once it is created, we start the server...
Then finally we bind it to the ports. The first argument input is the TCP port, while the second is the UDP port. The recommended ports for KryoNet are as follows...
We surround it with try and catch statements for two reasons;
1 - It's required by Java in case of an IOException.
2 - We can create custom handles to deal with the binding problem.
Testing:
Now try to run the program, you should receive the following message in the console...
00:00 INFO: [kryonet] Server opened.
This tutorial is the second in my Java Network Programming using KryoNet series in which we are creating a test server.
Previous:
In the previous tutorial we downloaded the KryoNet files and set up our projects.
Test Server:
So first, create a new file called Main (can be whatever you like) and add the following imports...
- import
java.io.IOException
;
- import
com.esotericsoftware.kryonet.Server
;
Next we are going to create the basic Java Application main method to create a new instance of the current class once the application is ran...
- public
class
Main {
- public
static
void
main(
String
args[
]
)
{
- new
Main(
)
;
- }
- }
Now we need the constructor. This is where we will create the test server. First we create a new Server object named "server"...
- public
Main(
)
{
- Server server =
new
Server(
)
;
- }
Once it is created, we start the server...
- public
Main(
)
{
- Server server =
new
Server(
)
;
- server.start
(
)
;
- }
Then finally we bind it to the ports. The first argument input is the TCP port, while the second is the UDP port. The recommended ports for KryoNet are as follows...
- public
Main(
)
{
- Server server =
new
Server(
)
;
- server.start
(
)
;
- try
{
- server.bind
(
54555
, 54777
)
;
- }
catch
(
IOException
e)
{
- e.printStackTrace
(
)
;
- }
- }
We surround it with try and catch statements for two reasons;
1 - It's required by Java in case of an IOException.
2 - We can create custom handles to deal with the binding problem.
Testing:
Now try to run the program, you should receive the following message in the console...
00:00 INFO: [kryonet] Server opened.