wood burning stoves
The moose likes Sockets and Internet Protocols and the fly likes cleint-server interaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "cleint-server interaction" Watch "cleint-server interaction" New topic

cleint-server interaction

Sanjul Jain

Joined: Oct 21, 2004
Posts: 14

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,

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

class TCPServer{
public static void main(String[] argv)throws Exception{
String clientSentence,capitalizedSentence;
ServerSocket welcomeSocket=new ServerSocket(7777);
Socket connectionSocket=welcomeSocket.accept();
BufferedReader inFromClient=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient=new DataOutputStream(connectionSocket.getOutputStream());


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();
System.out.println("From Server----->"+modifiedSentence);

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sanjul Jain

Joined: Oct 21, 2004
Posts: 14
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,
I agree. Here's the link: http://aspose.com/file-tools
subject: cleint-server interaction
It's not a secret anymore!