forked from tgeier/online-chat-ueb3
Timo Geier
7 years ago
commit
d2d48e8eab
5 changed files with 249 additions and 0 deletions
-
12Online-Chat-Dev.iml
-
68src/com/vertsys/Client.java
-
41src/com/vertsys/ClientThread.java
-
65src/com/vertsys/Server.java
-
63src/com/vertsys/ServerThread.java
@ -0,0 +1,12 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="JAVA_MODULE" version="4"> |
||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true"> |
||||
|
<exclude-output /> |
||||
|
<content url="file://$MODULE_DIR$"> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> |
||||
|
</content> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
</component> |
||||
|
</module> |
||||
|
|
@ -0,0 +1,68 @@ |
|||||
|
package com.vertsys; |
||||
|
|
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.DataOutputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.net.Socket; |
||||
|
import java.util.Random; |
||||
|
|
||||
|
public class Client { |
||||
|
|
||||
|
protected static Socket sock; |
||||
|
private static String username; |
||||
|
private static BufferedReader ConsoleIn; |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
init(); |
||||
|
do { |
||||
|
try { |
||||
|
String msg = ConsoleIn.readLine(); |
||||
|
sendMessage(msg); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
}while (true); |
||||
|
} |
||||
|
|
||||
|
public static void init(){ |
||||
|
String hostname = "localhost"; |
||||
|
int port = 42424; |
||||
|
|
||||
|
// Falls die Vergabe eines Benutzernamen fehlschlägt --> "test" + Zufallszahl |
||||
|
Random rand = new Random(); |
||||
|
username = "test" + Integer.toString(rand.nextInt(4)); |
||||
|
try { |
||||
|
ConsoleIn = new BufferedReader(new InputStreamReader(System.in)); |
||||
|
System.out.print("Your Username: "); |
||||
|
String user = ConsoleIn.readLine(); |
||||
|
if (user != "") |
||||
|
username = user; |
||||
|
System.out.print("Enter Server hostname: "); |
||||
|
hostname = ConsoleIn.readLine(); |
||||
|
System.out.print("Enter port number: "); |
||||
|
port = Integer.parseInt(ConsoleIn.readLine()); |
||||
|
}catch (IOException e){ |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
try { |
||||
|
sock = new Socket(hostname, port); |
||||
|
System.out.println("Connection established!"); |
||||
|
System.out.println("--------------------------------"); |
||||
|
System.out.println(""); |
||||
|
new Thread(new ClientThread()).start(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void sendMessage(String message){ |
||||
|
String moddedMessage = username + " wrote: " + message + "\n"; |
||||
|
try { |
||||
|
DataOutputStream toServer = new DataOutputStream(sock.getOutputStream()); |
||||
|
toServer.writeBytes(moddedMessage); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
package com.vertsys; |
||||
|
|
||||
|
//import static com.vertsys.Server.sock; |
||||
|
|
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.io.ObjectInputStream; |
||||
|
import java.util.ArrayList; |
||||
|
|
||||
|
|
||||
|
public class ClientThread extends Client implements Runnable{ |
||||
|
|
||||
|
@Override |
||||
|
public void run() { |
||||
|
ArrayList<String> msgs = new ArrayList<>(); |
||||
|
try { |
||||
|
ObjectInputStream allMessagesStream = new ObjectInputStream(sock.getInputStream()); |
||||
|
msgs = (ArrayList<String>) allMessagesStream.readObject(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} catch (ClassNotFoundException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
msgs.forEach(msg -> { |
||||
|
System.out.println(msg); |
||||
|
}); |
||||
|
|
||||
|
while(!sock.isClosed()){ |
||||
|
try { |
||||
|
BufferedReader fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream())); |
||||
|
String newMessage = fromServer.readLine(); |
||||
|
System.out.println(newMessage); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,65 @@ |
|||||
|
package com.vertsys; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.net.ServerSocket; |
||||
|
import java.net.Socket; |
||||
|
import java.util.ArrayList; |
||||
|
|
||||
|
|
||||
|
public class Server { |
||||
|
protected static ServerSocket sock; |
||||
|
protected static ArrayList<String> messages = new ArrayList<>(); |
||||
|
protected static ArrayList<Socket> clients = new ArrayList<>(); |
||||
|
// protected static ArrayList<String> messages = new ArrayList<>(); |
||||
|
protected static int port; |
||||
|
|
||||
|
protected static boolean DEBUG_MODE = true; |
||||
|
|
||||
|
public static void main(String[] args){ |
||||
|
init(args); |
||||
|
try { |
||||
|
sock = new ServerSocket(port); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
while (true){ |
||||
|
if (DEBUG_MODE) |
||||
|
System.out.println("Socket opened... Waiting for connections"); |
||||
|
try { |
||||
|
Socket connSocket = sock.accept(); |
||||
|
clients.add(connSocket); |
||||
|
new Thread(new ServerThread(connSocket)).start(); |
||||
|
} catch (IOException e){ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static void init(String[] args){ |
||||
|
|
||||
|
switch (args.length) { |
||||
|
case 0: |
||||
|
if (DEBUG_MODE) |
||||
|
System.out.println("No Parameters. Set port to default values..."); |
||||
|
port = 42424; |
||||
|
break; |
||||
|
case 1: |
||||
|
if (DEBUG_MODE) |
||||
|
System.out.println("Port as Parameter."); |
||||
|
port = Integer.parseInt(args[1]); |
||||
|
if (port < 0) { |
||||
|
System.err.println("ERROR: Port must be a value >= 0!"); |
||||
|
System.out.println("Usage: java ConcatServer [port]"); |
||||
|
System.exit(-1); |
||||
|
} |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
System.out.println("Usage: java ConcatServer [port]"); |
||||
|
System.exit(0); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,63 @@ |
|||||
|
package com.vertsys; |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.net.Socket; |
||||
|
|
||||
|
public class ServerThread extends Server implements Runnable{ |
||||
|
|
||||
|
private Socket sock; |
||||
|
|
||||
|
public ServerThread(Socket sock){ |
||||
|
this.sock = sock; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void run() { |
||||
|
if (DEBUG_MODE) |
||||
|
System.out.println("New connection --> New Thread started."); |
||||
|
if(DEBUG_MODE) |
||||
|
System.out.println("Send all received messages to new client..."); |
||||
|
try { |
||||
|
ObjectOutputStream toNewClient = new ObjectOutputStream(sock.getOutputStream()); |
||||
|
toNewClient.writeObject(messages); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
while(!sock.isClosed()){ |
||||
|
System.out.println("Still running..."); |
||||
|
this.addMessage(); |
||||
|
} |
||||
|
if(DEBUG_MODE) |
||||
|
System.out.println("Socket closed --> Joining Thread..."); |
||||
|
} |
||||
|
|
||||
|
private void addMessage(){ |
||||
|
String newMessage = ""; |
||||
|
try { |
||||
|
newMessage = readMessage(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
if (DEBUG_MODE) |
||||
|
System.out.println("Add new Message..."); |
||||
|
messages.add(newMessage); |
||||
|
if(DEBUG_MODE) |
||||
|
System.out.println(newMessage); |
||||
|
String finalNewMessage = newMessage; |
||||
|
clients.forEach(socket -> { |
||||
|
DataOutputStream outgoing = null; |
||||
|
|
||||
|
try { |
||||
|
outgoing = new DataOutputStream(socket.getOutputStream()); |
||||
|
outgoing.writeBytes(finalNewMessage + "\n"); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
}); |
||||
|
} |
||||
|
private String readMessage() throws IOException { |
||||
|
BufferedReader incoming = new BufferedReader(new InputStreamReader(sock.getInputStream())); |
||||
|
return incoming.readLine(); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue