Note that there are some explanatory texts on larger screens.

plurals
  1. POJava chat socket reading and sending data
    text
    copied!<p>I successfully developed an app which you can create/connect to a server. I have a class named Person in which I have a public static LinkedList. In this list I'm storing clients who are connected to the server. The server/client classes are in the same package. The server and the client classes are extending Threads (they are working in the background).</p> <p>What I'm trying to do.</p> <ol> <li>I want to send the online client list (LinkedList) to all clients who are connected to the server.</li> <li>I want to read data (String from StyledText) from clients and write it to the global main chat area (StyledText).</li> </ol> <p>(something like mIRC program).</p> <p>I have read and searched all day long, but I never saw a good explanation, example or documentation about "Java chat app". I found with AWT, Swing, but the problem is I have knowledge only in SWT. Also, I found tutorials with TELNET, and the Input and Output Stream was read and wrote in the console (CMD). Am I need to use InputStreamReader, BufferedStreamReader and PrintWriter? If I have, am I need to convert those data in byte array then convert them into Objects?</p> <p>I post some screenshots for a better understanding:</p> <p><img src="https://i.stack.imgur.com/GgQtB.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/NM3Kh.png" alt="enter image description here"></p> <p>This is how I create the server:</p> <pre><code>DialogServer dialog = new DialogServer(new Shell()); dialog.create(); if(dialog.open() == Window.OK){ new Server(serverAddress).start(); //dialog sets serverAddress as a String IP } </code></pre> <p><strong>Server class:</strong></p> <pre><code>public class Server extends Thread{ private static ServerSocket serverSocket; private static Socket socket; private static String address; public Server(String address){ this.address = address; } public void run(){ try { serverSocket = new ServerSocket(5000, 10, InetAddress.getByName(address)); View.serverUp = true; View.log("Server is up. Listening on " + address + ":5000"); Person p = new Person(View.nickname, true, 100.0); Person.listPeople.add(p); //add ADMIN to static LinkedList while(true){ socket = serverSocket.accept(); new Client(socket).start(); // client class thread refresOnlineList(); // refresh UI table } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void refresOnlineList(){ View.display.syncExec(new Runnable() { @Override public void run() { // TODO Auto-generated method stub View.tableViewer.refresh(); // TableViewer which contains online client list } }); } } </code></pre> <p>Dialog to connect to the server:</p> <pre><code> DialogConnect dialog = new DialogConnect(new Shell()); dialog.create(); if(dialog.open() == Window.OK){ new Client(connectAddress, connectNickname).start(); // dialog sets String parameters. new Client start a new Thread for a client. } </code></pre> <p><strong>Client class:</strong></p> <pre><code>public class Client extends Thread{ private static Socket socket; private static String nickname; public Client(Socket socket){ this.socket = socket; } public Client(String address, String nickname){ socket = new Socket(); try { socket.connect(new InetSocketAddress(address, 5000), 10); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); View.log("Cannot connect to server."); } this.nickname = nickname; } public void run(){ try { Person p = new Person(nickname, false, 15.4); Person.listPeople.add(p); // add PERSON to static LinkedList socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>Developed in Eclipse RCP 3.8</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload