This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket Problems :(

 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Kashif Iqbal Khan
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you start the server by issuing


    java DailyAdviceServer
     
    Nicholas Carrier
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've also pinged the IP address to make sure that I can connect to it (I can).
     
    Nicholas Carrier
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just disabled it and ran it again to no avail
     
    Timmy Marks
    Ranch Hand
    Posts: 226
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does the System.out.println(advice) line run on the server for each connection attempt?
     
    Timmy Marks
    Ranch Hand
    Posts: 226
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic