Meaningless Drivel is fun!*
The moose likes Sockets and Internet Protocols and the fly likes Knock-Knock Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Knock-Knock Client" Watch "Knock-Knock Client" New topic
Author

Knock-Knock Client

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jan 19, 2004
Posts: 125
Yep, Will go through tutorial.
 
jQuery in Action, 2nd edition
 
subject: Knock-Knock Client
 
Similar Threads
creating new sockets
I am getting java.net.UnknownHostException
client / server (communication problem...)
connection problem...
sending my name