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

sahilgandhi87

sahilgandhi87

@sahilgandhi87-jf94Wy Oct 16, 2024
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.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at AdviceClient.go(AdviceClient.java:10)
at AdviceClient.main(AdviceClient.java:27)

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • shalini_goel14

    shalini_goel14

    @shalini-goel14-ASmC2J Mar 24, 2009

    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

    @xero-28ehVN Mar 24, 2009

    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

    @shalini-goel14-ASmC2J Mar 24, 2009

    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