Skip to content

Commit

Permalink
big changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre committed Nov 18, 2017
1 parent 60a4508 commit 0cb86b5
Show file tree
Hide file tree
Showing 25 changed files with 1,289 additions and 1,252 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
./out
./*.class
./*/*.class
./**/*.class
11 changes: 11 additions & 0 deletions newChatClient.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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>
Binary file modified out/production/newChatClient/Client/Client.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/newChatClient/Client/GUI$1.class
Binary file not shown.
Binary file added out/production/newChatClient/Client/GUI.class
Binary file not shown.
Binary file added out/production/newChatClient/Client/GUIApp.class
Binary file not shown.
Binary file modified out/production/newChatClient/Server/AccountHandler.class
Binary file not shown.
Binary file modified out/production/newChatClient/Server/ChatRoom.class
Binary file not shown.
Binary file modified out/production/newChatClient/Server/Server.class
Binary file not shown.
Binary file not shown.
300 changes: 108 additions & 192 deletions src/Client/Client.java
Original file line number Diff line number Diff line change
@@ -1,192 +1,108 @@
package Client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;

import static com.sun.java.accessibility.util.AWTEventMonitor.addWindowListener;
import static java.lang.Thread.sleep;

public class Client extends JFrame{
/**
* Socket handle
*/
private Socket sock;
/**
* Output stream, writes a Packet to the socket
*/
private OutputStream output;
/**
* Reads from socket, runs as its own Thread
*/
private ClientReader reader;
/**
* Username
*/
private String username;
/**
* Password
*/
private String password;
/**
* Is everything working properly?
*/
private boolean running;

private String hostaddress = "192.168.2.127";



JTextArea _textArea;
JTextField _textField;
JScrollPane _scrollPane;

/**
* Constructor
* Create socket, read from it, close it
*/
public Client() {
try {
running = true;

/**
* Create a new socket. Server localhost (192.168.2.127), port 7777
*/
sock = new Socket(InetAddress.getLocalHost(), 7777);
output = sock.getOutputStream();
reader = new ClientReader(sock, this);
Thread readerThread = new Thread(reader);
readerThread.start();

if(running()){
//sendData("FREITEXT");
}

} catch (ConnectException e) {
System.out.println("ConnectException. Chances are the server is turned off or the port is blocked or wrong.");
close();
} catch (Exception e) {
e.printStackTrace();
close();
}


addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
sendData(" has Left.");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
close();
}
});

setLayout(new BorderLayout());

_textArea = new JTextArea(20, 40);
_textArea.setLineWrap(true);
_textArea.setEditable(false);

_scrollPane = new JScrollPane(_textArea);
add(_scrollPane, BorderLayout.CENTER);

_textField = new JTextField(30);
_textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
sendData(_textField.getText());
_textField.setText("");
}
});

JButton sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
sendData(_textField.getText());
_textField.setText("");
}
});

JPanel entryPanel = new JPanel(new BorderLayout());
entryPanel.add(_textField, BorderLayout.CENTER);
entryPanel.add(sendButton, BorderLayout.EAST);
add(entryPanel, BorderLayout.SOUTH);
pack();
setVisible(true);

String address = null;
try {
address = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
_textArea.append("Connecting to: " + address + "\n");
_textArea.append("Connected\n");

sendData(address + " has Joined.");
}

/**
* Close the input stream and the socket.
*/
public void close() {
if (running) {
running = false;
try {
reader.close();
sock.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
// closed before initialized, this is OK
}
System.out.println("Closed chat.");
}
}

/**
* Send the server information
* @param data String to be sent.
*/

private void sendData(String data) {
if (!running) { return; }
try {
System.out.println("Sending " + username + " '" + data + "'");
output.write((data).getBytes());
output.flush();
} catch (IOException e) {
System.out.println("Can't write bytes"); e.printStackTrace();
close();
}
}

public void setUser(String user) {
username = user;
}

public void setPass(String pass) {
password = pass;
}

public String getUser() {
return username;
}

public String getPass() {
return password;
}

public boolean running() {
return running;
}

public static void main(String[] args) {
new Client();
}
}
package Client;

import Server.SocketListener;

import java.io.*;
import java.net.*;
import java.util.Observable;
import java.util.Observer;

public class Client extends Observable implements Observer {
/**
* Socket handle
*/
private Socket sock;
/**
* Output stream, writes a Packet to the socket
*/
private PrintWriter output;
/**
* Reads from socket, runs as its own Thread
*/
private SocketListener listener;

/**
* Username
*/
private String username;

/**
* Constructor
* Create socket, read from it, close it
*/
public Client(String serverAddress, int port) {
try {
//hostaddress = JOptionPane.showInputDialog(null, "choose a Serveraddress ");
username = InetAddress.getLocalHost().getHostAddress();

/**
* Create a new socket. Server localhost (192.168.2.127), port 7777
*/
sock = new Socket(InetAddress.getByName(serverAddress), port);
OutputStream out = sock.getOutputStream();
InputStream in = sock.getInputStream();
if (out != null && in != null) {
output = new PrintWriter(new OutputStreamWriter(out));
listener = new SocketListener(in);
}
listener.addObserver(this);
new Thread(listener).start();
} catch (ConnectException e) {
System.out.println("ConnectException. Chances are the server is turned off or the port is blocked or wrong.");
close();
} catch (Exception e) {
e.printStackTrace();
close();
}

String address = null;
try {
address = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
sendData(address + " has Joined.");
}

/**
* Close the input stream and the socket.
*/
public void close() {
try {
output.close();
sock.close();
setChanged();
notifyObservers(new CloseRequest());
} catch (SocketException e) {
// Do nothing here
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
System.out.println("Closed chat.");
}

/**
* Send the server information
* @param data String to be sent.
*/

public void sendData(String data){
System.out.println("Sending " + username + " '" + data + "'");
output.println(data);
output.flush();
if (data.startsWith("$DISCONNECT")) {
close();
}
}

@Override
public void update(Observable o, Object arg) {
if(o instanceof SocketListener) {
setChanged();
notifyObservers((String) arg);
}

}
}
Loading

0 comments on commit 0cb86b5

Please sign in to comment.