Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

PmtAce (TechnicalUser)
22 May 08 14:52
I am writing program to be used 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?   
meadandale (Programmer)
3 Aug 08 14:19
I'm not sure why you are reinventing the wheel. Java already supports using a hostname or IP address in the net/nio classes. Perhaps your address class is buggy?

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close