Commit 83a401d8 authored by Ali Shakoori's avatar Ali Shakoori

Final Commit Of ApLab97-lab10 with socket

parents
Pipeline #677 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/GUI.iml" filepath="$PROJECT_DIR$/GUI.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?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" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import javax.swing.*;
import java.awt.*;
public class ChatArea extends JTextArea {
private static final int ROWS = 10 , COLUMN = 30;
public ChatArea(){
super(ROWS , COLUMN);
this.setEditable(false);
this.setLineWrap(true);
setVisible(true);
}
public void addMessage(String userName , String Message){
this.append(userName + " : " + Message + "\n");
}
}
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
public class ChatRoomGUI extends JFrame {
private final String WINDOWS_TITLE = "AUT Chat Room";
private final int WIDTH = 500, HEIGHT = 500;
private final int X = 100, Y = 100;
private ChatArea chatBox = new ChatArea();
private JPanel users = new JPanel();
private ParticipantArea user = new ParticipantArea();
private MessageArea messageArea = new MessageArea();
private JPanel msgArea = new JPanel();
public ChatRoomGUI(){
super();
this.setTitle(WINDOWS_TITLE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setLocation(X, Y);
this.add(new JScrollPane().add(chatBox) ,BorderLayout.CENTER );
chatBox.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
users.setLayout(new BorderLayout());
users.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
users.add(user.getLabel() , BorderLayout.PAGE_START);
users.add(user.getList() , BorderLayout.CENTER);
this.add(users , BorderLayout.WEST);
msgArea.setLayout(new BorderLayout());
msgArea.add(new JScrollPane().add(messageArea) , BorderLayout.CENTER);
msgArea.add(messageArea.getBtn() , BorderLayout.EAST);
this.add(msgArea , BorderLayout.PAGE_END);
users.setVisible(true);
this.setVisible(true);
}
public MessageArea getMessageArea() {
return messageArea;
}
public void addNewMessage(String name , String message){
chatBox.addMessage(name , message);
}
public void addNewParticipant(String name){
user.addUser(name);
}
public Boolean removeParticipant(String name){
if(user.getListOfUsers().contains(name)){
user.getListOfUsers().removeElement(name);
return true;
}
else{
return false;
}
}
}
import networking.Network;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Logic implements ActionListener {
private static UsernameFrame usernameFrame;
@Override
public void actionPerformed(ActionEvent e) {
String user = usernameFrame.textField.getText();
usernameFrame.setVisible(false);
ChatRoomGUI chatRoom = new ChatRoomGUI();
chatRoom.addNewParticipant(user);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = chatRoom.getMessageArea().getText();
Network network = new Network();
network.send(text);
try {
chatRoom.addNewMessage(user , network.receive());
} catch (IOException e1) {
e1.printStackTrace();
}
chatRoom.getMessageArea().setText("");
}
};
chatRoom.getMessageArea().addActionListener(listener);
chatRoom.getMessageArea().getBtn().addActionListener(listener);
}
public static void main(String[] args) {
Logic.usernameFrame = new UsernameFrame();
}
}
import javax.swing.*;
public class MessageArea extends JTextField {
private JButton btn;
private final static String BTN_TXT = "Send";
public MessageArea(){
super();
btn = new JButton(BTN_TXT);
this.setEditable(true);
this.setVisible(true);
}
public JButton getBtn() {
return btn;
}
}
import javax.swing.*;
public class ParticipantArea extends JList {
private final static String LBL_TXT = "Online People: ";
private JLabel label;
private DefaultListModel<String> listOfUsers = new DefaultListModel<>();
private JList<String> list = new JList<>(listOfUsers);
public ParticipantArea(){
label = new JLabel(LBL_TXT);
this.setVisible(true);
}
public JLabel getLabel() {
return label;
}
public JList<String> getList() {
return list;
}
public void addUser(String name){
listOfUsers.addElement(name);
}
public DefaultListModel<String> getListOfUsers() {
return listOfUsers;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UsernameFrame extends JFrame {
public static final String BTN_TXT = " Start Chatting ...";
public static final String LABEL_TXT = " Choose Your UserName ";
private static final int WIDTH = 300, HEIGHT = 100;
JTextField textField;
JButton btn;
public UsernameFrame() throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel("Choose Your UserName");
add(label , BorderLayout.PAGE_START);
textField = new JTextField();
add(textField , BorderLayout.CENTER);
btn = new JButton("Start Chatting ...");
add(btn , BorderLayout.PAGE_END);
setSize(WIDTH , HEIGHT);
setVisible(true);
Logic logic = new Logic();
btn.addActionListener(logic);
textField.addActionListener(logic);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
package networking;
public class Main {
public static void main(String[] args) {
Network n = new Network();
n.send("Hi");
}
}
package networking;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Network {
Socket socket;
InetAddress address = null;
public Network(){
try {
address = InetAddress.getByName("localhost");
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
socket = new Socket(address , 1234);
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String m){
try {
PrintStream print = new PrintStream(socket.getOutputStream());
print.println(m);
}
catch (Exception e){
e.printStackTrace();
}
}
public String receive() throws IOException {
Scanner scanner = new Scanner(socket.getInputStream());
return scanner.next();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ServerSide.iml" filepath="$PROJECT_DIR$/ServerSide.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="edf3d70f-fbcb-4cea-ac9a-176e02b9638b" name="Default Changelist" comment="" />
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/ChatRoomServer.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="34">
<caret line="5" selection-start-line="5" selection-end-line="5" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="119">
<caret line="7" column="8" selection-start-line="7" selection-start-column="8" selection-end-line="7" selection-end-column="8" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/ClientHandler.java" />
<option value="$PROJECT_DIR$/src/Main.java" />
<option value="$PROJECT_DIR$/src/ChatRoomServer.java" />
</list>
</option>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="870" />
<option name="y" value="25" />
<option name="width" value="965" />
<option name="height" value="970" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="ServerSide" type="b2602c69:ProjectViewProjectNode" />
<item name="ServerSide" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
<pane id="PackagesPane" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1559138432025" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="ServerSide" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="edf3d70f-fbcb-4cea-ac9a-176e02b9638b" name="Default Changelist" comment="" />
<created>1558874822179</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1558874822179</updated>
<workItem from="1558874826015" duration="5082000" />
<workItem from="1558967788540" duration="1951000" />
<workItem from="1559122896330" duration="2717000" />
<workItem from="1559136714844" duration="1525000" />
<workItem from="1559218949333" duration="97000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="11372000" />
</component>
<component name="ToolWindowManager">
<frame x="696" y="20" width="772" height="776" extended-state="0" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.5505618" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="UI Designer" order="4" />
<window_info id="Capture Tool" order="5" />
<window_info id="Favorites" order="6" side_tool="true" />
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" weight="0.36024845" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Version Control" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Database Changes" order="8" show_stripe_button="false" />
<window_info anchor="bottom" id="Terminal" order="9" />
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info anchor="bottom" id="Messages" order="11" />
<window_info anchor="right" id="Maven" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Capture Analysis" order="4" />
<window_info anchor="right" id="Database" order="5" />
<window_info anchor="right" id="Theme Preview" order="6" />
<window_info anchor="right" id="Palette&#9;" order="7" />
<window_info anchor="right" id="Maven Projects" order="8" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/ClientHandler.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
<caret line="4" column="25" selection-start-line="4" selection-start-column="25" selection-end-line="4" selection-end-column="25" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/../../CapitalizerServer.java" />
<entry file="file://$PROJECT_DIR$/src/ChatRoomServer.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="34">
<caret line="5" selection-start-line="5" selection-end-line="5" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="119">
<caret line="7" column="8" selection-start-line="7" selection-start-column="8" selection-end-line="7" selection-end-column="8" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
</project>
\ No newline at end of file
<?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
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
class ChatRoomServer{
ChatRoomServer(Socket s) throws IOException {
Scanner get = new Scanner(s.getInputStream());
PrintStream pri = new PrintStream(s.getOutputStream());
pri.println(get.next());
}
}
\ No newline at end of file
import java.io.IOException;
import java.net.Socket;
public class ClientHandler{
Socket socket = new Socket("127.0.0.1" , 1234);
public ClientHandler() throws IOException {
}
}
\ No newline at end of file
import java.io.IOException;
import java.net.ServerSocket;
public class Main{
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
while (true){
System.out.println("Server is listening");
new ChatRoomServer(serverSocket.accept());
}
}
}
\ No newline at end of file
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