What does the following stack trace means and how can I correct my codes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    What does the following stack trace means and how can I correct my codes?

    In this programs i want my client to connect to the server.I run my server,till now it's ok but when I run my client,I get the java.net.Connec tException and the stach trace is something like that :
    java.net.Connec tException: Connection refused: connect
    at java.net.PlainS ocketImpl.socke tConnect(Native Method)
    at java.net.PlainS ocketImpl.doCon nect(Unknown Source)
    at java.net.PlainS ocketImpl.conne ctToAddress(Unk nown Source)
    at java.net.PlainS ocketImpl.conne ct(Unknown Source)
    at java.net.SocksS ocketImpl.conne ct(Unknown Source)
    at java.net.Socket .connect(Unknow n Source)
    at java.net.Socket .connect(Unknow n Source)
    at java.net.Socket .<init>(Unknow n Source)
    at java.net.Socket .<init>(Unknow n Source)
    at client1.create_ client(client1. java:34)
    at client1.main(cl ient1.java:17)


    client1 class
    <code>

    import java.net.*;
    import java.io.*;
    import javax.swing.JTe xtField;
    import java.awt.List;
    import java.util.Scann er;
    /**
    *
    * @author vimal
    */
    public class client1 {

    public static void main(String[] args){

    System.out.prin tln("Client test1 successful");
    client1 vimal=new client1();
    vimal.create_cl ient("localhost ",3005);
    }

    public void create_client(S tring hostname,int port){


    Socket client_socket;
    BufferedReader from_server;
    PrintWriter to_server;
    Scanner user_input=new Scanner(System. in);
    String str;


    try
    {


    client_socket=n ew Socket(hostname ,port);

    from_server=new BufferedReader( new InputStreamRead er(client_socke t.getInputStrea m()));
    to_server=new PrintWriter(cli ent_socket.getO utputStream(),t rue);

    String client_log;
    to_server.print ln(user_input.n extLine());

    while(((str=fro m_server.readLi ne() )!=null )){
    System.out.prin tln(hostname+" says : "+str);
    client_log=user _input.nextLine ();

    System.out.prin tln("Client says : "+client_lo g);

    to_server.print ln(client_log);
    }

    client_socket.c lose();
    from_server.clo se();
    to_server.close ();

    }
    catch(IOExcepti on e)
    {
    e.printStackTra ce();
    }
    }

    }
    </code>



    server1 class
    <code>
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */


    import java.net.*;
    import java.io.*;
    import java.util.Scann er;

    /**
    *
    * @author vimal
    */
    public class server1 {


    public static void main(String[] args){
    server2 vimal=new server2();
    vimal.create_se rver();
    }
    }

    class server2 implements Runnable{
    int port;
    //String host=null;
    PrintWriter to_client=null;
    BufferedReader from_client=nul l;
    ServerSocket server=null;
    Socket client=null;

    server2(int port){
    this.port=port;
    //this.host=host;
    }

    server2(){

    }


    public void create_server() {

    Socket client=null;

    try
    {
    server=new ServerSocket(po rt);
    while(true)
    {
    client=server.a ccept();
    Runnable sr=new server2(3005);
    Thread thr=new Thread(sr);
    thr.start();

    }

    }
    catch(IOExcepti on e)
    {
    System.err.prin tln(e.getMessag e());
    }
    }


    public void run(){
    String str;


    try{
    to_client=new PrintWriter(cli ent.getOutputSt ream(),true);
    from_client=new BufferedReader( new InputStreamRead er(client.getIn putStream()));

    while(((str=fro m_client.readLi ne())!=null) && (!(str.equals(" quit"))))
    {


    to_client.print ln(str);


    }

    client.close();
    to_client.close ();
    from_client.clo se();
    }
    catch(IOExcepti on e){
    System.err.prin tln(e.getMessag e());
    }


    }
    }

    </code>


    What is wrong wd these codes?

    With regards
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    @phpuser123,

    Please use code tags around your programs in the future. It's simple, just place a line the words "code" and "/code" in square brackets above and below your code.

    Your problem is that your server refused the IP connection, right?

    That means that your server isn't running, it's running on a different port, or there's a firewall in the way, or maybe you got the server's IP address wrong.

    Luck!

    Comment

    Working...