From 6f61fbeebe8c214960fa6a7430b41262fe79d8fc Mon Sep 17 00:00:00 2001 From: BerdanInformatik123 Date: Thu, 13 May 2021 19:40:22 +0200 Subject: [PATCH] Neue Class "Client1" mit Message Object --- src/client/Client1.java | 68 +++++++++++++++++++++++++++++++++++++ src/client/MainClient.java | 23 ++----------- src/client/MainClient1.java | 3 ++ 3 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 src/client/Client1.java diff --git a/src/client/Client1.java b/src/client/Client1.java new file mode 100644 index 0000000..7628846 --- /dev/null +++ b/src/client/Client1.java @@ -0,0 +1,68 @@ +package client; + +/** + * @author berdan + */ + + +import java.net.*; +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; + + + +public class Client1 { + public Client1() throws IOException, ClassNotFoundException { + Socket socket = new Socket( "localhost", 1236 ); + ObjectOutputStream oboust = new ObjectOutputStream(socket.getOutputStream()); + ObjectInputStream obinstr = new ObjectInputStream(socket.getInputStream()); + + Message senden = new Message( "berdan", "test" ); + System.out.println(senden); + + // Message senden + oboust.writeObject(senden); + oboust.flush(); + + // Message erhalten + Object erhalten = obinstr.readObject(); + System.out.println(erhalten); + + oboust.close(); + obinstr.close(); + } + + public static void main(String args[]) throws IOException, ClassNotFoundException { + new Client1(); + } +} + + + +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); + + + public Message( String username, String message ){ + this.username = username; + this.message = message; + //this.date = date; + } + + @Override + public String toString() { + return "nickname=" + username + + ", message=" + message + + ", time=" + date; + } + +} \ No newline at end of file diff --git a/src/client/MainClient.java b/src/client/MainClient.java index 3a7f6e2..b719911 100644 --- a/src/client/MainClient.java +++ b/src/client/MainClient.java @@ -1,29 +1,10 @@ -/* - * Copyright (C) 2021 eichehome - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ + package client; import java.io.*; import java.net.*; -/** - * Hauptklasse des Clients - * @version 0.0.1 - * @author eichehome - */ + public class MainClient { static Socket server = null; diff --git a/src/client/MainClient1.java b/src/client/MainClient1.java index 3760182..db2c678 100644 --- a/src/client/MainClient1.java +++ b/src/client/MainClient1.java @@ -24,6 +24,9 @@ import java.net.*; * @version 0.0.1 * @author eichehome */ + + + public class MainClient1 { static Socket server = null;