Results 1 to 17 of 17
  1. #1
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Problem connection client to server (Unknown Source)

    Greetings EveryOne :D

    I want to achieve server/client communication, I started with Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets).

    I have made some changes on the code to make server/client connect on the network not only on localhost, but unfortunately this is what i get instead:

    Java Code:
    Exception in thread "main" java.net.SocketException: Connection reset
            at java.net.SocketInputStream.read(Unknown Source)
            at java.net.SocketInputStream.read(Unknown Source)
            at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
            at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
            at sun.nio.cs.StreamDecoder.read(Unknown Source)
            at java.io.InputStreamReader.read(Unknown Source)
            at java.io.BufferedReader.fill(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at KnockKnockServer.main(knockknockserver.java:50)
    This is the code i'm working on:

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class KnockKnockServer
    {
        public static void main(String args[]) throws IOException
        {
            int Port = Integer.parseInt(args[0]); //4444;
            
            ServerSocket S_Socket = null;
            
            try
            {
                S_Socket = new ServerSocket(Port);
            }
            catch(IOException IOE)
            {
                System.err.println("Could Not Listen On Port: "+Port+".");
                System.exit(-1);
            }
            
            Socket C_Socket = null;
            
            try
            {
                C_Socket = S_Socket.accept();
            }
            catch(IOException IOE)
            {
                System.err.println("Accept Failed.");
                System.exit(-1);
            }
            
            PrintWriter    PW = new PrintWriter(C_Socket.getOutputStream(), true);
            
            BufferedReader BR = new BufferedReader(new InputStreamReader
                                                      (C_Socket.getInputStream()));
            
            String InputLine, OutputLine;
            
            KnockKnockProtocol KKP = new KnockKnockProtocol();
            
            OutputLine = KKP.ProcessInput(null);
            PW.println(OutputLine);
            
            while((InputLine = BR.readLine()) != null)
            {
                OutputLine = KKP.ProcessInput(InputLine);
                PW.println(OutputLine);
                
                if(OutputLine.equalsIgnoreCase("Bye."))
                {
                    System.out.println("Bye Bye.");
                    break;
                }
            }
            
            PW.close();
            BR.close();
            C_Socket.close();
            S_Socket.close();
        }
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class KnockKnockClient
    {
        public static void main(String args[]) throws IOException
        {
            String IP$ = args[0];
            int Port = Integer.parseInt(args[1]); //4444;
            
            Socket C_Socket   = null;
            PrintWriter    PW = null;
            BufferedReader BR = null;
            
            try
            {
    //            C_Socket = new Socket("localhost", Port);
                C_Socket = new Socket(InetAddress.getByName(IP$), Port);
                PW       = new PrintWriter(C_Socket.getOutputStream(), true);
                BR       = new BufferedReader(new InputStreamReader
                                                      (C_Socket.getInputStream()));
            }
            catch(UnknownHostException UHE)
            {
                System.err.println("Don't Know About Host: "+IP$);
                System.err.println(UHE);
                System.exit(-1);
            }
            catch(IOException IOE)
            {
                System.err.println("Couldn't Get I/O For The Connection To: "
                                    +IP$);
                System.err.println(IOE);
                System.exit(-1);
            }
            
            BufferedReader StdIn = new BufferedReader(new InputStreamReader
                                                               (System.in));
            
            String FromServer;
            String FromUser;
            
            while((FromServer = BR.readLine()) != null)
            {
                System.out.println("Server: "+FromServer);
                
                if(FromServer.equals("Bye."))
                    break;
                
                FromUser = StdIn.readLine();
                
                if(FromUser != null)
                {
                    System.out.println("Client: "+FromUser);
                    PW.println(FromUser);
                }
            }
            
            PW.close();
            BR.close();
            StdIn.close();
            C_Socket.close();
        }
    }
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class KnockKnockProtocol
    {
        private static final int WAITING         = 0;
        private static final int SENTKNOCKKNOCK  = 1;
        private static final int SENTCLUE        = 2;
        private static final int ANOTHER         = 3;
        
        private static final int NUMJOKES = 5;
        
        private int State       = WAITING;
        private int CurrentJoke = 0;
        
        private String Clues[] =
        {
          "Turpin", "Little Old Lady", "Atch", "Who", "Who"  
        };
        
        private String Answers[] =
        {
            "Turpin The Heat, It's Cold In Here!",
            "I Didn't Know You Could Yodel!",
            "Bless You!",
            "Is There An Owl  In Here?",
            "Is There An Echo In Here?"
        };
        
        public String ProcessInput(String TheInput)
        {
            String TheOutput = null;
            
            if(State == WAITING)
            {
                TheOutput = "Knock! Knock!";
                State     = SENTKNOCKKNOCK;
            }
            else
            if(State == SENTKNOCKKNOCK)
            {
                if(TheInput.equalsIgnoreCase("who's there?"))
                {
                    TheOutput = Clues[CurrentJoke];
                    State     = SENTCLUE;
                }
                else
                {
                    TheOutput = "You're Supposed To Say \"Who's There?\"! "
                                +"Try Again. Knock! Knock!";
                }
            }
            else
            if(State == SENTCLUE)
            {
                if(TheInput.equalsIgnoreCase(Clues[CurrentJoke]+" Who?"))
                {
                    TheOutput = Answers[CurrentJoke]+" Want Another? (y/n)";
                    State     = ANOTHER;
                }
                else
                {
                    TheOutput = "You're Supposed To Say \""
                                +Clues[CurrentJoke]+" Who?\"!"
                                +"Try Again. Knock! Knock!";
                    State     = SENTKNOCKKNOCK;
                }
            }
            else
            if(State == ANOTHER)
            {
                if(TheInput.equalsIgnoreCase("y"))
                {
                    TheOutput = "Knock! Knock!";
                    
                    if(CurrentJoke == (NUMJOKES-1))
                    {
                        CurrentJoke = 0;
                    }
                    else { CurrentJoke++; }
                    
                    State = SENTKNOCKKNOCK;
                }
                else
                {
                    TheOutput = "Bye.";
                    State     = WAITING;
                }
            }
            
            return TheOutput;
        }
    }
    Thank You In Advance, Please SomeOne Help.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    Could you show the consoles from when you execute the code to show the args passed to main().

    For easier testing, hardcode the values in the program so it easier to start and execute the code without having to pass it args on the commandline.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    Thank you Sir Norm for your interest to help and sorry for taking long to reply it's so busy with work at this time of year.

    Could you show the consoles from when you execute the code to show the args passed to main().
    Here are the screenshot:

    Problem connection client to server (Unknown Source)-serv.png
    Problem connection client to server (Unknown Source)-client.png

    For easier testing, hardcode the values in the program so it easier to start and execute the code without having to pass it args on the commandline.
    The IP isn't static so i thought it would be easier to pass it thought args on the commandline instead of having to recompile the code every time the code would be tested as the IP address may change.

    Thank You In Advance :D
    Last edited by Shikatsu; 08-12-2012 at 04:48 PM.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    I have not setup my system to work using an external IP address. I can only test with localhost.
    If the program works using localhost, then you need to check for problems with things like firewalls, routers etc.


    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Last edited by Norm; 08-12-2012 at 05:15 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Java Code:
    C:\Documents and Settings\Administrator\Bureau\KKSC>java KnockKnockServer 4444
    Exception in thread "main" java.net.SocketException: Connection reset
            at java.net.SocketInputStream.read(Unknown Source)
            at java.net.SocketInputStream.read(Unknown Source)
            at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
            at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
            at sun.nio.cs.StreamDecoder.read(Unknown Source)
            at java.io.InputStreamReader.read(Unknown Source)
            at java.io.BufferedReader.fill(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at KnockKnockServer.main(knockknockserver.java:50)
    
    C:\Documents and Settings\Administrator\Bureau\KKSC>
    Thank You In Advance ;D
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    When I execute the programs I get this:
    Running: java KnockKnockProgram

    S got connection soc=Socket[addr=/127.0.0.1,port=59241,localport=8880]
    C - from Server: Knock! Knock!
    C - Sending to Server: who's there?
    S read IL=who's there?
    C - from Server: Turpin
    C - Sending to Server: Turpin Who?
    S read IL=Turpin Who?
    C - from Server: Turpin The Heat, It's Cold In Here! Want Another? (y/n)
    C - Sending to Server: n
    S read IL=n
    S - Bye Bye.
    C - from Server: Bye.
    exiting main

    0 error(s)
    My testing code:
    Java Code:
       final static String ThePort = "8880";
    
       public static void main(final String[] args) { //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
          Thread t1 = new Thread(new Runnable() {
             public void run(){
                KnockKnockServer.main(new String[]{ThePort});
             }
          });
          t1.start();
          Thread t2 = new Thread(new Runnable() {
             public void run() {
               KnockKnockClient.main(new String[]{"127.0.0.1", ThePort});
             }
          });
          t2.start();
    
          // wait some
          try{Thread.sleep(5000);}catch(Exception x){}
          System.out.println("exiting main");
          System.exit(0); // done
    
       }
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    when i test the program on my local host it works fine, but when attempting to make the altered code to connect two computers on the world wide web it doesn't work, maybe my altered code misses something. That's my problem.

    Thank You In Advance.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    What about routers and firewalls, etc Something outside of your program.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Problem connection client to server (Unknown Source)

    I do not think you have your computer, your routers, and your firewalls are set up properly to work on the world wide web. Also, making them open can be a security hole but putting them on a higher port number will help you prevent that. The port number you have chosen is way too low. You should choose one above 49151. The ones above this number are not registered. So 50000 would work.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  10. #10
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    Thank you Sir Norm and Mr kammce for your reply, I'll disable my firewall and test my code.

    I had made a checkers game presented on JApplet, now i want to step further and learn how to make the game playable between two players over the WWW, that's why i searched for client server communication, the only useful thing i found was Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets) tutorial, but afterward seems to me it's not enough.

    Would You Guide Me Please To The Right Path, Thank You In Advance ;D
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    Please describe in detail how you want the clients and server to communicate? Are the clients executing the applets?
    There are many sample client server programs here on the forum that you should be able to get some ideas from.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    First step, i would like to learn to make one computer executes the game's applet as a server and the other computer would executes the game's applet as a client, establish a socket communication, transfer the data between them, and each computer-end process and implements the received data.

    Second step, i would like to learn to make the game executed on a host server then get the two players (they are both clients in this case) establish a connection to the server and transfer the data between the server and the two clients to make the game (applet) playable.

    Thank You In Advance, I Really Appreciate Your Help.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    one computer executes the game's applet as a server and the other computer would executes the game's applet as a client,
    That does not make sense. An applet executes on a client's machine, not on a server.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    Quote Originally Posted by Norm View Post
    That does not make sense. An applet executes on a client's machine, not on a server.
    Please don't mind my luck of knowledge on java networking.
    From this tutorial: Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets), you can see on the server-side, the user can type chain of characters and send them to the client, so i thought an applet can be executed on a server machine.

    Can you advice me what readings should i start with? Thank You.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    Sorry, I don't have any links for reading material for networking.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Problem connection client to server (Unknown Source)

    After some readings i found that you use servlets for server-side programming instead of applets.

    Quote Originally Posted by Norm View Post
    Sorry, I don't have any links for reading material for networking.
    I am not asking for links to physical materials, what i am asking for is the topics i should look for and start with, and maybe a name for a book or two, Thank You.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Problem connection client to server (Unknown Source)

    Look at Sockets and ServerSockets.
    Go through the tutorial:
    The Really Big Index
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 04-28-2012, 02:11 PM
  2. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  3. nextInt(unknown source)
    By babyburger in forum New To Java
    Replies: 3
    Last Post: 03-18-2010, 04:03 PM
  4. NullPointerException at FindAppletJDKLevel(unknown source)
    By PeteMansfield in forum Java Applets
    Replies: 18
    Last Post: 09-21-2009, 06:54 PM
  5. Sql server connection problem
    By tanvirtonu in forum JDBC
    Replies: 1
    Last Post: 03-31-2008, 12:46 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
  •