Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > can't connect to remote host

Reply
Thread Tools

can't connect to remote host

 
 
Sid
Guest
Posts: n/a
 
      12-03-2004
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);
}

}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot connect to the Network Host with Remote Computer =?Utf-8?B?Sk9BVC1FT04=?= Wireless Networking 1 11-01-2006 09:44 AM
Could Not Connect To SMTP Host Connection Failed -- Software caused connection abort: connect kishore@novelteam.com Java 3 11-16-2005 03:33 PM
Unable to connect to remote host srithota@gmail.com Perl Misc 6 04-03-2005 10:07 AM
Remote Assistance fails to connect, remote remote host name could not be resolved Peter Sale Wireless Networking 1 12-11-2004 09:09 PM
can't connect to remote host Sid Java 1 12-04-2004 08:25 AM



Advertisments