|  | 
 
As many who read this article will already know, I'm the creator of the much debated Jupiter Engine in the showcase of this webpage. 
 This command only has one argument. It needs to know which port the server is going to use to listen for messages. For now, just type in or copy this: This function, as you can see, creates a server on any given port and then performs some errorchecking to see if the server was started properly. To make this function work though, you'll have to make a global var somewhere in your program so that the entire program can access the server that the function creates. So just stick this at the top of your program: Once you're done with that, you need to initialize function, so that the server will be created: 7000 is a good port, because it is rarely used by other programs. You can in theory just type in any name though, and the server will still work as planned. Now I will show you the mainloop of the server: This needs some explanation. The first line of the mainloop creates a new TCP stream. The server uses this stream to listen for messages. This stream is connected to the port that you you specified as an argument for the CreateServer function, and every message from other clients will be stored in this stream. The first if check of the mainloop obviously creates a new playertype if a new client connects to the stream that we've created. It also stores a copy of the stream that we've created in the stream field of the player type so that we can send messages to each player individually and to every player at the same time. I will show you the player type later. The for loop should need no explanation. The ReadAvail command should, though. It checks if there is something to read in a stream, and it takes a valid stream as an argument. As you can see, we've supplied it with one. For simplicity's sake, all we're doing for now if there is a message in one of the playertype's streams, is to print out the message and then send a message back to the client who sent the message. Now it's time to show you the actual player type! I usually store all my types in a separate file, but you can do what you want to. Anyway, here is the code: For now, it's not really huge, but it contains some important basic fields. As you can see from the servercode, all we are actually utilizing for now is the stream field, but the second part of this tutorial will show you how to utilize the other fields. If you choose to have this type stored along with your server, make sure that you stick it in your code BEFORE the mainloop! Now it's finally time to cover the clientcode:) To create a client, you must use this command: It takes two parameters. The first parameter is a string that must contain the adress of the machine to connect to. For now, all we will use is "127.0.0.1", which indicates that the client should connect to your own PC. When you are going to launch this game though, you have to remember to change that parameter so that it contains the adress to your server. You can find your real IP adress at: This creates a client connection and performs error checking;) DO check though, that the port number matches the one in your servercode, in case you changed it there. Here's the last bit of clientcode that I will show you in this part of the tutorial: This code first uses the WriteString command to send a message to the server via the client that we created previously. Then it checks if there is anything to read in the clientconnection, and prints out whatever there is to read if there is anything:D Ok, now we have created a nice framework on which to build ourselves a beatiful WoW killer:) To test this statement, you can compile two clients if you'd like, and run them. As you will see, the clients will both recieve a message each, and the server will recieve two messages, one from each client. What we have accomplished in this part: - Creating a system through which we can send messages from the server to a specific client, or send messages to every client. - Creating a system through which we can send messages to the server from the client. What we will accomplish in the next part of the tutorial: - Modifying the server so that we can utilize the id and name fields of the playertype to reckognize a specific player by his id, and keep track of each player's nickname in the server. For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |