Nachrichtenversenden gefixt/Test-Klassen in eigenes Paket

This commit is contained in:
eichehome 2021-05-14 17:44:48 +02:00
parent 8f2c12dab8
commit 6f39b1ba46
7 changed files with 81 additions and 84 deletions

View file

@ -1,65 +0,0 @@
package client;
import java.io.*;
import java.net.*;
public class MainClient {
static Socket server = null;
static PrintWriter pr = null;
static BufferedReader bf = null;
static InputStreamReader in = null;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
server = new Socket("localhost", 1236);
pr = new PrintWriter(server.getOutputStream());
pr.println("is it working?");
pr.flush();
in = new InputStreamReader(server.getInputStream());
bf = new BufferedReader(in);
String str = bf.readLine();
System.out.println("server: " + str);
try {
System.out.println("Sleepin for 5 seconds");
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Thread is interrupted");
}
} catch (IOException e) {
System.err.println("Fehler: " + e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.err.println("Fehler: " + e);
}
}
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
System.err.println("Fehler: " + e);
}
}
if (pr != null) {
pr.close();
}
if (server != null) {
try {
server.close();
} catch (IOException e) {
System.err.println("Fehler: " + e);
}
}
}
}
}

View file

@ -17,7 +17,6 @@
package server; package server;
import utils.FifoPipe; import utils.FifoPipe;
import java.net.Socket;
import utils.ArrayHelper; import utils.ArrayHelper;
import utils.Message; import utils.Message;
@ -53,4 +52,10 @@ public class ClientMessageStore {
public synchronized Thread removeThread(Thread thread) { public synchronized Thread removeThread(Thread thread) {
return arrayHelper.popThread(thread, clientThreads); return arrayHelper.popThread(thread, clientThreads);
} }
public void notifyAllClients() {
for (int i = 0; i < clientThreads.length; i++) {
clientThreads[i].interrupt();
}
}
} }

View file

@ -17,9 +17,10 @@
package server; package server;
import java.io.*; import java.io.*;
import java.net.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.net.*;
import utils.Message;
/** /**
* *
@ -35,19 +36,27 @@ public class ServerHandelClientsThread extends Thread {
public void run() { public void run() {
boolean continueLoop = true; boolean continueLoop = true;
while (continueLoop) { while (continueLoop) {
try (InputStreamReader in = new InputStreamReader(clientSocket.getInputStream())) { try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
pr = new PrintWriter(clientSocket.getOutputStream()); ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream())) {
BufferedReader bf = new BufferedReader(in); Message empfangen = null;
for(String clientInput = bf.readLine(); clientInput != null; clientInput = bf.readLine()) { while ((empfangen = (Message) in.readObject()) != null) {
System.out.println("client: " + clientInput); System.out.println("client: " + empfangen);
pr.write("yes"); Message message = empfangen;
pr.flush(); out.writeObject(message);
out.flush();
} }
} catch (ClassNotFoundException ex) {
System.err.println(ex);
//for(String clientInput = in.readLine(); clientInput != null; clientInput = in.readLine()) {
//}
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
System.err.println("test gegkückt"); System.err.println("test gegkückt");
System.err.println(ex); System.err.println(ex);
pr.write("Test"); pr.write("Test");
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (SocketException ex) {
System.err.println("Socket geschlossen");
continueLoop = false;
} catch (IOException ex) { } catch (IOException ex) {
System.err.println("Exeption: " + ex); System.err.println("Exeption: " + ex);
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);

43
src/test/MainClient.java Normal file
View file

@ -0,0 +1,43 @@
package test;
import java.io.*;
import java.net.*;
import utils.Message;
public class MainClient {
public MainClient() throws IOException, ClassNotFoundException {
System.out.println("[CLIENT] START");
Socket socket = new Socket( "localhost", 1236 );
ObjectOutputStream oboust = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream obinstr = new ObjectInputStream(socket.getInputStream());
System.out.println("[CLIENT] START1");
Message senden = new Message( "berdan", "test" );
System.out.println(senden); //nickname=berdan, message=test
// Message senden
oboust.writeObject(senden);
oboust.flush();
System.out.println("[CLIENT] NACHRICHT GESENDET");
// Message erhalten
Object erhalten = null;
while(erhalten==null){
erhalten = obinstr.readObject();
}
System.out.println("[CLIENT] NACHRICHT ERHALTEN");
System.out.println(erhalten); //nickname=berdan, message=test
oboust.close();
obinstr.close();
socket.close();
}
public static void main(String args[]) throws IOException, ClassNotFoundException {
new MainClient();
}
}

View file

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package server; package test;
import utils.ArrayHelper; import utils.ArrayHelper;
import java.io.IOException; import java.io.IOException;
@ -31,7 +31,7 @@ import utils.PipeElement;
public class PipeTest { public class PipeTest {
public static void main(String[] args) { public static void main(String[] args) {
FifoPipe pipe = new FifoPipe(); /*FifoPipe pipe = new FifoPipe();
System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Erstes Element: " + pipe.firstElement);
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
Message el = new Message("Christian", "Test"); Message el = new Message("Christian", "Test");
@ -68,7 +68,7 @@ public class PipeTest {
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
ArrayHelper test = new ArrayHelper(); ArrayHelper test = new ArrayHelper();
*/
} }
} }

View file

@ -19,6 +19,7 @@ package utils;
/** /**
* *
* @author eichehome * @author eichehome
* @param <T> Type des Arrays, mit dem gearbeitet wird.
*/ */
public class ArrayHelper<T> { public class ArrayHelper<T> {
@ -42,7 +43,8 @@ public class ArrayHelper<T> {
if (array != null) { if (array != null) {
int index = getIndex(element, array); int index = getIndex(element, array);
T result = array[index]; T result = array[index];
T[] temp = new T[2 - array.length]; T[] temp;
temp = new T[array.length - 2];
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {
temp[i] = array[i]; temp[i] = array[i];
} }
@ -56,10 +58,13 @@ public class ArrayHelper<T> {
} }
int getIndex(T match, T[] array) { int getIndex(T match, T[] array) {
int result = -1;
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
if (array[i].equals(match)) { if (array[i].equals(match)) {
return i; result = i;
break;
} }
} }
return result;
} }
} }

View file

@ -10,9 +10,9 @@ public class Message implements Serializable {
String username; String username;
String message; String message;
//String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); //String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
LocalDateTime current = LocalDateTime.now(); //LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); //DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String date = current.format(formatter); //String date = current.format(formatter);
public Message(String username, String message) { public Message(String username, String message) {
this.username = username; this.username = username;
@ -23,8 +23,8 @@ public class Message implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "nickname=" + username return "nickname=" + username
+ ", message=" + message + ", message=" + message;
+ ", time=" + date; //+ ", time=" + date;
} }
} }