java - Connection refused when trying to set up a simple Client Server system -


i'm getting

exception in thread "main" java.net.connectexception: connection  

whenever try start server in simple system have setup. code copied tutorial in ran perfectly, can assume enviormental issue encountering (mac osx 10.10.5 / netbeans)

here classes:

client:

public class client {    public static void main(string [] args) throws unknownhostexception, ioexception    {        int number,temp;        scanner sc = new scanner(system.in);        socket s = new socket("127.0.0.1",1342);        scanner sc1 = new scanner(s.getinputstream());        system.out.println("print number:");        number = sc.nextint();        printstream p = new printstream(s.getoutputstream());        p.println(number);        temp = sc1.nextint();        system.out.println(temp);      } } 

server:

public class server  {     public static void main (string args[]) throws ioexception{          int number,temp;         serversocket s1 = new serversocket(1342);         socket ss= s1.accept();         scanner sc = new scanner(ss.getinputstream());         number=sc.nextint();          temp = number*2;          printstream p = new printstream(ss.getoutputstream());         p.println(temp);        } } 

does have ideas on ding wrong, or whats wrong setup?

that's happening because you're starting first client class , server.

you should first run server , afterwards client.

first go server , do: run > java application

the server starts.

then, go client , do: run > java application

the client starts , you'll see on console:

print number: 

Comments