code for server
import java.io.BufferedReader;
//imports system
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class main {
public static String login;
public static String b = "bbinterjju";
public static String h = "hobag";
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
int socketPort = 3009;
ServerSocket serverSocket = new ServerSocket(socketPort); // 소켓 만들기
Socket socketUser = null; // 클라이언트 접속시 사용할 Socket
System.out.println("socket : " + socketPort + "으로 서버가 열렸습니다");
// 소켓 서버가 종료될 때까지 반복
while(true) {
socketUser = serverSocket.accept();
//client login ip address
System.out.println("Client logined ip : " + socketUser.getLocalAddress());
//catch client message&data
InputStream input = socketUser.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String login= reader.readLine();
if (login == "bbinterjju") {
OutputStream out = socketUser.getOutputStream();
PrintWriter writer = new PrintWriter(out, true);
writer.println("V");
System.out.println("logining ID : " + login);
System.out.println(" 'V' login succeded in : " + socketUser.getLocalAddress());
} else {
OutputStream out = socketUser.getOutputStream();
PrintWriter writer = new PrintWriter(out, true);
writer.println("X(ID_not_corret)");
System.out.println("logining ID : " + login);
System.out.println(" 'X' failled(No Id found) in : " + socketUser.getLocalAddress());
System.out.println("------------------------------------------------------------");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
code for client login
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class auth {
public static GUI G = new GUI();
public static yellowstrawberry_client yst_c = new yellowstrawberry_client();
public static String login;
public static String stats;
public static String star;
public static void main() throws IOException, InterruptedException{
Socket clientSocket = new Socket("127.0.0.1", 3009);
//send data to server
OutputStream out = clientSocket.getOutputStream();
PrintWriter writer = new PrintWriter(out, true);
writer.println(G.ID);
//server is sending back
InputStream input = clientSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
login = reader.readLine();
System.out.println("login(V = good/ X = bad)" + login);
if (login == "V") {
System.out.println("login good");
stats = "V";
star = "O";
} else if (login == "X(ID_not_corret)") {
stats = "No_Id_found";
star = "O";
} else if (login == "X(NOID)") {
stats = "Can't_read";
star = "O";
} else {
System.out.println("login bad");
stats = "program_can't_login";
star = "O";
}
}
}
GUI Code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class GUI extends JFrame {
yellowstrawberry_client yst_c = new yellowstrawberry_client();
auth a = new auth();
//login
public static String ID;
public static String Code;
JLabel lblTitle; // 타이틀
JLabel lblName; // 이름
JLabel lblNumber; // 번호
JTextField tfName = null; // 이름 입력창
JTextField tfNumber = null; // 번호 입력창
JButton btnSave; // 로그인 버튼
JButton btnReset; // 리셋 버튼
public GUI() {
init();
setDisplay();
showFrame();
}
private void init(){
lblTitle = new JLabel("아이디와 코드를 입력하십시오");
lblName = new JLabel("ID - 아이디");
lblNumber = new JLabel("Code - 코드");
tfName = new JTextField(10);
tfNumber = new JTextField(10);
btnSave = new JButton("로그인");
btnReset = new JButton("이 버튼은(만드는중)");
btnSave.addActionListener(new ActionListener(){ //익명클래스로 리스너 작성
public void actionPerformed(ActionEvent e){
JButton btn = (JButton) e.getSource();
if(btn.getText().equals("로그인")) {
//서버에 아이디, 코드르 줌
ID = tfName.getText();
Code = tfNumber.getText();
if (ID == "") {
ID = null;
}
try {
auth.main();
} catch (IOException | InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while (a.star == "O") {
if (a.stats == "V") {
btn.setText("로그인중..");
System.out.println("login good");
return;
} else {
btn.setText("로그인");
JOptionPane.showMessageDialog(null, "로그인에 실패 하였습니다(" + a.stats + ")", "에러", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}});
}
private void setDisplay() {
JPanel pnlNorth = new JPanel();
pnlNorth.add(lblTitle);
pnlNorth.setBorder(new TitledBorder("North"));
JPanel pnlWest = new JPanel(new GridLayout(0,1,0,10));
pnlWest.add(lblName);
pnlWest.add(lblNumber);
pnlWest.setBorder(new TitledBorder("West"));
JPanel pnlEast = new JPanel(new GridLayout(0,1,0,10));
pnlEast.add(tfName);
pnlEast.add(tfNumber);
pnlEast.setBorder(new TitledBorder("East"));
JPanel pnlSouth = new JPanel();
pnlSouth.add(btnSave);
pnlSouth.add(btnReset);
pnlSouth.setBorder(new TitledBorder("South"));
add(pnlNorth, BorderLayout.NORTH);
add(pnlWest, BorderLayout.WEST);
add(pnlEast, BorderLayout.EAST);
add(pnlSouth, BorderLayout.SOUTH);
}
private void showFrame() {
setTitle("Yellowstrawberry client");
pack();
ImageIcon img = new ImageIcon("C:/user/eunso/OneDrive/Desktop/main.png");
setIconImage(img.getImage());
setLocationRelativeTo(null);
setResizable(false); // 창을 고정
setVisible(true);
setSize(400, 250);
}
public static void main(String[] args){
new GUI();
}
public void exit() {
}
}
Code is like that and if I write bbinterjju in gui then it comes like this "Client logined ip : /127.0.0.1 logining ID : bbinterjju 'X' failled(No Id found) in : /127.0.0.1" and in gui it need say like 'login in client!' but it keep saying like 'ERROR THIS CLIENT CAN'T LOGIN THAT' please help me :(
Aucun commentaire:
Enregistrer un commentaire