I'm starting out with a little chat client to get used to networking and sockets and stuff.

I managed to get the code to work when its done on the same machine, but when the code runs on another machine, the following code:

SocketChannel schannel = SocketChannel.open(address);

does not run. Does anyone know why? The code doesn't throw an exception when this line runs, it just doesn't do anything.

Code:

import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.io.*; import java.nio.*; import java.nio.channels.*; import java.net.*; import static java.nio.charset.StandardCharsets.UTF_8; import java.util.concurrent.Executors; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.*; public class TestClient // Chat Room TIME { static Scanner scanner = new Scanner(System.in); class Scene{ boolean running = true; private BufferedReader reader; private PrintWriter writer; public String moniker; String m; public void go(){ System.out.println("Please input your moniker:"); moniker = scanner.nextLine(); if(!Network()) { System.out.println("Failed to connect"); return; } ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(new ServiceReader(this)); try { while (running) { String input = scanner.nextLine(); if (input.equalsIgnoreCase("/quit")) { running = false; break; } SendChat(input); } } catch (Exception e) { e.printStackTrace(); } finally { executor.shutdownNow(); } } public BufferedReader getReader(){ return reader; } public boolean Network(){ try{ System.out.println("Which port? Please input an integer"); String Stringport = scanner.nextLine(); int intport = Integer.valueOf(Stringport); System.out.println("What is the IP address of the Server?"); String ipa = scanner.nextLine(); System.out.println("Details Received"); InetSocketAddress address = new InetSocketAddress(ipa, intport); System.out.println("Address Created"); SocketChannel schannel = SocketChannel.open(address); //THE PROBLEM IS HERE! System.out.println("Creating Socket"); writer = new PrintWriter(Channels.newWriter(schannel, UTF_8)); System.out.println("Writer Opened"); reader = new BufferedReader(Channels.newReader(schannel, UTF_8)); System.out.println("Reader Opened"); System.out.println("Networking Established, finished."); return true; } catch (Exception e){ e.printStackTrace(); return false; } } public void SendChat(String input){ if(writer == null) { System.out.println("Not Connected to Server"); return; } String m = moniker+": "+input; writer.println(m); writer.flush(); if (writer.checkError()) { System.out.println("Connection lost while sending."); running = false; } } } public class ServiceReader implements Runnable{ private Scene scene; private BufferedReader reader; public void run(){ reader = scene.getReader(); String message; try{ while ((message = reader.readLine()) != null){ System.out.println(message); } } catch (Exception e){ e.printStackTrace(); } } public ServiceReader(Scene scene){ this.scene = scene; this.reader = scene.getReader(); } } public static void main(String[] args) { TestClient outer = new TestClient(); Scene scene = outer.new Scene(); scene.go(); } } //

user207421's user avatar

user207421

312k45 gold badges324 silver badges493 bronze badges

JovenlyCosmo's user avatar

New contributor

JovenlyCosmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.