Skip to content

Commit 731e46a

Browse files
committed
⬆️ Init
1 parent 25e129d commit 731e46a

9 files changed

+344
-0
lines changed

Java/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

Java/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ChatThreadServer</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.5
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.5

Java/bin/Server/ClientHandler.class

3.93 KB
Binary file not shown.
5.71 KB
Binary file not shown.

Java/bin/Server/Server.class

466 Bytes
Binary file not shown.

Java/src/Server/ClientHandler.java

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package Server;
2+
3+
import java.io.DataInputStream;
4+
import java.io.IOException;
5+
import java.io.PrintStream;
6+
import java.net.Socket;
7+
import java.net.SocketException;
8+
import java.util.ArrayList;
9+
10+
public class ClientHandler extends Thread {
11+
12+
private ConnectionHandler connectionHandler;
13+
14+
private Socket conn;
15+
16+
private Boolean isActive = true;
17+
18+
private String username,userInput;
19+
20+
private DataInputStream in;
21+
22+
private PrintStream out;
23+
24+
private String userListResponse = "";
25+
26+
public ClientHandler(ConnectionHandler cHandler,Socket conn) {
27+
try{
28+
this.connectionHandler = cHandler;
29+
this.conn = conn;
30+
super.start();
31+
//get socket writing and reading streams
32+
in = new DataInputStream(conn.getInputStream());
33+
out = new PrintStream(conn.getOutputStream());
34+
}catch(Exception e){
35+
e.printStackTrace();
36+
}
37+
}
38+
39+
public void run(){
40+
while(isActive){
41+
try{
42+
if(username == null){
43+
out.println("Type your Username:");
44+
while((username = in.readLine()) != null){
45+
if(connectionHandler.getClients().containsKey(username)){
46+
System.out.println("You cannot chose this username");
47+
}
48+
else if(!username.equals("")){
49+
System.out.println("Username accepted");
50+
System.out.println(username);
51+
connectionHandler.addClient(username,this);
52+
break;
53+
}else{
54+
System.out.println("No Username chosen.");
55+
}
56+
}
57+
}
58+
59+
while((userInput = in.readLine()) != null && !userInput.equals(".")){
60+
if(userInput.equals("")){
61+
System.out.println("The Commands");
62+
break;
63+
}else if(userInput.equals("/M")){
64+
if(connectionHandler.getClients().size() > 1){
65+
String message,receiver;
66+
while((receiver = in.readLine()) != null){
67+
System.out.println("SENDER / " + receiver);
68+
while((message = in.readLine()) != null){
69+
connectionHandler.sendMessage(this,receiver,username + ": " + message);
70+
break;
71+
}
72+
break;
73+
}
74+
}else{
75+
out.println("/M");
76+
out.println("[NOBODY IS HERE]");
77+
}
78+
break;
79+
}else if(userInput.equals("/EXIT")){
80+
try{
81+
connectionHandler.removeClient(username);
82+
conn.close();
83+
isActive = false;
84+
}catch(Exception e){
85+
e.printStackTrace();
86+
}
87+
}
88+
}
89+
}catch(SocketException se){
90+
try {
91+
se.printStackTrace();
92+
connectionHandler.removeClient(username);
93+
conn.close();
94+
isActive = false;
95+
} catch (IOException e) {
96+
e.printStackTrace();
97+
}
98+
}catch(Exception e){
99+
System.out.println(e);
100+
}
101+
}
102+
}
103+
104+
public void sendUserList(ArrayList<String> ul){
105+
out.println("/GAU");
106+
for (int i = 0; i < ul.size(); i++) {
107+
userListResponse += (ul.get(i) + ":") ;
108+
}
109+
out.println(userListResponse + "[END]");
110+
userListResponse = "";
111+
}
112+
113+
public void sendMessage(String msg){
114+
115+
out.println("/M");
116+
out.println(msg);
117+
118+
}
119+
120+
public void terminate(){
121+
isActive = false;
122+
}
123+
124+
public String getUsername() {
125+
return username;
126+
}
127+
128+
public void setUsername(String username) {
129+
this.username = username;
130+
}
131+
132+
}
+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package Server;
2+
3+
import java.io.BufferedReader;
4+
import java.io.DataInputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.io.PrintStream;
8+
import java.io.PrintWriter;
9+
import java.net.InetAddress;
10+
import java.net.ServerSocket;
11+
import java.net.Socket;
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.Iterator;
15+
16+
public class ConnectionHandler extends Thread {
17+
18+
private HashMap<String,ClientHandler> clients = new HashMap<String,ClientHandler>(); //Username,Client
19+
20+
private ArrayList<String> userList = new ArrayList<String>();
21+
22+
private Boolean isActive = true;
23+
24+
private Boolean newUser = true;
25+
26+
private ServerSocket s;
27+
private String host = "Admin-PC";
28+
private int portNumber;
29+
private PrintWriter s_out;
30+
private BufferedReader s_in;
31+
private String serverName;
32+
private Socket conn;
33+
34+
public ConnectionHandler(){
35+
portNumber = 5000;
36+
serverName = "Test";
37+
super.start();
38+
}
39+
40+
public ConnectionHandler(String serverName,int portNumber){
41+
this.portNumber = portNumber;
42+
this.serverName = serverName;
43+
super.start();
44+
}
45+
46+
public void run(){
47+
while(isActive){
48+
try{
49+
50+
s = null;
51+
conn = null;
52+
53+
try{
54+
//1. creating a server socket - 1st parameter is port number and 2nd is the backlog
55+
InetAddress addr = InetAddress.getByName("Admin-PC");
56+
57+
s = new ServerSocket(portNumber , 10, addr);
58+
s.setReuseAddress(true);
59+
System.out.println("InetAddress : " + s.getInetAddress());
60+
61+
while(true){
62+
if(newUser){
63+
64+
newUser = false;
65+
}else{
66+
//get the connection socket
67+
conn = s.accept();
68+
69+
System.out.println("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());
70+
71+
//create new thread to handle client
72+
new ClientHandler(this,conn);
73+
}
74+
}
75+
}
76+
catch(IOException e){
77+
System.err.println("IOException");
78+
}
79+
//2. close the connections and stream
80+
try{
81+
s.close();
82+
}catch(IOException ioException){
83+
System.err.println("Unable to close. IOexception");
84+
}
85+
}catch(Exception e){
86+
System.out.println(e);
87+
}
88+
}
89+
}
90+
91+
public void test(PrintStream output){
92+
output.println("TEST");
93+
}
94+
95+
public void sendMessage(ClientHandler sender,String clientRCVR,String message) throws IOException{
96+
OutputStream outstream = conn.getOutputStream();
97+
PrintWriter out = new PrintWriter(outstream);
98+
if(clients.size() > 1){
99+
if(clientRCVR.equals("GLOBAL")){
100+
message = "[GLOBAL] " + message;
101+
for (Iterator it = clients.keySet().iterator(); it.hasNext(); ){
102+
Object key = it.next();
103+
ClientHandler client = clients.get(key);
104+
client.sendMessage(message);
105+
}
106+
}else{
107+
try{
108+
System.out.println("Contain PLZ: " + clients.containsKey(clientRCVR));
109+
if(clients.containsKey(clientRCVR)){
110+
message = "[PRIVATE] " + message;
111+
clients.get(clientRCVR).sendMessage(message);
112+
}else{
113+
message = "[MESSAGE COULD NOT BE SENDED]";
114+
sender.sendMessage(message);
115+
}
116+
}
117+
catch(Exception e){
118+
e.printStackTrace();
119+
}
120+
}
121+
}
122+
else{
123+
message = "[MESSAGE COULD NOT BE SENDED]";
124+
sender.sendMessage(message);
125+
}
126+
}
127+
128+
public synchronized void addClient(String username, ClientHandler ch){
129+
clients.put(username,ch);
130+
informAllClientsUserlist();
131+
}
132+
133+
public synchronized void removeClient(String username){
134+
clients.remove(username);
135+
informAllClientsUserlist();
136+
}
137+
138+
public void informAllClientsUserlist(){
139+
fillUserList();
140+
for (Iterator it = clients.keySet().iterator(); it.hasNext(); ){
141+
Object key = it.next();
142+
ClientHandler client = clients.get(key);
143+
client.sendUserList(userList);
144+
}
145+
}
146+
147+
private void fillUserList(){
148+
userList.clear();
149+
for (Iterator it = clients.keySet().iterator(); it.hasNext(); ){
150+
Object key = it.next();
151+
ClientHandler client = clients.get(key);
152+
userList.add(client.getUsername());
153+
}
154+
}
155+
156+
public void terminate(){
157+
isActive = false;
158+
}
159+
160+
public synchronized HashMap<String, ClientHandler> getClients() {
161+
return clients;
162+
}
163+
164+
public synchronized void setClients(HashMap<String, ClientHandler> clients) {
165+
this.clients = clients;
166+
}
167+
168+
169+
}

Java/src/Server/Server.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package Server;
2+
3+
public class Server {
4+
5+
public static void main(String[] args) {
6+
ConnectionHandler cH = new ConnectionHandler();
7+
}
8+
9+
}

0 commit comments

Comments
 (0)