• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Socket Problems :(

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I just disabled it and ran it again to no avail
     
    Ranch Hand
    Posts: 226
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic