Commit abfb1e0b authored by Till Hennig Kobbe's avatar Till Hennig Kobbe
Browse files

Init -> Server sendet Nachrichten an alle verbundenen Clients außer den Sender

parents
<?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>
\ No newline at end of file
package voip.client;
public class Client {
public static void main(String args[]) {
}
}
package voip.server;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private static final String HOST = "localhost";
private static final int PORT = 8080;
private List<ServerThread> clients = new ArrayList<>();
/**
* Accepts a connection on the given port and returns the connected socket.
*
* @param port port for the connection
* @return connected socket
* @throws IOException
*/
public Socket acceptConnection(int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Accepted client");
ServerThread client = new ServerThread(socket, this, clients.size());
clients.add(client);
client.start();
}
}
public static void main(String[] args) {
Server server = new Server();
try {
server.acceptConnection(Integer.parseInt(args[0]));
} catch (IOException exception) {
System.out.println("Fehler beim akzeptieren der Verbindung");
}
}
/**
* This function writes the message to all client sockets except the given id
* @param message
*/
public void writeToAllOther(String message, int id) {
for (ServerThread client : clients) {
if (id == client.getId()) continue;
Socket socket = client.getSocket();
DataOutputStream out = null;
try {
out = new DataOutputStream(socket.getOutputStream());
out.writeBytes(message + "\n\r");
out.flush();
} catch (IOException exception) {
exception.printStackTrace();
continue;
}
}
}
}
package voip.server;
import java.io.*;
import java.net.Socket;
/**
* This class handles incoming messages from the client
*/
public class ServerThread extends Thread {
private Socket socket;
private Server server;
private InputStream inputStream;
private int id;
public ServerThread(Socket socket, Server server, int id) {
this.socket = socket;
this.server = server;
this.id = id;
try {
this.inputStream = socket.getInputStream();
} catch (IOException exception) {
System.err.println("Error getting Streams");
return;
}
}
public Socket getSocket() {
return socket;
}
public long getId() {
return id;
}
@Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.inputStream));
String line = null;
while (true) {
try {
line = reader.readLine();
} catch (IOException exception) {
exception.printStackTrace();
}
if (line != null) {
server.writeToAllOther(line, this.id);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment