Network Game - Sending Over a network question

I am going to make my first network game.
I have read tutorials on networking and have designed an echoserver.
I am however not sure how to pass player moves over the network. I know that it will work the same way as strings
but how do I pass other players new positions so I can then broadcast changes to all other clients.
Thanks in advance for any help
Marc

you could use serialization, but that can be too big an overhead when you have a lot of players. I would use a simple messaging system, where you send strings over the network in a specific format that you can create on the sending side and unpack on the receiving side. Something like:
ID;PARAMS
where ID is a messageId identifying the specific type of message you are sending, and PARAMS is a list of parameters that depend on the message type. For example a movement message could be:
1;R;40
This would then mean that the player moved 40 "units" to the Right. A very simple example, of course adapt to your game's situation.
The advantage of a system like this is that you can string together multiple messages and send them at once. At the receiving end you can just keep parsing messages from the string until there is no more data.
If you are really tight on bandwidth, then you'll have to start packing bits in binary data in stead. I would go for the messaging system first though, it is the easiest to get working.

Similar Messages

  • No JMS over the network

    hello,
    Im learning JMS and trying the examples from the tutorial. Both my SDK's are version 1.4.
    At home, I have a Linux machine directly connected to a win-XP machine. Im trying to connect the two machines as in the example "Running JMS Client Programs on Multiple Systems" I do exactly what the turorial states. I check, as recommended, that the JmsFactory does exist.
    What happens is somewhat dissapointing: when i issue the command "run SimpleProducer MyQueue queue 3", instead of messages get send over the network they end up in the own local queue at the side of the SimpleProducer. Both on the linux as on the win machine the same happens. I used both the IP-address and the netbios name of the remote machine in "ant add-remote-factory -Dsys=sys-name". The effect is the same.
    Anyone an idea what is wrong??
    Thank i.a.

    hi!
    i went to bed frustrated, turned on my computer and now this comes up:
    I try to lookup the connection factory with this code:
    try {
    connectionFactory = (ConnectionFactory)
    jndiContext.lookup("EarthQueueConnectionFactory");
    if (destType.equals("queue")) {
    dest = (Queue) jndiContext.lookup(destName);
    } else if (destType.equals("topic")) {
    dest = (Topic) jndiContext.lookup(destName);
    } else {
    throw new Exception("Invalid destination type" +
    "; must be queue or topic");
    } catch (Exception e) {
    System.out.println("JNDI API lookup failed: " +
    e.toString());
    System.exit(1);
    This is the error it returns:
    C:\j2sdkee1.4\doc\tutorial\examples\jms\simple>run SimpleProducer MyQueue queue 1
    Destination name is MyQueue, type is queue
    18-feb-2003 11:35:52 com.sun.jms.ConnectionFactoryImpl establishRemoteReferences
    SEVERE: ConnectionFactoryImpl: Failed to lookup or connect to JMS service because: org.omg.CORBA.OBJECT_N
    OT_EXIST: vmcid: SUN minor code: 202 completed: No
    18-feb-2003 11:35:52 com.sun.jms.ConnectionFactoryImpl establishRemoteReferences
    SEVERE: ConnectionFactoryImpl: Failed to lookup or connect to JMS service because: org.omg.CORBA.OBJECT_N
    OT_EXIST: vmcid: SUN minor code: 202 completed: No
    Exception in thread "main" java.lang.NullPointerException
    at com.sun.jms.client.ConnectionImpl.invokeRemoteCreateConnection(ConnectionImpl.java:161)
    at com.sun.jms.ConnectionFactoryImpl.createConnection(ConnectionFactoryImpl.java:314)
    at com.sun.jms.ConnectionFactoryImpl.createXAConnection(ConnectionFactoryImpl.java:468)
    at com.sun.jms.ConnectionFactoryImpl.createXAConnection(ConnectionFactoryImpl.java:444)
    at com.sun.jms.connector.ra.JMSConnectionFactoryAdapter.createConnection(JMSConnectionFactoryAdap
    ter.java:129)
    at SimpleProducer.main(Unknown Source)
    MyQueue exists and my j2eeadmin -listJmsFactory looks like this:
    JmsFactory
    < Connector Resource : jms/TopicConnectionFactory, jmsra.rar, , , javax.jms.TopicConnectionFactory
    Properties: NONE >
    < Connector Resource : jms/QueueConnectionFactory, jmsra.rar, , , javax.jms.QueueConnectionFactory
    Properties: NONE >
    < Connector Resource : EarthQueueConnectionFactory, jmsra.rar, , , javax.jms.QueueConnectionFactory
    Properties: >
    I have really no clue what is wrong. Can someone help me please????

  • Network game communication algorithms... small question

    Just a (hopefully) quick question about getting input from the client side for passing information to a server. At the moment it is set up to take and send the input from the client once the return key is pressed, and send the whole line (with the system.in part). But obviously for a game to be played I am needing the client to send every individual key press from the client. I could try and use the read() method for the bufferedreader, but this returns an int, and I require a string so it can be checked in the server (as I am also going to have a server side representation of the game, as the project is to be investigating lag effects and different solutions to get around online game lag). Or could I use the read() method and use the returned int to find out from that key what was pressed and then change it to the key pressed from the int value returned, or is there a quicker way that doing this check?
    I think there was a method that takes a character array as a parameter and I could somehow set the size to be only 1, so it would send every individual key pressed by the client, then allowing me to send this and display it at the server side? Or am I off on the wrong foot entirely...
    This is the basic code of getting the actual input and then calling the methods which actually sort out writing to the streams elsewhere in the program:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    s = br.readLine();
    if (s.length() > 0)
    cc.sendMessage(s + "\n");
    Thanks for any help given,
    Cheers

    Hi,
    You could send the KeyEvent object created by the key press event using an ObjectOutputStream or you could just write its code (KeyEvent.getKeyCode()) using DataOutputStream.writeInt().
    If you need the distinction between a press and a release you could write presses as normal key codes and releases as negative key codes.
    In a game I'm making that uses UDP instead of TCP to communicate over the network, since UDP packets aren't guaranteed to get to their destination, whenever a key is pressed or released I send a whole array of integer keyCodes that show which keys are down. This way when a key is released, but the packet indicating the release doesn't get to the server, the server will find out eventually when the next key is pressed/released and a new 'key state' array is sent.

  • Send BufferedImage over the network

    I have already successfully setup a client/server connection. The issue, is when I send the BufferedImage over the network, it says:
    "Caused by: java.io.NotSerializableException: java.awt.image.BufferedImage"
    Can someone please tell me how to fix this?

    Like the exception say, BufferedImage is not serializable. You cannot serialize it. The problem will go away if you don't try to serialize it. Of course, that doesn't answer the question you really meant to ask... which I'll leave for somebody else.

  • Need to send object instances over the network

    I found no other way to implement a switch case to cast objects on both sides to solve my problem.
    Basically I need to send objects over a network protocol based on XML, the object is sent inside XML
    converted in base64, overall encoding of XML is utf-8.
    Let's suppose in my network there are 2 peers connected via socket.
    I have multiple instances of different types on both peers but I want to keep these instances
    synchronized. If something changes on side A, side B must receive that instance and replace
    it in the correct place (just one way, from A to B).
    When I receive such instance on B I want to cast it to it's proper instance
    of it's proper type and I am scratching my head on how could I implement this without some
    sort of unique ID table and switch case.
    If I had 1 instance per type could it be done easily?
    But I need to keep in synch many instances per type.
    Is there any dynamic casting that I can trigger based on some type/instanceID information
    I could send along the object?

    I found no other way to implement a switch case to cast objects on both sides to solve my problem.
    Basically I need to send objects over a network protocol based on XML, the object is sent inside XML
    converted in base64, overall encoding of XML is utf-8.
    Let's suppose in my network there are 2 peers connected via socket.
    I have multiple instances of different types on both peers but I want to keep these instances
    synchronized. If something changes on side A, side B must receive that instance and replace
    it in the correct place (just one way, from A to B).
    When I receive such instance on B I want to cast it to it's proper instance
    of it's proper type and I am scratching my head on how could I implement this without some
    sort of unique ID table and switch case.
    If I had 1 instance per type could it be done easily?
    But I need to keep in synch many instances per type.
    Is there any dynamic casting that I can trigger based on some type/instanceID information
    I could send along the object?

  • App imitates sending out a virus over the network

    The last time I ran the Classic App called "Farallon Ping" which came with my old Farallon NC I bought years ago was back in 2003 or 2004 and I was told by IS of the University I was attending not to run it again as the app makes it appear to be sending out viruses over the network. The app is useful as it tells me my IP address and the IP address of every computer in my domain and offers many other features some of which that are lacking in the built in OSX utilities.
    I am at a much larger University these days and I was wanting to run this app for the features but not sure if I should.
    Is this post appropriate for this group and if so what do you say?
    Thanks

    Know nothing about Farallon Ping. Would MacPing at http://74.125.93.104/translate_c?hl=en&sl=nl&u=http://dartware.com/downloads/leg acy.html do the same thing?
     Cheers, Tom

  • Send wav files over a network

    Hi folks,
    I need to send sound files over a network. The files I'm wanting to send are wav files. I kinda have the mechanism that sends a file, but I am not sure how to convert wav files into something that I can easily send. Do I need to convert them into bytes, serialize them...etc I don't know.
    Can anyone please point me in the right direction? An example would be very much appreciated.
    Thank you,
    E

    hi
    this is very simple
    convert voice to bytearray
    just create one server socket and client socket then connect a sockets and transmit
    download sample from
    http://javasolution.blogspot.com/2007/04/voice-chat-using-java.html

  • Sending text messages to pc's over the network

    OK, so I know the system administrators where I work can send me text messages over their network directly to my computer.  How do you do this over the Linksys wireless network?  (Directly - as in not using an IM program).  Thanks!

    There is this nifty little program you can use over a LAN... http://www.dynatalk.com/

  • Sending Connection Object over the Network using RMI

    Hi,
    How can a Connection object be sent over the network and run on another JVM. I need to hold connection object to execute processes one after other, that require Oracle connection without ever connecting again. I do not have J2EE container or webserver setup to hold connection/connectionpool,but need to run the process on command line. I am using RMI infrastructure to pass parameters/return values but connection object is not serializable and connot be marshalled and failing. Please explain, if there is another way using JDK 1.4
    Sudheer

    I think that what you want to do is connect to the database on the RMI server object, then use the server object from your remote clients to execute the processes you require to rrun.

  • Sending Connection object over the network

    Hi,
    How can a Connection object be sent over the network and run on another JVM. I need to hold connection object to execute processes one after other, that require Oracle connection without ever connecting again. I do not have J2EE container or webserver setup to hold connection/connectionpool,but need to run the process on command line. I am using RMI infrastructure to pass parameters/return values but connection object is not serializable and cannot be marshalled and failing. Please explain, if there is another way using JDK 1.4
    Sudheer

    I don't believe this is possible. A connection object has a number of associated structures on the operating system, which generally makes it impossible to move. From a fundamental networking level, you also cannot, in general, cause a connection to machine1 to start communicating with machine2-- that would introduce all manner of security problems.
    Why don't you want to just create another connection on the other machine?
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • Network Game - Adding more players

    Hey. i am in the middle of making a network game. I am have a problem with adding new players.
    I am not really sure the best way to do this but what I am doing this now is when a player is first created a
    string is sent to the server and returns with how many clients there are and then sets a client id based on that result.
    In the main class I have a thread running which moves the players that where created (new Players are stored in an arraylist). Whenever a
    player moves it sends its id and current position to the server and the server broadcasts it to all clients.
    So, when this thread runs it gets the id number that it broadcasted and if it is higher than the arraylist of stored players, then I create a new player,
    and add to the arraylist which is then drawn by the paint method.
    When i add a new player, I either add it from another computer or run the program again and connect to the server through port and ip address.
    Is this the correct way to do this because what is happening is that when a second players tried to join new players keep getting added. If any of this is not clear I will try and
    explain it better, sorry
    Thanks in advance for any help given,
    Marc

    if the game is really simple, sure why not. If your game will send a lot of data over the network, then you'll end up in the interesting part of network game programming; how to deal with latency / slow connections. Whole studies have been made out of that subject alone and it is far from easy material to understand, solve and implement.
    I would suggest you check out websites such as gamasutra and gamedev.net for discussions on the topic. A good place to get questions answered is here:
    http://www.gamedev.net/community/forums/showfaq.asp?forum_id=15
    Don't let the fact that it is aimed at C++ scare you away, the information in there mostly applies in general.
    because what is happening is that when a second players tried to join new players keep getting addedYes well that would be a bug in your code. Nobody can help you with that until you provide the code that is misbehaving.

  • HP Laserjet M1132 MFP is too slow over the network!

    Hi,
    My HP Laserjet M1132 MFP is too slow over the network!
    I have connect the printer to a windows XP 32bit SP3 using UBS port. The printer works fine on the local computer but when sharing it over the network it works too slow, for example with print test page it takes about 45 seconds to print and for other documents it takes much longer time.
    I have installed the latest driver form HP website and upgrade the firmware it the latest version.
    It happens on other Windows XP machines as well.
    I connected the printer to my laptop (Windows 7 64bit) and it works fine (No delays over the network) and it seems that this problem have some relations with the windows XP Driver.
    I have used local port trick on remote machines (instead of regular method) like this:
    ''add a local printer;
    'new port'
    'localport'
    \\XPcomputer\HPPrinterName as port name
    but still nothing!
    There is nothing wrong with the network, we use to use a Samsung printer on the same machines over the network with no problem.
    I Really appreciate your HELP!
    This question was solved.
    View Solution.

    BEHZAD_T, how is the printer connected to the network (wireless or Ethernet)? If it is slow wirelessly, I would suggest trying to connect the printer to your router with the Ethernet cord and install it to the networked computer that way.
    Another question is, relatively speaking, how close to your router are the Samsung printer and the HP printer? Depending on the distance (and what stands between the devices) there can be a lag between sending a print job and it being received by the printer.
    Let me know!

  • Slow network game

    Hi. I am trying to make a multiplayer network game. I got the game to work over a network connection, but there is a delay when I send and receive data and the delay seems to get bigger the longer the program runs. As you can see, I send 20 bytes and this is send about 30 times a second but should this be a problem on a localarea network?
    Here is the code I use for sending and receiving:
    // Class that send data
    import java.io.*;
    import java.net.*;
    public class SendNet {
        int port;
        String group;
        MulticastSocket s;
        public SendNet() {
            System.out.println("Connected SendNet");
            port = 5000;
            group = "230.0.0.1";
            try {
                s = new MulticastSocket();
                System.out.println("Connected!");
            } catch (IOException e) {System.out.println("Error");}
        public void sendData(String up, String down, String left, String right) {
             try
                String st = up+"-"+down+"-"+left+"-"+right+"-";
                System.out.println("Sending: "+st);
                byte[] b = new byte[st.length()];
                b = st.getBytes();
                DatagramPacket pack = new DatagramPacket(b, b.length, InetAddress.getByName(group), port);
                s.send(pack);
            }  catch (IOException e) {}
        public void closeConnection() {
            try {
                s.leaveGroup(InetAddress.getByName(group));
                s.close();
            } catch (IOException e) {}
    // Class that receive data
    import java.net.*;
    import java.io.*;
    public class ReceiveNet {
        int port;
        String group;
        MulticastSocket s;
        String[] move;
        public ReceiveNet() {
            port = 5000;
            group = "230.0.0.1";
            try {
                s = new MulticastSocket(port);
                s.joinGroup(InetAddress.getByName(group));
                System.out.println("Connected!");
            } catch (IOException e) {System.out.println("Error");}
        public String[] getData() {
            byte buffer[] = new byte[20];
            DatagramPacket pack = new DatagramPacket(buffer, buffer.length);
            try
                s.receive(pack);
                String st=new String(pack.getData());
                move = st.split("-");
                System.out.println("Receiving: "+st);
            catch (IOException e) {}
            return move;
        public void closeConnection() {
            try {
                s.leaveGroup(InetAddress.getByName(group));
                s.close();
            } catch (IOException e) {}
    }I've never played around with networking before, so maybe I am doing something wrong, or is it just this form of connection (multicastsocket) that is slow.

    I've never used multicast... but seeing as how nobody replied I'll add a few comments.
    First, I see you specify a port and group in the sender, but it's never used when initializing your connection. Is that a mistake?
    Second, there are a few places where you are creating excess objects which will eventually cause the garbage collector to do some work... which could conceivably slow things down. For example:
    // #1.  You are creating a new String for every invocation of sendData.
    # instead, you could use a StringBuffer, or build your data directly in a byte[]
    String st = up+"-"+down+"-"+left+"-"+right+"-";
    // #2.  You are allocating a byte array and throwing it away.  st.getBytes()
    // returns its own byte array.
    byte[] b = new byte[st.length()];
    b = st.getBytes();
    // change to:
    byte[] b = st.getBytes();
    // #3.  Related to #1, you are creating a new String object with every receive.
    s.receive(pack);
    String st=new String(pack.getData());Not sure whether those are the sources of your slowdown. If you still have trouble, how about providing the code which contains the send/receive loops?

  • IPhone game playing on internet, network  or AIM

    Hi All,
    I have completed my card board game for iphone. Now it has capability of playing usual 4 types(H VsH, C Vs C, C Vs H and H Vs C). Now I would like to provide players to play over internet, network or AIM.
    I wondering what kind of technologies are available on iPhone? If you can give me some references It will be grateful. Usually in Java/J2EE world, (Client/Server environment) create a web service interface between client and server, and send/receive events from web service. (Lot more things happens under cover)...
    Is this same approach I need to follow here also ....
    Thanks in advance
    - Ravi

    Okay, people, certainly someone has had a problem with "Syncing Contacts" taking a long long time.  I believe this has to have something to do with iCloud/iTunes/Outlook.  Please help.

  • Encrypted data transmission over the network

    Dear,
    We used to install oracle with default settings. For security , we use TDE, but in tde the data is not encrypted while transmitted over the network. My question, here are lots of experience DBA , is the network level encryption are used whenever you guy work with PROD system?? Or just ensure only password level encryption??
    Secondly, whenever we encrypt the network flow over the network ,is there any  performance overhead??
    Lastly, How can we configure the  network level encryption??

    I have not needed it, no, but that has nothing to do with whether *you* need it.
    I did not say there would be a performance issue. I said there would be some level of performance overhead. If (as an example), the overhead was very very very small, it's not an issue. I always get a kick out of people asking "I really need this feature (security, encryption, auditing, whatever). Will it cause performance issues?" If you NEED the feature, then you NEED it - the performance overhead is part of playing the game - it's no longer overhead. It's like asking, I need an Oracle database, will it cause my computer to run slower?

Maybe you are looking for