Results 1 to 3 of 3
  1. #1
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default java knock knock client server

    Hello i am trying to run the well known java example java knock-knock client-server and i am having some problems.Could someone help me please?


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

    public class KnockKnockClient {
    public static void main(String[] args) throws IOException {
    InetAddress host = InetAddress.getLocalHost();
    final int PORT = 4444;
    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
    echoSocket = new Socket(host, PORT);
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(
    echoSocket.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 userInput;

    while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println("echo: " + in.readLine());
    }

    out.close();
    in.close();
    stdIn.close();
    echoSocket.close();
    }
    }


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

    public class KnockKnockServer {
    public static void main(String[] args) throws IOException {
    final int PORT = 1234;
    ServerSocket serverSocket = null;
    try {
    serverSocket = new ServerSocket(PORT);
    } catch (IOException e) {
    System.err.println("Could not listen on port: 1234.");
    System.exit(1);
    }

    Socket clientSocket = null;
    try {
    clientSocket = serverSocket.accept();
    } catch (IOException e) {
    System.err.println("Accept failed.");
    System.exit(1);
    }

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    clientSocket.getInputStream()));
    String inputLine, outputLine;
    KnockKnockProtocol kkp = new KnockKnockProtocol();

    outputLine = kkp.processInput(null);
    out.println(outputLine);

    while ((inputLine = in.readLine()) != null) {
    outputLine = kkp.processInput(inputLine);
    out.println(outputLine);
    if (outputLine.equals("Bye."))
    break;
    }
    out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mike_ledis View Post
    Hello i am trying to run the well known java example java knock-knock client-server and i am having some problems.Could someone help me please?
    Ok, my guess is that you don't have a KnockKnockProtocl class? Right? Or your local host isn't named taranis ...

    Seriously, how are we supposed to know what your problems are? Don't make us guess but give us as much relevant information as possible.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Java Server/C Client
    By FallenBlade in forum Networking
    Replies: 13
    Last Post: 03-10-2011, 11:22 PM
  2. Java Server Client C problem
    By Jelle87 in forum Networking
    Replies: 1
    Last Post: 01-13-2011, 02:32 PM
  3. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  4. java server and c client ?????????
    By biebo in forum Networking
    Replies: 7
    Last Post: 07-24-2010, 04:35 AM
  5. Knock Knock!
    By Boyee in forum Introductions
    Replies: 8
    Last Post: 11-07-2007, 06:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •