bugs in client\server program - can't connect to remote server

Discussion in 'Java' started by Sid, Dec 3, 2004.

  1. Sid

    Sid Guest

    hello!

    I have a client/server program in which client lets user input two
    numbers and the server adds the numbers and returns the sum to client.

    the problems I am having are:

    1. client is not able to connect to remote server. it is only
    connecting to local host.

    2. if a string seperated by "non-numeric" characters (e.g. 34a2dd) is
    sent, both client and server throw an uncaught exception. i need to
    fix the client or server or both and give useful feedback to the user.
    i need to do the error checking of user i/p so that user enters only
    integers seperated by whitespace.

    I am attaching my code.
    I am new to java and probably don't know much.
    'shall greatly appreciate your help.

    thanks in advance,
    siddharth

    code:

    // usage: java TCPAdditionServer3 <port number>
    // default port is 7777
    // connection to be closed by client
    // this server handles only one connection

    import java.io.*;
    import java.net.*;
    import java.lang.*;
    import java.util.*;

    class TCPAdditionServer3{
    public static void main(String[] args)throws Exception{
    String clientSentence,capitalizedSentence;
    int sum=0;
    String sumString = "";
    InetAddress serverAddress;
    int portNumber = 7777;
    Socket connectionSocket = null;

    try {
    portNumber = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
    System.out.println(" port = 7777 (default)");
    portNumber = 7777;
    }

    // create welcoming socket at port number specified by user or
    7777 if no choice is specified by user
    ServerSocket welcomeSocket=new ServerSocket(portNumber);

    //server infinite loop
    while(true){
    try {
    // wait, on welcoming socket for contact by
    client
    connectionSocket=welcomeSocket.accept();
    } catch (IOException e) {
    System.out.println("Accept failed:");
    System.exit(-1);
    }

    // create input stream, attached to socket
    BufferedReader inFromClient=new BufferedReader(new
    InputStreamReader(connectionSocket.getInputStream()));
    // create output stream, attached to socket
    DataOutputStream outToClient=new
    DataOutputStream(connectionSocket.getOutputStream());

    // read in line from socket
    clientSentence=inFromClient.readLine();

    StringTokenizer st= new StringTokenizer(clientSentence);
    while(st.hasMoreTokens()) {
    sum += Integer.parseInt(st.nextToken());
    }

    System.out.println("Waiting for connections on port " +
    connectionSocket.getLocalPort() + " ... ");
    System.out.println("Received connection from " +
    connectionSocket.getInetAddress() + ":" +
    connectionSocket.getLocalPort());

    sumString = Integer.toString(sum);
    clientSentence = " ";
    capitalizedSentence = clientSentence + sumString + "\n";

    //write out line to socket
    outToClient.writeBytes(capitalizedSentence);

    //connection closed by client
    try {
    connectionSocket.close();
    System.out.println("Connection closed by client");
    }
    catch (IOException e) {
    System.out.println(e);
    }

    }
    }

    }

    code:

    // usage: java TCPAdditionClient3 <server> <port>
    // default port is 7777

    /**
    * TCPAdditionClient1.java
    *
    * @author Siddharth Jain
    * @version 3.0
    */

    /** Client Code */

    import java.io.*;
    import java.net.*;

    /** this is the main class */
    class TCPAdditionClient3{
    public static void main(String[] args)throws Exception{

    String sentence,sentc,portNumberString;
    int portNumber= 7777;
    String hostName = "localhost";
    String modifiedSentence;
    InetAddress clientAddress;
    /** create input stream */
    BufferedReader inFromUser1=new BufferedReader(new
    InputStreamReader(System.in));

    /** read arguments */
    if(args.length == 2) {
    hostName = args[0];
    try {
    portNumber = Integer.parseInt(args[1]);
    System.out.println("client user i/p port number " +
    portNumber);
    }
    catch (Exception e) {
    System.out.println(" server port = 7777 (default)");
    portNumber = 7777;
    }
    }

    /** connect to server */
    try{
    /** create client socket, connect to server */
    Socket clientSocket=new Socket(hostName,portNumber);

    if (clientSocket.isConnected()== true);
    {
    System.out.println("Connecting to Server " +
    clientSocket.getInetAddress() + ":" + clientSocket.getPort() + "..." +
    "Connected");
    }

    /** create output stream attached to socket */
    DataOutputStream outToServer=new
    DataOutputStream(clientSocket.getOutputStream());
    /** create input stream attached to socket */
    BufferedReader inFromServer=new BufferedReader(new
    InputStreamReader(clientSocket.getInputStream()));

    System.out.println();
    System.out.println("Enter a string of integers seperated by
    non-numeric characters:");

    sentence = inFromUser1.readLine();
    /**send line to server */
    outToServer.writeBytes(sentence+"\n");
    /** read line from server */
    modifiedSentence=inFromServer.readLine();
    System.out.println("From Server-----> The Sum
    is:"+modifiedSentence);

    try {
    clientSocket.close();
    }
    catch(IOException e) {
    System.out.println(e);
    }

    } catch(UnknownHostException e) {
    System.err.println(" Don't know about host: ");
    System.exit(1);
    } catch(IOException e) {
    System.err.println(" Couldn't get I/O for the connection");
    System.exit(1);
    }

    }
    }
    Sid, Dec 3, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mamcx
    Replies:
    1
    Views:
    391
    karim
    Jul 18, 2003
  2. Otis Mukinfus
    Replies:
    3
    Views:
    904
    Juan T. Llibre
    Jul 8, 2005
  3. Siddharth Jain
    Replies:
    1
    Views:
    2,870
    Thomas Fritsch
    Dec 2, 2004
  4. Josef 'Jupp' Schugt

    Still use 'ruby-bugs' for Ruby bugs?

    Josef 'Jupp' Schugt, Nov 4, 2004, in forum: Ruby
    Replies:
    2
    Views:
    156
    Tom Copeland
    Nov 4, 2004
  5. Replies:
    2
    Views:
    271
Loading...

Share This Page