how to run this program successfully?...this has an ERROR

import java.io.*;
import java.net.*;

class AdviceClient
{
void go()
{
try
{
Socket s = new Socket("127.0.0.1",4242);

InputStreamReader ir = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(ir);

String str=br.readLine();
System.out.println("Advice = " + str);
br.close();
}catch(Exception e)
{
e.printStackTrace();
}

}
public static void main(String args[])
{
AdviceClient obj = new AdviceClient();
obj.go();
}
}

my system is also connected to net but still error
any suggestions with reason...
ERROR:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at AdviceClient.go(AdviceClient.java:10)
at AdviceClient.main(AdviceClient.java:27)

Replies

  • shalini_goel14
    shalini_goel14
    As the program you have shown is TCP Client Socket program , so I guess because of no server connection, your specified 4242 port no is getting failed, try it with default port no 8080. I think then it will work.

    And one more thing as you have mentioned
    InputStreamReader ir = new InputStreamReader(s.getInputStream());
    Where you are giving the input. I am not clear? Please clarify ๐Ÿ˜•. If you are not giving input then how can you get input, so after using default port no 8080 you will get output as follows:
    Advice = null
    Thanks
  • xero
    xero
    I think first, you should study about sockets in java ๐Ÿ˜€. Here you have created a socket client.

    Socket s = new Socket("127.0.0.1",4242);

    means that your trying to connect to socket server which is hosted at localhost, i.e. your machine which is listening at 4242.

    This means that you have another java application acting as server. Please do refer to sun java tutorials for socket programming.

    So even if you have internet nothing's gonna happen ๐Ÿ˜€

    In case of further issues i can help you out, however have a look at tutorial !
    Do change the topic of this thread as Problem with this socket program, it will be much clearer
  • shalini_goel14
    shalini_goel14
    Hey sahilgandhi,

    Luckily this site already has something good for you. Please look at following thread and feel free to ask any questons there. In this thread links for sun java tutorials are also given. ๐Ÿ˜€

    #-Link-Snipped-#

    Thanks

You are reading an archived discussion.

Related Posts

can anyone suggest me some books which explain the various stages of game development?
This year I have started using other sites that also having the features like CE site...But CE site is much interesting and I Cannot have a mind to leave once...
i m using ms office xp but while working in word or excel files gets corrupt & sometimes it not recoverable pls suggest.... how to stop files bieng corrupted....
man m makin a voice recognition security system in matlab...can u guide me how to go about it especially the feature extraction and pattern matching part...it will be of great...
hi, what is minimum BE% for admission in M.TECH.(CS) courses offered by IIT's and IISc.anybody having under 200 rank & less then 60% in BE CS can getting into IIT....