Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Knock-Knock Client

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the example code from the Sun website for the KnockKnock client.
public class KnockKnockClient {
public static void main(String[] args) throws IOException {
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("taranis", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;

fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
My questions are-
Where in the code is the client actually sending the string values to the server?
I want to implement a simple client where I want to send my name as entered on the command prompt to the server.
What should to do to achieve this?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucky Singh:

My questions are-
Where in the code is the client actually sending the string values to the server?

The line
out.println(fromUser);

I want to implement a simple client where I want to send my name as entered on the command prompt to the server.
What should to do to achieve this?

Well, that's pretty much what this does, right? Do you mean you only want it to prompt once?
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sending my name to the server. Nothing else.
Is the following correct?
import java.io.*;
import java.net.*;
public class KnockKnockClient
{
public static void main(String[] args) throws IOException
{
Socket kkSocket = null;
PrintWriter out = null;
try
{
kkSocket = new Socket("taranis", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: taranis.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to:
taranis.");
System.exit(1);
}
String fromUser = new String("Lucky Singh");;
while ((fromUser = in.readLine()) != null)
{
if (fromUser != null)
{
out.println(fromUser);
}
}
out.close();
kkSocket.close();
}
}
Why are we using out.println() to send message from client to server? Does this mean, my name is being printd on the server?
What do I do if I donot want my name to be printed on the server?
Is there no method like kksocket.send(fromUser) ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucky Singh:

Why are we using out.println() to send message from client to server? Does this mean, my name is being printd on the server?
What do I do if I donot want my name to be printed on the server?
Is there no method like kksocket.send(fromUser) ?

Babies have to learn to crawl before they can walk, and walk before they can run. Programmers are no different. You've got to understand Java I/O in a single process before there's any point in trying to learn about networking. So what I would do, if I were you, would be to drop this problem for now, and go work through the Java I/O tutorial. Once you've done that, come back to this program and see if you don't understand it much better.
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, Will go through tutorial.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic