• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

a big problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.net.*;
import java.io.*;
import java.util.*;
public class Client1{
public static void main(String[] args){
String server="localhost";
int port=80;

try{
Socket socket=new Socket(server,port);
BufferedReader inputStream=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter outputStream=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
outputStream.println("Hello");
System.out.println(inputStream.readLine());
socket.close();
Server s=new Server();
}
catch(ConnectException e){System.out.println(e);}
catch(UnknownHostException e){System.out.println(e);}
catch(IOException e){System.out.println(e);}

}
}
when i execute this programme i got an error message"java.net.ConnectionException:Connection refused:connect".Telll me whats the problem with this programme.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception means, very simply, that there's no server on port 80 of the local host when the client opens a socket.
The line
Server s=new Server();
serves no apparent purpose here. I'm guessing that Server is a class that listens on port 80, and this client is supposed to connect to it. If that's the case, then perhaps you should try creating the server before trying to connect to it.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the previous post states there is no web server running on port 80 for you to create a socket connection to. Because of this the exception is being thrown. Try a different server for example www.yahoo.com etc..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic