Commit 770fa7be authored by 9731301's avatar 9731301

add server client

parent d0c8f600
...@@ -2,16 +2,15 @@ ...@@ -2,16 +2,15 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment=""> <list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/Server_Client/Client.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameLoop.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/Server_Client/Server.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameState.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/ThreadPool.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/InitialFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/InitialFrame.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Main.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Main.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel$2.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel$2.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Main.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/UI/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/MainFrame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainInitialPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/MainInitialPanel.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/UI/MainInitialPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/MainInitialPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainPanel.java" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
...@@ -79,7 +78,13 @@ ...@@ -79,7 +78,13 @@
<workItem from="1593598248640" duration="34000" /> <workItem from="1593598248640" duration="34000" />
<workItem from="1593723764237" duration="20000" /> <workItem from="1593723764237" duration="20000" />
<workItem from="1595279525335" duration="2577000" /> <workItem from="1595279525335" duration="2577000" />
<workItem from="1595311289843" duration="13950000" /> <workItem from="1595311289843" duration="14991000" />
<workItem from="1595367196355" duration="1206000" />
<workItem from="1595413381205" duration="5440000" />
<workItem from="1595438548966" duration="7525000" />
<workItem from="1595496830549" duration="1042000" />
<workItem from="1595500392081" duration="9357000" />
<workItem from="1595519390600" duration="8731000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -87,34 +92,38 @@ ...@@ -87,34 +92,38 @@
<option name="version" value="1" /> <option name="version" value="1" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1595312273481"> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1595424907485">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1595312273481" /> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1595424907485" />
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1593430104059"> <state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1593430104059">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1593430104059" /> <state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1593430104059" />
<state width="1493" height="89" key="GridCell.Tab.0.bottom" timestamp="1595328847854"> <state x="593" y="0" key="CommitChangelistDialog2" timestamp="1595526973946">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="89" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" /> <state x="593" y="0" key="CommitChangelistDialog2/0.0.1536.824@0.0.1536.824" timestamp="1595526973946" />
<state width="1493" height="89" key="GridCell.Tab.0.center" timestamp="1595328847854"> <state width="1078" height="121" key="GridCell.Tab.0.bottom" timestamp="1595527014303">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="89" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" /> <state width="1078" height="121" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1595527014303" />
<state width="1493" height="89" key="GridCell.Tab.0.left" timestamp="1595328847854"> <state width="1078" height="121" key="GridCell.Tab.0.center" timestamp="1595527014303">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="89" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" /> <state width="1078" height="121" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1595527014303" />
<state width="1493" height="89" key="GridCell.Tab.0.right" timestamp="1595328847854"> <state width="1078" height="121" key="GridCell.Tab.0.left" timestamp="1595527014303">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="89" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" /> <state width="1078" height="121" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1595527014303" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1595311302683"> <state width="1078" height="121" key="GridCell.Tab.0.right" timestamp="1595527014303">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1595311302683" /> <state width="1078" height="121" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1595527014303" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1595519582789">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1595519582789" />
<state x="209" y="33" key="new project wizard" timestamp="1593723788909"> <state x="209" y="33" key="new project wizard" timestamp="1593723788909">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
...@@ -123,5 +132,13 @@ ...@@ -123,5 +132,13 @@
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="511" y="41" key="refactoring.ChangeSignatureDialog/0.0.1536.824@0.0.1536.824" timestamp="1595279815795" /> <state x="511" y="41" key="refactoring.ChangeSignatureDialog/0.0.1536.824@0.0.1536.824" timestamp="1595279815795" />
<state x="425" y="187" key="run.anything.popup" timestamp="1595509733410">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="425" y="187" key="run.anything.popup/0.0.1536.824@0.0.1536.824" timestamp="1595509733410" />
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1595522974558">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1595522974558" />
</component> </component>
</project> </project>
\ No newline at end of file
package Server_Client;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
private String user_pass;
public Client(String username, String password) {
user_pass = username + password;
}
public void run(){
try (Socket client = new Socket(InetAddress.getLocalHost().getHostName(),5757)){
OutputStream out = client.getOutputStream();
byte[] buffer = new byte[2048];
out.write(user_pass.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
package Server_Client;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private File registeredList = createAFile();
public Server() throws IOException {
}
/**
*
* @return
*/
public File runRegistration() throws IOException {
ServerSocket serverSocket = new ServerSocket(5757);
try (Socket connectionSocket = serverSocket.accept()){
System.out.println("new client connected");
OutputStream out = connectionSocket.getOutputStream();
InputStream in = connectionSocket.getInputStream();
byte[] buffer = new byte[2048];
int read = in.read(buffer);
FileOutputStream fileWriter = new FileOutputStream(registeredList);
fileWriter.write(read);
}
return null;
}
/**
*
* @param password
* @param username
* @return
* @throws IOException
*/
public boolean pass_userNameIsAvailable(String password , String username) throws IOException {
String pass_userName = password+username;
if (readFile().contains(pass_userName)){
return true;
}
return false;
}
/**
*
* @return
* @throws IOException
*/
private String readFile() throws IOException {
String fileContent = "";
FileInputStream read = new FileInputStream(registeredList);
int c;
while ((c = read.read()) != -1){
fileContent += new Character((char)c).toString();
}
return fileContent;
}
/**
*
* @return
*/
public File createAFile() throws IOException {
try {
File registeredList = new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\src\\Server_Client\\registeredName\\registeredList");
if (registeredList.createNewFile()) {
System.out.println("File created: " + registeredList.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return runRegistration();
}
}
\ No newline at end of file
...@@ -16,9 +16,9 @@ public class Main { ...@@ -16,9 +16,9 @@ public class Main {
ThreadPool.init(); ThreadPool.init();
// Show the game menu ... // Show the game menu ...
// InitialFrame initialFrame = new InitialFrame("Trouble Tank"); InitialFrame initialFrame = new InitialFrame("Trouble Tank");
System.out.println("yes"); System.out.println("yes");
// if (initialFrame.isClickOnPlay()) { if (initialFrame.isClickOnPlay()) {
// After the player clicks 'PLAY' ... // After the player clicks 'PLAY' ...
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
...@@ -37,6 +37,6 @@ public class Main { ...@@ -37,6 +37,6 @@ public class Main {
} }
}); });
// } }
} }
} }
package UI; package UI;
import Server_Client.Client;
import Server_Client.Server;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.IOException;
public class MainInitialPanel extends JPanel{ public class MainInitialPanel extends JPanel{
private boolean clickOnPlay; private boolean clickOnPlay;
private JTextField userName = new JTextField();
private JTextField pass = new JTextField();
public MainInitialPanel(){ public MainInitialPanel(){
clickOnPlay = false; clickOnPlay = false;
...@@ -26,12 +32,17 @@ public class MainInitialPanel extends JPanel{ ...@@ -26,12 +32,17 @@ public class MainInitialPanel extends JPanel{
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
super.mouseClicked(e); super.mouseClicked(e);
if (true){ try {
Server server = new Server();
if (server.pass_userNameIsAvailable( pass.getText() , userName.getText())){
clickOnPlay = true; clickOnPlay = true;
} }
else { else {
JOptionPane.showMessageDialog(logInBtn,"error"); JOptionPane.showMessageDialog(logInBtn,"error");
} }
} catch (IOException ex) {
ex.printStackTrace();
}
} }
}); });
...@@ -55,7 +66,6 @@ public class MainInitialPanel extends JPanel{ ...@@ -55,7 +66,6 @@ public class MainInitialPanel extends JPanel{
private JPanel setUserNamePanel(){ private JPanel setUserNamePanel(){
JPanel userNamePanel = new JPanel(); JPanel userNamePanel = new JPanel();
JLabel label = new JLabel("user name"); JLabel label = new JLabel("user name");
JTextField userName = new JTextField();
userName.setPreferredSize(new Dimension(userName.getWidth() + 200,userName.getHeight()+ 30)); userName.setPreferredSize(new Dimension(userName.getWidth() + 200,userName.getHeight()+ 30));
userNamePanel.add(label); userNamePanel.add(label);
userNamePanel.add(userName); userNamePanel.add(userName);
...@@ -65,7 +75,6 @@ public class MainInitialPanel extends JPanel{ ...@@ -65,7 +75,6 @@ public class MainInitialPanel extends JPanel{
private JPanel setPassPanel(){ private JPanel setPassPanel(){
JPanel passPanel = new JPanel(); JPanel passPanel = new JPanel();
JLabel label = new JLabel("password"); JLabel label = new JLabel("password");
JTextField pass = new JTextField();
pass.setPreferredSize(new Dimension(pass.getWidth() + 200,pass.getHeight()+ 30)); pass.setPreferredSize(new Dimension(pass.getWidth() + 200,pass.getHeight()+ 30));
passPanel.add(label); passPanel.add(label);
passPanel.add(pass); passPanel.add(pass);
...@@ -75,17 +84,17 @@ public class MainInitialPanel extends JPanel{ ...@@ -75,17 +84,17 @@ public class MainInitialPanel extends JPanel{
private JPanel setRegistrationPanel(){ private JPanel setRegistrationPanel(){
JPanel regPanel = new JPanel(); JPanel regPanel = new JPanel();
JPanel fieldsPanel = new JPanel(new GridLayout(4,2)); JPanel fieldsPanel = new JPanel(new GridLayout(4,2));
JLabel tankNameLabel = new JLabel("Name your tank"); JLabel username= new JLabel("username");
JTextField tankName = new JTextField(); JTextField usernameField= new JTextField();
JLabel password = new JLabel("password"); JLabel password = new JLabel("password");
JTextField passwordField = new JTextField(); JTextField passwordField = new JTextField();
JLabel passwordRp = new JLabel("password"); JLabel passwordRp = new JLabel("passwordRp");
JTextField passwordRepField = new JTextField(); JTextField passwordRepField = new JTextField();
JLabel email = new JLabel("Email address"); JLabel email = new JLabel("Email address");
JTextField emailField = new JTextField(); JTextField emailField = new JTextField("optional");
JButton saveBtn = new JButton("sign up"); JButton saveBtn = new JButton("sign up");
fieldsPanel.add(tankNameLabel); fieldsPanel.add(username);
fieldsPanel.add(tankName); fieldsPanel.add(usernameField);
fieldsPanel.add(password); fieldsPanel.add(password);
fieldsPanel.add(passwordField); fieldsPanel.add(passwordField);
fieldsPanel.add(passwordRp); fieldsPanel.add(passwordRp);
...@@ -94,6 +103,27 @@ public class MainInitialPanel extends JPanel{ ...@@ -94,6 +103,27 @@ public class MainInitialPanel extends JPanel{
fieldsPanel.add(emailField); fieldsPanel.add(emailField);
regPanel.add(fieldsPanel); regPanel.add(fieldsPanel);
regPanel.add(saveBtn); regPanel.add(saveBtn);
saveBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (usernameField.getText().equalsIgnoreCase("") || passwordField.getText().equals("") || passwordRepField.getText().equals("")){
JOptionPane.showMessageDialog(saveBtn,"you should fill blank fields.\n Email is not necessary.");
}
else if (passwordField.getText().equals(passwordRepField)){
JOptionPane.showMessageDialog(saveBtn,"password and repetition are not equal");
}
else {
try {
Server server = new Server();
} catch (IOException ex) {
ex.printStackTrace();
}
Client newClient = new Client(usernameField.getText() , passwordField.getText() );
newClient.run();
}
}
});
return regPanel; return regPanel;
} }
} }
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