Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket wont connect to other computers

Status
Not open for further replies.

PmtAce

Technical User
Apr 6, 2006
17
US
I am writing a multiplayer game to be played over a lan network. I am trying to get one computer connect to a server that is located on another computer. When the server and client are on the same computer, one can connect to the other. However, when trying to connect to another computer, the connection attempt fails.

SocketManager that handles interface with sockets: client and server built up from use of this class:

public class SocketManager {

/** Creates a new instance of SocketManager */
public SocketManager(String aHost, String aPort, boolean aIsServer) {
host = aHost;
port = aPort;
isServer = aIsServer;
try{
if(isServer){
server = ServerSocketChannel.open();
server.configureBlocking(false);
server.socket().bind(new InetSocketAddress(Address.getByAddress(host),Integer.valueOf(port)));
}else{
socket = SocketChannel.open();
socket.configureBlocking(false);
}
}catch(IOException e){
throw new RuntimeException(e);
}
}

public SocketManager(String aHost, String aPort,SocketChannel channel) {
host = aHost;
port = aPort;
isServer = false;
socket = channel;
}

public boolean isConnected(){
if(socket != null)
return socket.isConnected();
return false;
}

public SocketManager accept(){
if(isServer){
try{
SocketChannel a = server.accept();
if(a != null){
return new SocketManager(host,port,a);
}else{
return null;
}
}catch(IOException e){
throw new RuntimeException(e);
}
}
return null;
}

public String getHost(){
if(isServer){
return server.socket().getInetAddress().getHostAddress();
}
return null;
}

public boolean connect(){
if(!isServer){
try{
socket.connect(new InetSocketAddress(Address.getByAddress(host),Integer.valueOf(port)));
return socket.finishConnect();
}catch(IOException e){
throw new RuntimeException(e);
}
}
return false;
}

public boolean write(String message){
if(!isServer){
try{
ByteBuffer b = ByteBuffer.wrap(message.getBytes());
socket.write(b);
return true;
}catch(IOException e){
throw new RuntimeException(e);
}
}
return false;
}

public String read(){
if(!isServer){
try{
ByteBuffer b = ByteBuffer.allocate(10000);
socket.read(b);
b.flip();
Charset charset = Charset.forName( "us-ascii" );
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode( b );
String result = charBuffer.toString();
if(result.length()>1){
return result;
}
return null;
}catch(IOException e){
throw new RuntimeException(e);
}
}
return null;
}

public static String getLocal(){
try{
return InetAddress.getLocalHost().getHostAddress();
}catch(UnknownHostException e){

}
return null;
}

protected ServerSocketChannel server;
protected SocketChannel socket;

protected String host;
protected String port;
protected boolean isServer;

}

The Address class is just a class that converts a string ip address ("192.168.1.1") to a byte[] address

I have no idea why it will allow me to connect to the same computer and not another. I already tried turning firewalls off and it didnt work. I tried adding/checking the system securitymanager but there was none to begin with. I thought this code worked before but I guess not. Anybody have any ideas?
 
Some ideas:

- Look for exceptions and post the stacktrace.
- If this is an applet, you need to sign it.
- Check IP connectiviy between computers

Cheers,
Dian
 
Thanks for the response.
No exceptions are thrown and therefore no stack trace.
When trying to connect it just simply doesnt.
It is not an applet and I believe the connection between the computers is fine.
 
I found a way to get it to work. Unfortunately it is not the desired method. I put the sockets in non-blocking mode so that the program wouldnt freeze while it waited for information from the server or during connection. For the section that connected I put the socket temporarily back in blocking mode which allowed me to connect to other computers. I still have no idea what the problem was/ how to fix it, but I have found a temporary solution.

Oddly enough, when running the program line by line in debugging sessions, the program would connect in the desired way from time to time. I am not sure why this happened, maybe the extra time allowed the computer to connect? Any ideas? Id still rather see my program run completely non-blocking. Thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top