File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Socket Problems :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Socket Problems :(" Watch "Socket Problems :(" New topic
Author

Socket Problems :(

Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
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 ]

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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.


    With Best Regards,
    Shyam Prasad Murarka
    Kashif Iqbal Khan
    Greenhorn

    Joined: Dec 22, 2001
    Posts: 13
    Did you start the server by issuing


    java DailyAdviceServer


    Kashif I. Khan
    Nicholas Carrier
    Ranch Hand

    Joined: Apr 14, 2005
    Posts: 78
    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 ]
    Nicholas Carrier
    Ranch Hand

    Joined: Apr 14, 2005
    Posts: 78
    I've also pinged the IP address to make sure that I can connect to it (I can).
    Nicholas Carrier
    Ranch Hand

    Joined: Apr 14, 2005
    Posts: 78
    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 ]
    Nicholas Carrier
    Ranch Hand

    Joined: Apr 14, 2005
    Posts: 78
    I just disabled it and ran it again to no avail
    Timmy Marks
    Ranch Hand

    Joined: Dec 01, 2003
    Posts: 226
    Does the System.out.println(advice) line run on the server for each connection attempt?
    Timmy Marks
    Ranch Hand

    Joined: Dec 01, 2003
    Posts: 226
    I think I have it. Your client is trying to connect to 198.192.1.102 instead of 192.168.1.102
    Nicholas Carrier
    Ranch Hand

    Joined: Apr 14, 2005
    Posts: 78
    Thank you very much.
     
     
    subject: Socket Problems :(
     
    Similar Threads
    HFJ Example problem, please help
    Chapter 15 Head First - DailyAdviceClient and DailyAdviceService
    UnknownHostException when trying to connect to localhost
    client-server error
    Client/Server Communication Problem