Nachrichtenversenden gefixt/Test-Klassen in eigenes Paket
This commit is contained in:
parent
8f2c12dab8
commit
6f39b1ba46
7 changed files with 81 additions and 84 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,7 +17,6 @@
|
|||
package server;
|
||||
|
||||
import utils.FifoPipe;
|
||||
import java.net.Socket;
|
||||
import utils.ArrayHelper;
|
||||
import utils.Message;
|
||||
|
||||
|
@ -53,4 +52,10 @@ public class ClientMessageStore {
|
|||
public synchronized Thread removeThread(Thread thread) {
|
||||
return arrayHelper.popThread(thread, clientThreads);
|
||||
}
|
||||
|
||||
public void notifyAllClients() {
|
||||
for (int i = 0; i < clientThreads.length; i++) {
|
||||
clientThreads[i].interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,9 +17,10 @@
|
|||
package server;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.net.*;
|
||||
import utils.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -35,19 +36,27 @@ public class ServerHandelClientsThread extends Thread {
|
|||
public void run() {
|
||||
boolean continueLoop = true;
|
||||
while (continueLoop) {
|
||||
try (InputStreamReader in = new InputStreamReader(clientSocket.getInputStream())) {
|
||||
pr = new PrintWriter(clientSocket.getOutputStream());
|
||||
BufferedReader bf = new BufferedReader(in);
|
||||
for(String clientInput = bf.readLine(); clientInput != null; clientInput = bf.readLine()) {
|
||||
System.out.println("client: " + clientInput);
|
||||
pr.write("yes");
|
||||
pr.flush();
|
||||
try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
|
||||
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream())) {
|
||||
Message empfangen = null;
|
||||
while ((empfangen = (Message) in.readObject()) != null) {
|
||||
System.out.println("client: " + empfangen);
|
||||
Message message = empfangen;
|
||||
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) {
|
||||
System.err.println("test gegkückt");
|
||||
System.err.println(ex);
|
||||
pr.write("Test");
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (SocketException ex) {
|
||||
System.err.println("Socket geschlossen");
|
||||
continueLoop = false;
|
||||
} catch (IOException ex) {
|
||||
System.err.println("Exeption: " + ex);
|
||||
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
|
||||
|
|
43
src/test/MainClient.java
Normal file
43
src/test/MainClient.java
Normal 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();
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package server;
|
||||
package test;
|
||||
|
||||
import utils.ArrayHelper;
|
||||
import java.io.IOException;
|
||||
|
@ -31,7 +31,7 @@ import utils.PipeElement;
|
|||
public class PipeTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
FifoPipe pipe = new FifoPipe();
|
||||
/*FifoPipe pipe = new FifoPipe();
|
||||
System.out.println("Erstes Element: " + pipe.firstElement);
|
||||
System.out.println("Letztes Element: " + pipe.lastElement);
|
||||
Message el = new Message("Christian", "Test");
|
||||
|
@ -68,7 +68,7 @@ public class PipeTest {
|
|||
System.out.println("Letztes Element: " + pipe.lastElement);
|
||||
|
||||
ArrayHelper test = new ArrayHelper();
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
|
@ -19,6 +19,7 @@ package utils;
|
|||
/**
|
||||
*
|
||||
* @author eichehome
|
||||
* @param <T> Type des Arrays, mit dem gearbeitet wird.
|
||||
*/
|
||||
public class ArrayHelper<T> {
|
||||
|
||||
|
@ -42,7 +43,8 @@ public class ArrayHelper<T> {
|
|||
if (array != null) {
|
||||
int index = getIndex(element, array);
|
||||
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++) {
|
||||
temp[i] = array[i];
|
||||
}
|
||||
|
@ -56,10 +58,13 @@ public class ArrayHelper<T> {
|
|||
}
|
||||
|
||||
int getIndex(T match, T[] array) {
|
||||
int result = -1;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i].equals(match)) {
|
||||
return i;
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,9 +10,9 @@ public class Message implements Serializable {
|
|||
String username;
|
||||
String message;
|
||||
//String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
|
||||
LocalDateTime current = LocalDateTime.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
|
||||
String date = current.format(formatter);
|
||||
//LocalDateTime current = LocalDateTime.now();
|
||||
//DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
|
||||
//String date = current.format(formatter);
|
||||
|
||||
public Message(String username, String message) {
|
||||
this.username = username;
|
||||
|
@ -23,8 +23,8 @@ public class Message implements Serializable {
|
|||
@Override
|
||||
public String toString() {
|
||||
return "nickname=" + username
|
||||
+ ", message=" + message
|
||||
+ ", time=" + date;
|
||||
+ ", message=" + message;
|
||||
//+ ", time=" + date;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue