Skip to content

Commit

Permalink
added functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorTrojan committed Jan 13, 2022
1 parent b7c2b12 commit 9008e97
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 278 deletions.
2 changes: 1 addition & 1 deletion build/built-jar.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sun, 03 Oct 2021 22:43:46 +0200
#Thu, 13 Jan 2022 20:10:24 +0100


C\:\\Users\\Anix\\Documents\\Coding\\Java\\Projects\\SocketChatApp=
Empty file.
Empty file.
220 changes: 0 additions & 220 deletions build/classes/socketchatapp/Main.form

This file was deleted.

2 changes: 1 addition & 1 deletion nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/Network.java</file>
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/Client.java</file>
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/Server.java</file>
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/UserManager.java</file>
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/User.java</file>
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/UserManager.java</file>
<file>file:/C:/Users/Anix/Documents/Coding/Java/Projects/SocketChatApp/src/socketchatapp/Main.java</file>
</group>
</open-files>
Expand Down
47 changes: 28 additions & 19 deletions src/socketchatapp/Client.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
package socketchatapp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client extends Network {

public Client(String ip, int port) {
this.port = port;
this.ip = ip;
run();
init();
}

public void sendMSG(String msg) {
out.println(msg);
out.flush();
}

public void receivedMSG(String msg) {
Main.instance.addMessage("> " + msg);
}

public void run() {
public void init() {
try {
initSocket(new Socket(ip, port));
out.println(Main.instance.getNAME());
new Thread(() -> {
listener();
run();
}).start();

} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
Main.instance.addMessage("[!] Failed to connect to Server!");
Main.instance.switchAll(true);
}
}
}

public void run() {
String res;
Scanner sc = new Scanner(in);
while (sc.hasNextLine()) {
res = sc.nextLine();
receivedMSG(res);
}
// end of Thread
sc.close();
Main.instance.addMessage("Disconnected!");
Main.instance.switchAll(true);
}

public void sendMSG(String msg) {
out.println(msg);
out.flush();
}

public void receivedMSG(String msg) {
Main.instance.addMessage("> " + msg);
}
}
20 changes: 12 additions & 8 deletions src/socketchatapp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
pack();
}// </editor-fold>//GEN-END:initComponents

public void disableAll() {
NAME.setEnabled(false);
IP.setEnabled(false);
PORT.setEnabled(false);
JOIN.setEnabled(false);
STARTSERVER.setEnabled(false);
public void switchAll(boolean flag) {
NAME.setEnabled(flag);
IP.setEnabled(flag);
PORT.setEnabled(flag);
JOIN.setEnabled(flag);
STARTSERVER.setEnabled(flag);
}

public void addMessage(String msg) {
Expand All @@ -169,12 +169,16 @@ public String getNAME() {
}

private void JOINActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JOINActionPerformed
disableAll();
if(getNAME().isEmpty()){
addMessage("[!] Please Enter a Name!");
return;
}
switchAll(false);
client = new Client(IP.getText(), Integer.parseInt(PORT.getText()));
}//GEN-LAST:event_JOINActionPerformed

private void STARTSERVERActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_STARTSERVERActionPerformed
disableAll();
switchAll(false);
isServer = true;
server = new Server(Integer.parseInt(PORT.getText()));
//PLAYERCOUNT.setText("1/10");
Expand Down
17 changes: 1 addition & 16 deletions src/socketchatapp/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ public void initStreams() {
}
}

public void listener() {
try {
String res;
while ((res = in.readLine()) != null) {
receivedMSG(res);
}
} catch (IOException ex) {
Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void openServerSocket() {
try {
serverSocket = new ServerSocket(port);
Expand Down Expand Up @@ -74,8 +63,4 @@ public void closeServerSocket() {
Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void receivedMSG(String s) {
System.out.println("MSG: " + s);
}
}
}
Loading

0 comments on commit 9008e97

Please sign in to comment.