Sanjul Jain

Greenhorn
+ Follow
since Oct 21, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sanjul Jain

thanks for your reply, but its not working. this is what i have done:



I need to print the value of s.


Regards,
Sanjul
18 years ago
how can i convert string to short in java.

thanks in advance,
sanjul
18 years ago
Hello!,

Thanks for your mail.

can you please show me how to do the second part - 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 tried using isDigit(), but 'am struggling with it.

i probably don't know much about java.

i will be really grateful for your help.

warm regards,
siddharth
hello! alex,

i tried connecting two computers on a same network w/o firewall. it is simply not working, and i have no clue why is it so.

i shall really appreciate if you kindly look into it.

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



Hello!

I am a newbie to java and shall greatly appreciate your help.

I have an array of capacity 10 with values say {0,3,1,0,0,0,0,0,0,0}

the values in array, which are not zero, need to be decremented to 0. e.g. 3 needs to be decremented to 0. the criteria for decreasing value of 3 is that 3 should decrement by 1, after 1 minute of system time, until 3 is 0, and the value of 3 should be replaced by 0, in the array.


Thanks in advance,
Sanjul
19 years ago
Hello!

Instead of giving default value of port number 7777, I want to

let user specify the value of port number.

Can you please help me out.


Regards,
Sanjul

Here is my code:

//server code:
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;

class TCPAdditionServer1{
public static void main(String[] argv)throws Exception{
String clientSentence,capitalizedSentence;
int sum=0;
String sumString = "";
InetAddress serverAddress;

int listen_port = 7777;
//int listen_port = Socket.getLocalPort();

//ServerSocket welcomeSocket=new ServerSocket(7777);
ServerSocket welcomeSocket=new

ServerSocket(listen_port);

//ServerSocket welcomeSocket=new

ServerSocket(welcomeSocket.getPort());

//int cpn = welcomeSocket.getLocalPort();
// System.out.println(" get local port: " + cpn);

//int cpn1 = welcomeSocket.getPort();
//System.out.println(" get remote port: " + cpn1);

// welcomeSocket(cpn);

while(true){

Socket connectionSocket = null;

try {
connectionSocket=welcomeSocket.accept();
} catch (IOException e) {
System.out.println("Accept failed:");
System.exit(-1);
}

BufferedReader inFromClient=new BufferedReader(new

InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient=new

DataOutputStream(connectionSocket.getOutputStream());
clientSentence=inFromClient.readLine();

StringTokenizer st= new

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

int clientPortNumber =

connectionSocket.getLocalPort();
System.out.println("Waiting for connections on

port " + clientPortNumber + " ... ");


System.out.println("Received connection from "

+ connectionSocket.getRemoteSocketAddress());


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

"\n";
outToClient.writeBytes(capitalizedSentence);

}
}

}

========================================
//client code
import java.io.*;
import java.net.*;
import java.net.InetAddress;

class TCPAdditionClient1{
public static void main(String[] argv)throws Exception{
String sentence,sentc,portNumberString;
int portNumber= 7777;
String modifiedSentence;
InetAddress clientAddress;
BufferedReader inFromUser1=new BufferedReader(new

InputStreamReader(System.in));
BufferedReader inFromUser2=new BufferedReader(new

InputStreamReader(System.in));
BufferedReader inFromUser3=new BufferedReader(new

InputStreamReader(System.in)); //portNumberString

try{

System.out.println("Enter port number (Press Enter for

default value(7777)): ");
portNumberString = inFromUser3.readLine();

int portNumberInteger =

Integer.parseInt(portNumberString);

portNumber = portNumberString.length() == 0 ? 7777 :

portNumberInteger;


//System.out.println("port number:" + portNumber);

//working

System.out.println("Enter hostname or IP of server:

(Press Enter for default value(localhost)): ");
sentc = inFromUser1.readLine();

String hostName = sentc.length() == 0 ? "localhost" :

sentc;
Socket clientSocket=new Socket(hostName,portNumber);

SocketAddress sa =

clientSocket.getRemoteSocketAddress();

if (clientSocket.isConnected()== true);
{
System.out.println("Connecting to server " + "'" + sa

+ "'" + "..." + " Connected.");
}

DataOutputStream outToServer=new

DataOutputStream(clientSocket.getOutputStream());
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 = inFromUser2.readLine();

outToServer.writeBytes(sentence+"\n");
modifiedSentence=inFromServer.readLine();
System.out.println("From Server----->The Sum

is:"+modifiedSentence);

clientSocket.close();

} 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);
}

}
}

===================
Thanks a ton!


I need to change string "7777" to integer 7777, and integer 15 to string "15".

I would appreciate if you can help.

Thanks in advance,
Sanjul
19 years ago
Hello!

I need to change string "7777" to integer 7777, and integer 15 to string "15".

I would appreciate if you can help.

Thanks in advance,
Sanjul
19 years ago
Hello!

I am a newbie to Java. I want to write a program that lets user input numbers(integers) seperated by whitespace. The program displays sum of the numbers. Can someone suggest me code for that.


Thanks in advance,
Sanjul
19 years ago
Hello!

I am a newbie to Java. I have a string e.g "12 03". I need to add the two numbers present in

this string (sum = 15). Can someone please guide me how to do this.


Thanks in advance,
Sanjul
19 years ago
Thanks for your reply.

I need to send numbers in a single line, seperated by a whitespace.

How will I scan string and get numbers out of it.

e.g. the user inputs: "12 03 77", which is a string. I need to get these individual numbers as 12, 03 and 77, and then add them(92).
How will I do that.

Thanks in advance,
Warm Regards,
Sanjul
Hello!

I need to change the following code such that the Client sends numbers to the Server and Server replies with sum of thoose number.

I am new to Java and don't probably know much. I would greatly appreciate, if you can help.

Thanks in advance,
Regards,
Sanjul


============================================================
//TCPServer.java
import java.io.*;
import java.net.*;

class TCPServer{
public static void main(String[] argv)throws Exception{
String clientSentence,capitalizedSentence;
ServerSocket welcomeSocket=new ServerSocket(7777);
while(true){
Socket connectionSocket=welcomeSocket.accept();
BufferedReader inFromClient=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient=new DataOutputStream(connectionSocket.getOutputStream());
clientSentence=inFromClient.readLine();
capitalizedSentence=clientSentence.toUpperCase()+"\n";
outToClient.writeBytes(capitalizedSentence);
}
}
}

==============================================================
//TCPClient.java

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

class TCPClient{
public static void main(String[] argv)throws Exception{
String sentence;
String modifiedSentence;
BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket=new Socket("localhost",7777);
DataOutputStream outToServer=new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence+"\n");
modifiedSentence=inFromServer.readLine();
System.out.println("From Server----->"+modifiedSentence);
clientSocket.close();
}
}

==================================================================
Hello!

I am a new bie and am' trying to write a java program - one for server side and one for client side, using sockets, which takes input numbers from clients, and returns sum to the client.

Can anyone please help me how i can proceed.

Thanks in advance,
Sanjul