Elo guys, i have trying to develop a client/server application! i'm new with sockets, so i'm trying to learn from existing source codes. I have come accros a piece of code:

public static void main(String args[]) {

    // validate parameters:

    if (args.length != 2) {
        System.err.println("Usage: java GameClient <host> <friend_port>");
        System.exit(1);
    }


    // trying to connect to the server:
    try {
        String serverAddress = args[0];
        int serverPort = Integer.parseInt(args[1]);
        socket = new Socket(serverAddress, serverPort);
    } catch (Exception e) {
        System.err.println("Wrong arguments -> " + e);
        System.exit(1);
    }

    System.out.println(socket);

    // open in and out streams for talking with the server:
    try {
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintStream(socket.getOutputStream());
    }
    catch (IOException e) {
        System.err.println("Open streams -> " + e);
        System.exit(1);
    }


    // print the address of player - for verification:
    InetAddress localHost = null;
    try {
        localHost = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        System.out.println("Unknown host - probably localhost with no IP!");
        // no exit, since can work on "localhost" without internet.
    }
    System.out.println("Player's local address: " + localHost);

    // create GameClient and show its window:
    new GameClient();
}

the problem is first few lines where it has to validate the parameters

 if (args.length != 2) {
        System.err.println("Usage: java GameClient <host> <friend_port>");
        System.exit(1);
    }

String serverAddress = args[0];
int serverPort = Integer.parseInt(args[1]);

and got this error while running the code

Usage: java FriendClient <host> <friend_port>

where args[0] and args[1] comes from and where does args[0] and args[1] get their values from ?????

can anyone help please....!!!!

You might want to learn basic Java syntax before you start messing with network programming.

The Java tutorial (which can be found on Sun's Java site) explains things nicely, or get yourself an introductory text into the language like "Head First Java".

> Usage: java FriendClient <host> <friend_port>

This means that the program expects two command line parameters; the host name / address of the person you want to chat with along with the port to which chat application will listen to on his computer.

Like it has been already mentioned rightly, you need to read some good beginner books before diving in a complicated topic like Socket programming.

thanks...!

Like it has been already mentioned rightly, you need to read some good beginner books before diving in a complicated topic like Socket programming.

yep already started...!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.