Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Connection problem  (Read 2501 times)
0 Members and 1 Guest are viewing this topic.
Offline Grand Poeba

Junior Duke





« Posted 2005-12-17 21:12:17 »

Hey i am quite new to network programming

so i picked some example code from my books but i get always errors :s

I have a small client and server class
but when i want to make a new clientsocket with a addess and port i get an connection refused error

i can create a serversocket but when i ask a socket of it (by usijng accept) i get a cannot except error

i am behind a router but the port i work on is open so i cant seem to find the solution
any help?

my code:
import java.net.*;;
import java.io.*;

public class TestClient {
   public TestClient() throws IOException {
      Socket socket = null;
      PrintWriter out = null;
      BufferedReader in = null;

      try {
         socket = new Socket(InetAddress.getLocalHost(), 4444);
         System.out.println("Connected with server "
               + socket.getInetAddress() + ":" + socket.getPort());
         out = new PrintWriter(socket.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(socket
               .getInputStream()));
      } catch (UnknownHostException e) {
         System.err.println("Don't know about host.");
         System.exit(1);
      } catch (Exception e) {
         System.err
               .println("Couldn't get I/O for the connection to.");
         System.exit(1);
      }

      BufferedReader stdIn = new BufferedReader(new InputStreamReader(
            System.in));
      String fromServer;
      String fromUser;

      while ((fromServer = in.readLine()) != null) {
         System.out.println("Server: " + fromServer);
         if (fromServer.equals("Bye."))
            break;

         fromUser = stdIn.readLine();
         if (fromUser != null) {
            System.out.println("Client: " + fromUser);
            out.println(fromUser);
         }
      }

      out.close();
      in.close();
      stdIn.close();
      socket.close();
   }
}

import java.net.Socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.InetAddress;

public class TestServer {
   public TestServer() throws IOException {
      ServerSocket serverSocket = null;
      try {
         serverSocket = new ServerSocket(4444);
      } catch (IOException e) {
         System.err.println("Could not listen on port: 4444.");
         System.exit(1);
      }

      Socket clientSocket = null;
      try {
         clientSocket = serverSocket.accept();
      } catch (IOException e) {
         System.err.println("Accept failed.");
         System.exit(1);
      }

      clientSocket.close();
      serverSocket.close();
   }
}
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2005-12-17 23:19:07 »

Is this on one computer or two?

this line...
1  
socket = new Socket(InetAddress.getLocalHost(), 4444);


Will make a connection on port 4444 to the machine that this code is running on.  If your servre is running ona differnt machine, you wont get there this way.

If this is all on the same machine, how many internet connections does the machine have? if it has two or more then you may have to do more work to insure that the client and server are talkign on the same connection.  The ebst thing to do in that case is to explicitly state "127.0.0.1" for both the IP for both server socket and the client socket.

Finally, turn off your local firewall.  Under windows this will be in your security control panel. It gets in the way of a lot of networking. if this solves it, you can then go into the settings for the firewall and open the necessary prots so you can trun it back on.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Grand Poeba

Junior Duke





« Reply #2 - Posted 2005-12-18 00:22:04 »

Is this on one computer or two?

this line...
1  
socket = new Socket(InetAddress.getLocalHost(), 4444);


Will make a connection on port 4444 to the machine that this code is running on.  If your servre is running ona differnt machine, you wont get there this way.

If this is all on the same machine, how many internet connections does the machine have? if it has two or more then you may have to do more work to insure that the client and server are talkign on the same connection.  The ebst thing to do in that case is to explicitly state "127.0.0.1" for both the IP for both server socket and the client socket.

Finally, turn off your local firewall.  Under windows this will be in your security control panel. It gets in the way of a lot of networking. if this solves it, you can then go into the settings for the firewall and open the necessary prots so you can trun it back on.


these are two standalone classes/apps
i run then on 1 pc with only 1 connection
port 4444 is open in my router so it should work, dont see why
maybe windows firewall will try it later
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2005-12-18 00:25:50 »

Is this on one computer or two?

this line...
1  
socket = new Socket(InetAddress.getLocalHost(), 4444);


Will make a connection on port 4444 to the machine that this code is running on.  If your servre is running ona differnt machine, you wont get there this way.

If this is all on the same machine, how many internet connections does the machine have? if it has two or more then you may have to do more work to insure that the client and server are talkign on the same connection.  The ebst thing to do in that case is to explicitly state "127.0.0.1" for both the IP for both server socket and the client socket.

Finally, turn off your local firewall.  Under windows this will be in your security control panel. It gets in the way of a lot of networking. if this solves it, you can then go into the settings for the firewall and open the necessary prots so you can trun it back on.


these are two standalone classes/apps
i run then on 1 pc with only 1 connection
port 4444 is open in my router so it should work, dont see why
maybe windows firewall will try it later

If its on a single machine your router is irelllevent.

What about my other questions?  How many NICs does this machine have?

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Grand Poeba

Junior Duke





« Reply #4 - Posted 2005-12-18 12:38:42 »

i got it to make a connection with clientside
instead of the getlocalhost i used the standard gateway adress of my router and then it worked

anyone have an example of a lobby room?
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2005-12-18 20:57:17 »

Thats very odd.

I would say that (a) You have the port on the router mapped back to your box or it woudl never work that way and
(b)you have your networking configured badly at home. 

regards

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Grand Poeba

Junior Duke





« Reply #6 - Posted 2005-12-19 00:20:41 »

dont know about that
will test same code in linux as well

when i use localhose (127.0.0.1) is can now open socket on a port
i can also open a serversocket on an open port
but when i then try to accept() from it i get accept failed :s
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2005-12-19 04:44:19 »

Thats weird, can you show me the whole exception trace?

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Grand Poeba

Junior Duke





« Reply #8 - Posted 2005-12-19 10:33:20 »

for running testClient:

java.net.ConnectException: Connection refused: connect
   at java.net.PlainSocketImpl.socketConnect(Native Method)
   at java.net.PlainSocketImpl.doConnect(Unknown Source)
   at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
   at java.net.PlainSocketImpl.connect(Unknown Source)
   at java.net.SocksSocketImpl.connect(Unknown Source)
   at java.net.Socket.connect(Unknown Source)

for running testServer

java.net.SocketException: select failed
   at java.net.PlainSocketImpl.socketAccept(Native Method)
   at java.net.PlainSocketImpl.accept(Unknown Source)
   at java.net.ServerSocket.implAccept(Unknown Source)
   at java.net.ServerSocket.accept(Unknown Source)
Offline Grand Poeba

Junior Duke





« Reply #9 - Posted 2006-02-08 21:04:57 »

anyone still has no dieas why this doenst work Sad
just cant seem to find it
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sunsett

Senior Duke




ribbit!


« Reply #10 - Posted 2006-02-08 21:25:59 »

try this:

Server:
1  
2  
3  
ServerSocket ss = new ServerSocket(4444);
Socket s = ss.accept();
System.out.println("Client connected successfully to server!");


Client:
1  
Socket s = new Socket(InetAddress.getLocalHost(), 4444);


See if you get a connection to occur.  If that doesn't work give the examples in JGN a try. Wink

-Matt Hicks
Offline Grand Poeba

Junior Duke





« Reply #11 - Posted 2006-02-08 23:47:42 »

try this:

Server:
1  
2  
3  
ServerSocket ss = new ServerSocket(4444);
Socket s = ss.accept();
System.out.println("Client connected successfully to server!");


Client:
1  
Socket s = new Socket(InetAddress.getLocalHost(), 4444);


See if you get a connection to occur.  If that doesn't work give the examples in JGN a try. Wink

-Matt Hicks

errors but i guess got something to do with my router although the port is opened
what is JGN? Should i use Jini?
Offline sunsett

Senior Duke




ribbit!


« Reply #12 - Posted 2006-02-09 00:37:31 »

JGN = JavaGameNetworking = http://javagamenetworking.dev.java.net

Wow, as much advertising as I've been doing for it you'd think I wrote it or something. :-p

-Matt Hicks
Offline Jeff

JGO Coder




Got any cats?


« Reply #13 - Posted 2006-02-09 09:54:02 »

I can almost assure you your networking is confugred badly if you cannot even make a conenction explictly to 127.0.0.1 on the same machine.

Check to see if you have a firewall runnign on the amchine and if so disable it.  See if that helps.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Grand Poeba

Junior Duke





« Reply #14 - Posted 2006-02-09 12:09:50 »

I can almost assure you your networking is confugred badly if you cannot even make a conenction explictly to 127.0.0.1 on the same machine.

Check to see if you have a firewall runnign on the amchine and if so disable it.  See if that helps.



ServerSocket ss = new ServerSocket(4444);
Socket s = ss.accept();

I can execute the first line so i can make a connection on that port
but trying to get a socket on it fails. I only have the xp firewall but disabling it doesnt make a difference
Offline sunsett

Senior Duke




ribbit!


« Reply #15 - Posted 2006-02-09 14:19:37 »

Just for kicks try downloading JGN and running the basic test.  If that doesn't work then you've got serious problems.  If it does work then you've just got mental problems. Wink

-Matt Hicks
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!