Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Network Programming in C++/Java

Options
  • 22-02-2005 12:25pm
    #1
    Registered Users Posts: 96 ✭✭


    I'm looking for a program in either C++ or Java that can be run on a client in a small network. The program would "ask" the server for the time and it would be displayed on the screen of the client.

    I understand a program would also need to be running on the server and listening for requests.

    Any info on this would be appreciated.


Comments

  • Registered Users Posts: 2,426 ✭✭✭ressem


    Do you need the server or can you just use an NTP or SNTP client that interrogates one of the internets web services linked to the atomic clocks?


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,687 Mod ✭✭✭✭Capt'n Midnight


    You could just sync the PC's time with the server and then the local time will be matched

    alternativley in Windows you could setup a scheduled task ... every 12/24 hours
    NET time /domain:NAME /set /y

    no point in writing a program to do something that should really be done at OS level.


  • Registered Users Posts: 96 ✭✭TheHulk


    Sorry a lot of that went over my head!
    I dont know what you mean by do I "need" the server.

    Basically for a project, we've set up a network. 3 machines, 1 acting as the server. A program is needed to get the time from the server ( we were advised to use C++).

    Sorry if i'm repeating myself but I am not very knowledgable on this subject. :o


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    You can do this with Java very easily and quickly just look around on Google for java multithreaded server example. It shouldn't take long.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    If you're using Java then RMI is perfect for this.


  • Advertisement
  • Registered Users Posts: 4,287 ✭✭✭NotMe


    If you're using C++ then you want to look into sockets programming and Winsock in particular. It's fairly easy to write a basic server and client app.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    You were advised to use C++? Far far easier to do in java with rmi as suggested.
    see
    http://www.javacoffeebreak.com/articles/javarmi/javarmi.html
    and replace the power/square methods with a getdate function.


    If you end up having to use C++ then you have to choose whether you're going to use windows (mfc et al) or unix (bsd sockets, rpc, ...c o r b a...)


  • Registered Users Posts: 96 ✭✭TheHulk


    Thanks for your help guys. Just beginning to look in to it now. Seems like java rmi is the way to go with it. I'm sure I'll be back with more questions!


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    I don't see the need to delve into rmi for doing this in java. This is very basic java network programming, no need to complicate things. Both the client and server only need to be a handful of lines using java sockets.


  • Registered Users Posts: 96 ✭✭TheHulk


    MrPinK wrote:
    I don't see the need to delve into rmi for doing this in java. This is very basic java network programming, no need to complicate things. Both the client and server only need to be a handful of lines using java sockets.
    Could you direct me towards some sample code MrPink?


  • Advertisement
  • Registered Users Posts: 2,002 ✭✭✭bringitdown


    I am assuming this if for college, if wrong then I'll go away... *

    Short answer: Google my man, and a little research and brain usage.

    Long answer:
    If you were advised to use C++, and you are doing this for college/uni then use C++ ... throw in a Java version if you want brownie points or better still have a java cli, c++ server or vice versa.

    The problem is solved by low level socket programming in C++, as mentioned in Windows this probably means Winsock which resembles Berkley based sockets from the *n*x world. In *n*x sys/socket.h - provides a low level socket "API". It is usually BSD based too.

    In Java the java.net.* classes provide easy to use interfaces to the underlying socket based communications. The beauty of java is you are not tied to an OS and it masks much of the complexity. ie. is often easier. ;)

    As mentioned before there are some advanced APIs, standards and utilities that provide communication between client, server and so on such as RMI, CORBA, J2EE, ACE but they are probably out of scope in a college situation.

    Finding a basic Java, C++ and (socket.h || Winsock) sockets example is a Google away.

    * - I will not go away


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    TheHulk wrote:
    Could you direct me towards some sample code MrPink?
    Server
    ServerSocket serverSocket = new ServerSocket(portNumberToRunOn);
    while(true)
    {
        Socket newClientSocket = serverSocket.accept(); //wait for client connection
        PrintWriter out = new PrintWriter(newClientSocket.getOutputStream(), true);
        out.println("Time to be sent to client goes here");
        newClientSocket.close();
    }
    
    Client
    Socket socketToServer = new Socket(hostname, portNumber); //connect to server
    BufferedReader in = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
    String timeSentByServer = in.readLine();
    socketToServer.close();
    

    Not much more to Java network programming than that. Server waits for client connections, client connects to server. Once they're connected, you can open a PrintWriter/BufferedReader to send/recieve messages. Take a look at the Java API for Sockets for all the gory details. I left out some things like the Exception handling, didn't want to do all the work for you :)


  • Registered Users Posts: 3,299 ✭✭✭irishguy


    pm me your email ill send you on some code for a server/client and a multithreaded one and also some clock ones. It is fairly easy to do


Advertisement