can't connect to remote host

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

  2. On 3 Dec 2004 15:01:42 -0800, Sid wrote:

    > 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:


    You are having some problems even before your code, let's
    get those sorted first.

    > 1. client is not able to connect to remote server.


    Fails how? What happens, are exceptions thrown, what exceptions?
    <http://www.physci.org/codes/javafaq.jsp#exact>

    >....it is only connecting to local host.


    Also, ask a specific question.
    <http://www.physci.org/codes/javafaq.jsp#specific>
    (One of the aspects of asking a smart question is not to
    forget to *ask* an actual question)

    ....
    > I am attaching my code.


    Tips for example..
    Indent code lines with spaces, it is easier to read
    (but still keep them short). Further tips on the example.
    <http://www.physci.org/codes/sscce.jsp>

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


    You are posting to the wrong group.
    The best group for those starting Java can be found here..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    See to those matters in a new post to c.l.j.help
    (and I suggest you deal with them in separate threads)
    and you will probably get more and better help.

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 4, 2004
    #2
    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. Otis Mukinfus
    Replies:
    3
    Views:
    958
    Juan T. Llibre
    Jul 8, 2005
  2. Sid
    Replies:
    0
    Views:
    456
  3. Replies:
    3
    Views:
    40,660
    Roedy Green
    Nov 16, 2005
  4. Replies:
    6
    Views:
    370
    Brian McCauley
    Apr 3, 2005
  5. Replies:
    2
    Views:
    326
Loading...

Share This Page