Forums Register Login

Socket Problems :(

+Pie Number of slices to send: Send
Hello I'm getting a strange runtime error. Everything is compiling fine, and I'm using a an IP address behind a router (I'm guessing this is the problem) 192.168.1.102, however when I first ran it, my firewall asked me if I wanted to grant this program access on both computers, to which I replied with yes of course. Anyways, I'm guessing this is a hard ware problem and not a code problem, anyway, here is the error message.

java.netConnectException: Connetion timed out: connect
at java.net.PlainSocketImp1.socketConnetion(Native Method)
at java.net.PlainSocketImp1.doConnect(Unknown Source)
at java.net.PlainSocketImp1.connectToAddress(Unkown Source)
at java.net.PlainSocketImp1.connect(Unknown Source)
at java.net.SocksSocketImp1.connect(Unknown Source)
at java.net.SocketImp1.connect(Unknown Soure)
at java.net.SocketImp1.connect(Unknown Soure)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at DailyAdvieClient.go(DailyAdviceClient.java:10)
at DailyAdvieClient.main(DailyAdvieClient.java:31)

Here is the client code

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

public class DailyAdviceClient {

public static void main(String[] args) {

DailyAdviceClient client = new DailyAdviceClient();
client.go();
} // end of main

public void go() {

try{

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

InputStreamReader stream = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(stream);

String advice = reader.readLine();
System.out.println("Today you should: " + advice);

reader.close();

} catch(IOException e) {

e.printStackTrace();

} // end of catch

} // end of go

} // end of class

and here is the server code

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

public class DailyAdviceServer {

String[] adviceList = {"Rent a gun, buy a bullet", "People don't like you", "Your ugly", "Today is your lucky day", "ask for a promotion", "call your boss an ass",};

public static void main(String[] args) {

DailyAdviceServer server = new DailyAdviceServer();
server.go();

} // end of main

public void go () {

try {

ServerSocket serverSock = new ServerSocket(4242);

while(true) {

Socket sock = serverSock.accept();

PrintWriter writer = new PrintWriter(sock.getOutputStream());
String advice = getAdvice();
writer.println(advice);
writer.close();
System.out.println(advice);

} // end of while

} catch(IOException e) {

e.printStackTrace();

} // end of try catch

} // end of go

public String getAdvice () {

int random = (int) (Math.random() * adviceList.length);
return adviceList[random];

} // end of method

} // end of class

If anyone has any idea let me know? Any help will be greatly apprciated. Maybe it's something about running it behind a router or something, but I don't know.

[ June 07, 2005: Message edited by: Nicholas Carrier ]
[ June 07, 2005: Message edited by: Nicholas Carrier ]
+Pie Number of slices to send: Send
Dear Readers,
I have faced similar problems many times. But in my case this used to come whenever I tried to connect to the Server
  • when the Server was not Started
  • or the Server had crashed.


  • Basically when the Server was NOT RUNNING.
    +Pie Number of slices to send: Send
    Did you start the server by issuing


    java DailyAdviceServer
    +Pie Number of slices to send: Send
    yes I did, and it's just sitting there, running, any idea? Infact I have to hit ctrl C after awhile because it just sits there, waiting.
    [ June 07, 2005: Message edited by: Nicholas Carrier ]
    +Pie Number of slices to send: Send
    I've also pinged the IP address to make sure that I can connect to it (I can).
    +Pie Number of slices to send: Send
    Also just so you guys know how I'm going about it. I'm running the server code first, once that has started, I go to the other computer, and then run the client code.

    I'm also running Windows XP and I've tried disabling my firewall and then running it to no avail.

    [ June 08, 2005: Message edited by: Nicholas Carrier ]
    [ June 08, 2005: Message edited by: Nicholas Carrier ]
    +Pie Number of slices to send: Send
    I just disabled it and ran it again to no avail
    +Pie Number of slices to send: Send
    Does the System.out.println(advice) line run on the server for each connection attempt?
    +Pie Number of slices to send: Send
    I think I have it. Your client is trying to connect to 198.192.1.102 instead of 192.168.1.102
    +Pie Number of slices to send: Send
    Thank you very much.
    If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1525 times.
    Similar Threads
    UnknownHostException when trying to connect to localhost
    client-server error
    Chapter 15 Head First - DailyAdviceClient and DailyAdviceService
    HFJ Example problem, please help
    Client/Server Communication Problem
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 05:27:31.