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 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?
 
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top