f



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

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,
sanjul

    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);
    } 
	
    }
}
0
sidhjn (5)
12/2/2004 5:40:27 PM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

1 Replies
836 Views

Similar Articles

[PageSpeed] 51

Siddharth Jain wrote:
> hello!
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.
As you have said this: the group comp.lang.java.help is especially 
well-suited for java beginners.

> 'shall greatly appreciate your help.
> 
> thanks in advance,
> sanjul
> 
Without having the complete exception information, we can hardly find 
out where and why the error occurs. To get this information you should add
	e.printStackTrace();
to the catch-clauses of your code.
After that you will get an exception stack trace with a rich amount of 
information.
Some hints how to proceed:
(1)  Read the javadoc of the exception class and of the
      method(s) reported on the very top of the stack trace.
(2)  Analyze the method of *your* code reported most near
      to the top of the stack trace.
      (source file-names and line-numbers are reported, too)
(3)  Understand and fix the bug.
      This is the most difficult part, of course ;-)
If you still don't find a solution, post your complete stack trace here, 
and mark the line of your code where the exception occured.

>     code:
> 
<code snipped>

Regards
	Thomas
-- 
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

0
no-spam811 (22)
12/2/2004 7:16:10 PM
Reply: