Results 1 to 3 of 3
  1. #1
    leigh507 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Help with some Client-Server Programming

    I have been give an assignment to implement a simple Java download centre thingy, however i am having some trouble! :o. The problem lie in the protocol, we we given the Java knock knock example and had to adapt it to use switch statement instead of compound if statements. The protocol work before modification, but will not even execute the first Case loop! I have now been up for 36 hours because i think i am missing something really obvious. Here is the source code for the Client, server and protocol in that order...
    I would love some help please!!!

    Leigh

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package Assignment;
    import java.io.*;
    import java.net.*;
    /**
     *
     * @author Leigh
     */
    public class Assignment_1_client
    {
    
        public static void main(String[] args) throws IOException {
    
            Socket kkSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                kkSocket = new Socket("Leigh-PC", 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: Leigh-PC.");
                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();
        }
    }
    
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package Assignment;
    import java.net.*;
    import java.io.*;
    /**
     *
     * @author Leigh
     */
    public class Assignment_1_Server {
        public static void main(String[] args) throws IOException {
    
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(4444);
            } catch (IOException e) {
                System.err.println("Could not listen on port: 4444.");
                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;
            Assignment_1_Protocol kkp = new Assignment_1_Protocol();
    
            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();
        }
        }
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package Assignment;
    
    /**
     *
     * @author Leigh
     */
    public class Assignment_1_Protocol{
    
        private static final int WAITING = 1;
        private static final int SENTREPLY = 2;
        private static final int SENTCHOICE = 3;
        private int state = WAITING;
    
    
        public String processInput(String theInput) {
            String theOutput = null;
    
            switch (state){
                case 1:{
                    theOutput = "Terms of reference. Do you accept? Y or N";
                    
                    if (theInput.equalsIgnoreCase("Y")) {
                state = SENTREPLY;
                    }
            }
                case 2: {
                    theOutput = "1. computer program 2. picture 3. e-book";
                    state = SENTCHOICE;
               int i = Integer.parseInt(theInput);
                     switch (i) {
                    case 1: {
                        theOutput = " The program displays a message " ;
                    }
    
                    case 2: {
                        theOutput = "  The book is about...";
                    }
    
                    case 3: {
                        theOutput = "  The picture shows...";
                    }
    
                    default:{
                        theOutput = "That was not a valid selection";
                    }
    
                }
         }
            }
       return theOutput;     }
        }
    Last edited by leigh507; 02-23-2011 at 03:08 PM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Use Code Tags while posting your code here.

    Or else, majority of the members would avoid to read it.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    leigh507 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  2. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  4. PPPoE client programming in java
    By yeadam in forum Networking
    Replies: 0
    Last Post: 12-03-2007, 01:14 AM

Posting Permissions

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