bugs in clientserver program - can't connect to remote server
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   siddharth_jain
Posted On:   Thursday, December 2, 2004 09:21 AM

posted December 01, 2004 04:44 PM 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    More>>

posted December 01, 2004 04:44 PM


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
// 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 + "
";


//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
// 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+"
");
/** 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);
}



}
}

   <<Less
About | Sitemap | Contact