Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/Seventh-Assignment-Socket-Programming.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Thu May 16 14:28:24 PDT 2024
gradle.version=7.5.1
#Sun May 26 10:25:22 PDT 2024
gradle.version=8.7
Binary file modified seventh_assignment/.gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified seventh_assignment/.gradle/file-system.probe
Binary file not shown.
Binary file added seventh_assignment/.gradle/workspace-id.txt
Binary file not shown.
Binary file added seventh_assignment/.gradle/workspace-id.txt.lock
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 0 additions & 10 deletions seventh_assignment/src/main/Client/Client.java

This file was deleted.

10 changes: 0 additions & 10 deletions seventh_assignment/src/main/Server/Server.java

This file was deleted.

35 changes: 35 additions & 0 deletions seventh_assignment/src/main/java/sbu/cs/Client/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sbu.cs.Client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

// Client Class
public class Client {
// TODO: Implement the client-side operations

// TODO: Add constructor and necessary methods
private static final String SERVER_IP = "127.0.0.1";
private static final int SERVER_PORT = 3000;

public static void main(String[] args) throws IOException {
// TODO: Implement the main method to start the client
Socket client = new Socket(SERVER_IP, SERVER_PORT);
Boolean IsInChat = false;
DataOutputStream out = new DataOutputStream(client.getOutputStream());
//DataInputStream in = new DataInputStream(.getInputStream());
System.out.println("Name:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
out.writeUTF(name);
HandleServerResponse Handle = new HandleServerResponse(client);
Thread Handlethread = new Thread(Handle);
Handlethread.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while (true) {
userInput = reader.readLine();
out.writeUTF(userInput);
}
}
}
140 changes: 140 additions & 0 deletions seventh_assignment/src/main/java/sbu/cs/Client/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package sbu.cs.Client;

import sbu.cs.Server.Server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

public class ClientHandler implements Runnable {
private Socket client;
private static ArrayList<Socket> clients;
String name;
static ArrayList<String> chat = new ArrayList<>();
private static DataOutputStream out;
private DataInputStream in;
int n;
public ClientHandler(Socket client) throws IOException {
this.client = client;
this.clients = Server.clients;
this.in = new DataInputStream(client.getInputStream());
this.out = new DataOutputStream(client.getOutputStream());
}
// File directory = File.mkdirs();

File a_man_without_love = new File("src//main//java//sbu.cs//Server//data//a-man-without-love-ngelbert-Hmperdinck");
File all_of_me = new File("src//main//java//sbu.cs//Server//data//all-of-me-john-legend");
File birds_imagine = new File("src//main//java//sbu.cs//Server//data//birds-imagine-dragons");
File blinding_lights = new File("src//main//java//sbu.cs//Server//data//blinding-lights-the-weekend");
File dont_matter_to_me = new File("src//main//java//sbu.cs//Server//data//dont-matter-to-me-drake");
File feeling_in_my_body = new File("src//main//java//sbu.cs//Server//data//feeling-in-my-body-elvis");
File out_of_time = new File("src//main//java//sbu.cs//Server//data//out-of-time-the-weekend");
File something_in_the_way = new File("src//main//java//sbu.cs//Server//data//something-in-the-way-nirvana");
File why_you_wanna_trip_on_me = new File("src//main//java//sbu.cs//Server//data//why-you-wanna-trip-on-me-michael-jackson");
File you_put_a_spell_on_me = new File("src//main//java//sbu.cs//Server//data//you-put-a-spell-on-me-austin-giorgio");
@Override
public void run() {
try {
name = in.readUTF();
}
catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(name + "connected");
try {
//menu();
this.out.writeUTF("1-Join the chat");
this.out.writeUTF("2-Downloads");
this.out.writeUTF("3-Download new file");
this.out.writeUTF("Enter (E) whenever you want to return to the menu");
}
catch (IOException e) {
throw new RuntimeException(e);
}
String request;
try {
while (true) {
request = this.in.readUTF();
if (request != null) {
if (request.equals("1")) {
if (chat.size() <= 20) {
n = 0;
} else {
n = (chat.size())-20;
}
out.writeUTF("inChat");
Chat();
} else if (request.equals("2")) {

} else if (request.equals("3")) {

}
}
}
} catch (IOException e) {
// Handle any I/O exceptions that occur during communication with the client
System.err.println("IO Exception in client handler!!!!!!");
e.printStackTrace();
}
}
public void menu() throws IOException {
out.writeUTF("1-Join the chat");
out.writeUTF("2-Downloads");
out.writeUTF("3-Download new file");
String request;
try {
while (true) {
request = this.in.readUTF();
if (request != null) {
if (request.equals("1")) {
Chat();
break;
} else if (request.equals("2")) {

} else if (request.equals("3")) {

}
}
}
} catch (IOException e) {
// Handle any I/O exceptions that occur during communication with the client
System.err.println("IO Exception in client handler!!!!!!");
e.printStackTrace();
}
}
public void Chat() throws IOException {
System.out.println(name + this.in.readUTF());
out.writeUTF(" ");
out.writeUTF("Chat: ");
out.writeUTF(" ");
if (chat.size() != 0) {
for (int i = n; i < chat.size(); i++) {
out.writeUTF(chat.get(i));
}
}
String msg;
while (true) {
msg = this.in.readUTF();
if (msg != null) {
if (msg.equals("E")) {
out.writeUTF("E");
menu();
break;
} else {
String msgg = name + ": " + msg;
chat.add(msgg);
SendToAll(msgg);
}
}
}
}
private void SendToAll(String msgg) throws IOException {
for (Socket aclient : clients) {
DataOutputStream outs = new DataOutputStream(aclient.getOutputStream());
outs.writeUTF(msgg);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package sbu.cs.Client;

import java.io.Console;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public class HandleServerResponse implements Runnable{
private DataInputStream in;
private DataOutputStream out;
private Socket client;

public HandleServerResponse(Socket client) throws IOException {
this.client = client;
this.in = new DataInputStream(client.getInputStream());
this.out = new DataOutputStream(client.getOutputStream());
}

@Override
public void run() {
for (int i=0; i<4; i++) {
try {
System.out.println(this.in.readUTF());
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
try {
while (true) {
String Activity = this.in.readUTF();
if (Activity.equals("inChat")) {
out.writeUTF("is in chat");
while (true) {
String msg = this.in.readUTF();
if (msg.equals("E")) {
// Console console = System.console();
// if (console != null) {
// console.clear();
// }
for (int i=0; i<50; i++) {
System.out.println(" ");
}
for (int i=0; i<3; i++) {
System.out.println(this.in.readUTF());
}
break;
} else {
System.out.println(msg);
}
}
continue;
}
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading