Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 16th, 2006, 9:42 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
networking with sockets

I'm working on sending information to a server with my tetris application.

I have the sever and a client and in order to test the program i ran it on the same computer.

Does anyone know why it won't connect? maybe a firewall problem? I put unblock to the server part of the program when "windows firewall" gave me a prompt.

I'm using windows xp if that helps.

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

class Client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("localhost", 1234);
         BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));
         System.out.print("Received string: '");

         while (!in.ready()) {}
         System.out.println(in.readLine()); // Read one line and output it

         System.out.print("'\n");
         in.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

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

class Server {
   public static void main(String args[]) {
      String data = "Toobie ornaught toobie";
      try {
         ServerSocket srvr = new ServerSocket(1234);
         Socket skt = srvr.accept();
         System.out.print("Server has connected!\n");
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending string: '" + data + "'\n");
         out.print(data);
         out.close();
         skt.close();
         srvr.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old May 16th, 2006, 9:59 PM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Without testing it all, I can't see too much wrong. Plus, I've never worked with sockets in java before. I'm guessing that your getting "whoops, it didn't work" error that you have there. In a help to everyone else, tell is what the exception is. Does it just say it can't connect/find server?
Booooze is offline   Reply With Quote
Old May 17th, 2006, 12:34 AM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by Booooze
Without testing it all, I can't see too much wrong. Plus, I've never worked with sockets in java before. I'm guessing that your getting "whoops, it didn't work" error that you have there. In a help to everyone else, tell is what the exception is. Does it just say it can't connect/find server?
nope my error is "whoops! it didn't work"
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old May 17th, 2006, 1:25 AM   #4
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Quote:
Originally Posted by Eric the Red
nope my error is "whoops! it didn't work"
That's what you told it to do when it trys to catch an exception and succeeds. The error is stored in the variable e. Its there for a reason, not just for looks try

System.out.print(e.toString());

Then see what you get.

Capitialization matters so you might have to change it.
Booooze is offline   Reply With Quote
Old May 17th, 2006, 1:41 AM   #5
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
I ran your code on my machine and it works fine.
jaeusm is offline   Reply With Quote
Old May 17th, 2006, 2:23 AM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 936
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by jaeusm
I ran your code on my machine and it works fine.
If that's the case then I'm guessing it's a problem with your internet setup (firewall perhaps?) as you suggested. I haven't worked with sockets so I can't help you there.
titaniumdecoy is online now   Reply With Quote
Old May 17th, 2006, 5:09 AM   #7
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 406
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
And in the future, stop using try catch block without printing the error , then .... well it's pointless if can't see the error.
So always use as sugested by Booooze
System.out.print(e.toString());
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old May 17th, 2006, 7:47 AM   #8
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
Smile

Quote:
System.out.print(e.toString());
or better, for a good description
System.out.print(e.getMessage());
__________________
Visit: http://www.somaiya.edu
java_roshan is offline   Reply With Quote
Old May 17th, 2006, 9:08 AM   #9
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 406
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
or
e.printStackTrace();
i think he gets the message :p
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old May 17th, 2006, 6:11 PM   #10
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by Booooze
That's what you told it to do when it trys to catch an exception and succeeds. The error is stored in the variable e. Its there for a reason, not just for looks try

System.out.print(e.toString());

Then see what you get.

Capitialization matters so you might have to change it.
Okay.. I did what you told me too. The error is "java.net.ConnectException: Connection refused".

Now your probably thinking "turn off windows firewall" but that's done already. So any ideas?
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC