Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    i wrote Space ship multi-player game in java.... in which player one controls his space ship using W,A,S,D and player two I,J,K,L....and i use timer and velocity to control the animation of the two space ships.......so i want to take this game to another level by create a network support for it Server/Client......i want to create Server to host two Clients and then from the Client send Coordinates, Timer and Velocity state to the server and then from the server to the other client.......i read some articles that this can be done using DataOutputStream/DataInputStream......but i don't know to implement it.....i really need some help with this.....any kind of help would be greatly appreciated.................

  2. The Following User Says Thank You to MdeeY For This Useful Post:

    gloria10 (May 23rd, 2013)


  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    What is the design for how the clients and server will communicate?
    What will one client send to the other client?
    How will the Coordinates, Timer and Velocity values be sent so the other client knows what they are?
    How will the server help the clients communicate in the playing of the game?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    i want them to communicate using TCP/IP........i want Client one to control Blue Space ship and Client two to control Red Space ship....so i want Client one to send the spaceship state( integer coordinates axis x/y, Timer which i use a method called Star_Animation to start it.....then the Velocity which is an integer value ).....And the Server is the one who will receive the (e.g int x = 600,y= 400 and (int Velocity = 0) and (Timer t = new Timer().........so i guess the server will send this "t.start()" as well )
    open this link to see the design of the game.
    https://www.dropbox.com/s/fef9x7ow4u...29.25%20PM.png

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    want Client one to send the spaceship state( integer coordinates axis x/y, Timer
    So every message from one client to the other will always contain the following data:
    coordinates
    Timer value
    velocity

    A sample message would be these 4 numbers: 123 456 20 33
    where 123 and 456 are the coordinates
    20 is the timer value
    33 is the velocity

    All messages would have 4 numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Yes....i think so

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Ok. I'd suggest first writing the classes and methods with a set of testing drivers for clients that sent sample data from one client to the other, displayed what they received and would send sample messages back.
    When those work, then modify the game code to use the classes and methods.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    i already have a Server/ Client application lots of them........even GUI but i don't know how to modify the code to word with the server/client..........i can show u the server/client code if u want ?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Have you made a server that will wait for connections from two clients and set up the streams from the sockets so what is received from one client is sent to the other? Start with that and a simple client app that sends and receives via the server to test the server.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    yes i have........

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Does it work?

    --- Update ---

    On the sending side: Open a DataOutputStream, and write the 4 int values (as shown in post #4) and flush the stream.
    On the receiving side: read the 4 int values from the DataInputStream

    I have no idea how you have written the server and the client code to be able to recommend how to send data from one client to another.
    How have you tested the code? Did each client send several messages to the other client? Were the messages received ok? By message I mean any data, not necessarily Strings. The 4 int could be considered a message.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    import java.io.*;
    import java.net.*;
     
    public class TCPClient {
        public static void main(String argv[]) throws Exception {
            String sentence;
            String modifiedSentence;
            BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
     
            Socket clientSocket = new Socket(" localhost", 6789);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
     
            sentence = inFromUser.readLine();
            outToServer.writeBytes(sentence + '\n');
            modifiedSentence = inFromServer.readLine();
            System.out.println(modifiedSentence);
            clientSocket.close();
        }
    }
















    import java.io.*;
    import java.net.*;
     
    public class TCPServer {
        public static void main(String args[]) throws Exception {
            String clientSentence;
            String capitalizedSentence;
            ServerSocket welcomeSocket = new ServerSocket(6789);
     
            while(true) {
                Socket connectionSocket = welcomeSocket.accept();
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                clientSentence = inFromClient.readLine();
                capitalizedSentence = clientSentence.toUpperCase() + '\n';
                outToClient.writeBytes(capitalizedSentence);
            }
        }
    }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Where is the driver for testing? I don't see any code in the server for handling two clients.
    Post #8 asked:
    Have you made a server that will wait for connections from two clients and set up the streams from the sockets so what is received from one client is sent to the other? Start with that and a simple client app that sends and receives via the server to test the server.
    There is a lot more needed logic for the server and client. When the first client connects, the server needs to save the connection (and perhaps tell the client that he needs to wait for the second client).
    When the second client connects, the server needs to save the connection and provide code to connect the output from each client to be written to the opposite client. The server should also tell the clients that there are now two clients and the game can start.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    mean Multi-threaded Server?

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    I guess each connection would need its own thread.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?


  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    I'm not sure trying to rewrite someone else's code would be easier than writing new code.
    Make a design and then write the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    yeah someone gave me this code on Dream.in.code website....he said this would be easier and he think is similar to what my game needs.........https://www.dropbox.com/sh/j6seqhl3l4l30vh/wFrgXXnd1y

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Rework the code into the server and client code you need, make a testing class (what I called a driver) and test it.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    i don't know how to do it......can u do it for me please!!!!!!! u have a better knowledge of Java

    this is my Game code https://www.dropbox.com/sh/329uiet38jv529r/Uxo0e07Ygf

    thanks man!

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Sorry, I don't write code for students. Wouldn't that be cheating?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    man.....i have tried my best to understand that...but i couldn't.....and time is against me....this assignment is due to be submitted next week and this is my final year..i have a lot of work to do my final project and also three courses excluding Java.......Man please!! if i don't get good result in this course my GPA will drop........and if u help i'll study the code very well and possibly make so modifications.....And i study the codes cos we have exam on this course..........please! help me

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    Make a design for the server. When you get all the details worked out then try coding it. When you have problems,
    post the code and your questions about the problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java<<Coordinates, Timer and Velocity send through DataOutputStream/DataInputStream help?

    If you have a question feel free to post it.
    This site does not assist in cheating, as Norm mentioned, thread closed.

Similar Threads

  1. Reading the data from DataInputStream . help :(
    By cuihanluan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 7th, 2012, 07:54 AM
  2. Java Coordinates
    By qspec in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2012, 10:15 PM
  3. Java graphic coordinates out of bounds?
    By jnmcneel in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 24th, 2011, 03:28 PM
  4. Error by Using DataInputStream
    By pokuri in forum Collections and Generics
    Replies: 2
    Last Post: January 13th, 2011, 12:03 PM
  5. help with dataoutputstream
    By cool mr croc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 9th, 2011, 06:57 AM