This discussion is archived
3 Replies Latest reply: Mar 17, 2013 2:38 AM by 996701 RSS

Beginning Client-Server

996701 Newbie
Currently Being Moderated
hi everybody, i've got a simple program, but i've encountered a problem. Can you help me,please
--------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class Client_Server_1 {
     public static void main(String[] args){
          String hostname="localhost";
          if(args.length>0){
               hostname=args[0];
               
          }
          PrintWriter pw=null;
          BufferedReader br=null;
          try{
               Socket s= new Socket(hostname,2007);
               br=new BufferedReader(new InputStreamReader(s.getInputStream()));
               BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Connected to Server...");
               while(true){
                    String st=user.readLine();
                    if (st.equals("exit")){
                         break;
                    }
                    pw.println(st);
                    pw.flush();
                    System.out.println(br.readLine());
                    
               }
          }
          catch(IOException e){
               System.err.println(e);
          }
          
          finally{
               try{
                    if(br!=null)br.close();
                    if(pw!=null)pw.close();
               }
               catch(IOException e){
                    System.err.println(e);
                    }
               }
               
          }
     }
---------------
And Console Window:
java.net.ConnectException: Connection refused: connect
------------
I don't understand about this problem, Please explain to me. Thanh you very much!
  • 1. Re: Beginning Client-Server
    997337 Newbie
    Currently Being Moderated
    Instead of localhost give the name of Host-name which is of ur own desktop.

    You will get the Host-Name by giving the command "ipconfig/all" in cmd if ur using Microsoft OS.
  • 2. Re: Beginning Client-Server
    996701 Newbie
    Currently Being Moderated
    I've just changed the Port Number:
    ------------------
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;


    public class Client_Server_1 {
         public static void main(String[] args){
              String hostname="localhost";
              if(args.length>0){
                   hostname=args[0];
                   
              }
              PrintWriter pw=null;
              BufferedReader br=null;
              try{
                   Socket s= new Socket(hostname,445);
                   br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                   BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
                   System.out.println("Connected to Server...");
                   while(true){
                        String st=user.readLine();
                        if (st.equals("exit")){
                             break;
                        }
                        pw.println(st);
                        pw.flush();
                        System.out.println(br.readLine());
                        
                   }
              }
              catch(IOException e){
                   System.err.println(e);
              }
              
              finally{
                   try{
                        if(br!=null)br.close();
                        if(pw!=null)pw.close();
                   }
                   catch(IOException e){
                        System.err.println(e);
                        }
                   }
                   
              }
         }

    ----------------------------
    And a new problem:
    Exception in thread "main" java.lang.NullPointerException
         at Client_Server_1.main(Client_Server_1.java:27)
    -------------------
    How to fix it? (Line 27: pw.println(st);)
    Thank you for your helping!

    Edited by: 993698 on Mar 17, 2013 2:37 AM
  • 3. Re: Beginning Client-Server
    gimbal2 Guru
    Currently Being Moderated
    993698 wrote:
    ----------------------------
    And a new problem:
    Exception in thread "main" java.lang.NullPointerException
         at Client_Server_1.main(Client_Server_1.java:27)
    -------------------
    How to fix it? (Line 27: pw.println(st);)
    Thank you for your helping!
    You fix it by stop treating it like a problem. You do not have a problem. There is a mistake in your code on line 27 of your program, Java is preventing you from having problems by telling you that. It is also telling you that a variable that is on that line contains a null value and obviously it shouldn't have. So combine those two pieces of information and figure out what is wrong with your code, then fix it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points