• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cleint-server interaction

 
Sanjul Jain
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}

==================================================================
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming the capitalizing client-server work and you're cool with all the concepts there. How to do the new problem ...

Think up a protocol for sending numbers. Will you send 1..n numbers in a single line, perhaps separated by spaces or commas? Then your current structure is pretty good. Instead of raising the line to upper case, break it into numbers (see String.split), add them up and send back the sum.

It might be more interesting (challenging == fun ) to send one number per line and send a blank line to indicate the end of the numbers. Then the protocol is more like:

A blank line as a terminator - the end of the numbers - looks a little like the HTTP protocol that uses a blank line at the end of all headers.

See if you can think of any other protocols. I have one more in mind - it would be cool if you could come up with a different one just to make the program your own. Does that give you a starting point? Holler if any of the other details - line parsing, making numbers out of strings - give you trouble.
 
Sanjul Jain
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic