티스토리 뷰
일주일동안 코딩하여 완성시킨
ProLan FileGate입니다.
아직 프로그래밍 실력이 미숙하여 알고리즘이 개떡(?)같을테니
소스는 되도록이면 보시지 않으시길 바랍니다..
client-filegate
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JOptionPane;
class filegate{
static String[] head;
static int count = 0;
static OutputStream out;
static DataOutputStream dout;
static String ver = "1.0";
public static void main(String[] args){
Socket soc=null;
String ip = null;
try{
FileInputStream fin = new FileInputStream("C:\\proLan\\filegate\\ip.txt");
Scanner s = new Scanner(fin);
ip = s.nextLine();
}catch(Exception e){
File f = new File("C:\\proLan\\filegate\\ip.txt");
try {
FileOutputStream fout = new FileOutputStream(f);
PrintStream p = new PrintStream(fout);
p.println("prolan.oa.to");
ip = "prolan.oa.to";
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try{
soc = new Socket(ip, 25566);
}catch(Exception e){
String[] test = {"확인"};
int rc = JOptionPane.showOptionDialog(null,
"서버상태 : OFF",
"ProLan FileGate!",
0,
JOptionPane.INFORMATION_MESSAGE,
null,
test,
test[0]);
System.exit(0);
}
Input_Thread.run(soc);
gui.go(soc);
}
}
client-Input_Thread
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Input_Thread{
static Socket soc;
static InputStream in;
static DataInputStream din = null;
static OutputStream out;
static DataOutputStream dout = null;
public static void run(Socket s){
try{
soc = s;
in = soc.getInputStream();
din = new DataInputStream(in);
out = soc.getOutputStream();
dout = new DataOutputStream(out);
}catch(Exception e){
}
int check = 25566;
while(true){
try{
int input = din.readInt();
switch(input){
case 0:
String[] test = {"확인"};
int rc = JOptionPane.showOptionDialog(null,
"서버에 남은자리가 없습니다!",
"ProLan FileGate!",
0,
JOptionPane.INFORMATION_MESSAGE,
null,
test,
test[0]);
System.exit(0);
case 1:
firstconn();
dout.writeInt(check);
continue;
case 2:
headlist();
dout.writeUTF(filegate.ver);
continue;
case 5:
updatefile();
dout.writeInt(check);
continue;
case 6:
firstconn2();
dout.writeInt(check);
continue;
case 7:
firstconn3();
dout.writeInt(check);
continue;
case 10:
return;
}
}catch(Exception e){
System.err.println(e);
}
}
}
public static void firstconn(){
try{
FileOutputStream fo = new FileOutputStream("C:\\ProLan\\filegate\\share3.png");
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int i = din.readInt();
for(;i>0;i--){
len=in.read(buffer);
fo.write(buffer,0,len);
bytes += len;
}
fo.close();
}catch(Exception e){
System.err.println(e);
}
}
public static void firstconn2(){
try{
FileOutputStream fo = new FileOutputStream("C:\\ProLan\\filegate\\img1.png");
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int i = din.readInt();
for(;i>0;i--){
len=in.read(buffer);
fo.write(buffer,0,len);
bytes += len;
}
}catch(Exception e){
System.err.println(e);
}
}
public static void firstconn3(){
try{
FileOutputStream fo = new FileOutputStream("C:\\ProLan\\filegate\\img2.png");
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int i = din.readInt();
for(;i>0;i--){
len=in.read(buffer);
fo.write(buffer,0,len);
bytes += len;
}
}catch(Exception e){
System.err.println(e);
}
}
public static void headlist()throws Exception{
String head;
InputStream in = soc.getInputStream();
DataInputStream din = new DataInputStream(in);
int arr = din.readInt();
filegate.head = new String[arr];
while(!(head = din.readUTF()).equalsIgnoreCase("exit")){
filegate.head[filegate.count] = head;
filegate.count++;
}
}
public static void updatefile()throws Exception{
FileOutputStream fo = new FileOutputStream("prolan_FileGate.java");
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int i = din.readInt();
for(;i>0;i--){
len=in.read(buffer);
fo.write(buffer,0,len);
bytes += len;
}
String[] test = {"확인"};
int rc = JOptionPane.showOptionDialog(null,
"업데이트가 완료되었습니다. 재시작해주십시요",
"ProLan FileGate!",
0,
JOptionPane.INFORMATION_MESSAGE,
null,
test,
test[0]);
System.exit(0);
}
}
client-gui
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;public class gui{
private static JTextField textField;
static DataOutputStream dout;
static OutputStream out;
public static void go(Socket s){
try{
out = s.getOutputStream();
dout = new DataOutputStream(out);
}catch(Exception e){
}
JFrame main = new JFrame("ProLan File Gate");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.getContentPane().setLayout(null);
JLabel label = new JLabel("\uD5E4\uB354\uD30C\uC77C : ");
label.setBounds(23, 166, 88, 15);
main.getContentPane().add(label);
main.setSize(450,300);
JLabel label_1 = new JLabel("\uD30C\uC77C\uC774\uB984 : ");
label_1.setBounds(23, 194, 65, 15);
main.getContentPane().add(label_1);
JComboBox comboBox = new JComboBox(filegate.head); //콤보박스에 name 붙임
comboBox.setBounds(96, 163, 129, 21);
main.getContentPane().add(comboBox);
textField = new JTextField();
textField.setBounds(96, 191, 129, 21);
main.getContentPane().add(textField);
textField.setColumns(10);
JButton button = new JButton("");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
String filename = textField.getText();
if(filename.equalsIgnoreCase("")){
return;
}
textField.setText("");
String header = comboBox.getSelectedItem().toString();
fileout(filename,header);
}
});
button.setForeground(Color.WHITE);
button.setIcon(new ImageIcon("C:\\proLan\\filegate\\share3.png"));
button.setBounds(237, 152, 185, 99);
main.getContentPane().add(button);
JLabel label_2 = new JLabel("");
label_2.setIcon(new ImageIcon("C:\\proLan\\filegate\\img1.png"));
label_2.setBounds(136, 10, 168, 132);
main.getContentPane().add(label_2);
JLabel label_3 = new JLabel("");
label_3.setIcon(new ImageIcon("C:\\proLan\\filegate\\img2.PNG"));
label_3.setBounds(12, 10, 410, 132);
main.getContentPane().add(label_3);
main.setVisible(true);
}
public static void fileout(String filename, String header){
try{
FileInputStream in = new FileInputStream(filename);
dout.writeInt(1);
dout.writeUTF(filename);
dout.writeUTF(header);
System.out.println(filename+" "+header);
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int data=0;
while((len = in.read(buffer))>0){
data++;
}
in.close();
in = new FileInputStream(filename);
dout.writeInt(data);
len = 0;
for(;data>0;data--){
len = in.read(buffer);
out.write(buffer,0,len);
bytes += len;
}
out.flush();
in.close();
}catch(Exception e){
String[] test = {"확인"};
int rc = JOptionPane.showOptionDialog(null, "파일전송에 실패하였습니다",
"ProLan FileGate!",
0,
JOptionPane.INFORMATION_MESSAGE,
null,
test,
test[0]);
}
}
}
server-ServerMain
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Scanner;
public class ServerMain{
static ServerSocket server;
static Socket[] client = new Socket[100];
static Socket soc;
static String[] ip;
static String ver = "1.0";
static DataInputStream din;
static InputStream in;
static File javafile = new File("C:\\Users\\김성래\\Desktop\\proLan\\save\\datafile\\prolan.java");
static File share = new File("C:\\Users\\김성래\\Desktop\\proLan\\save\\datafile\\share3.png");
static File img1 = new File("C:\\Users\\김성래\\Desktop\\proLan\\save\\datafile\\img1.png");
static File img2 = new File("C:\\Users\\김성래\\Desktop\\proLan\\save\\datafile\\img2.png");
static File ips = new File("C:\\Users\\김성래\\Desktop\\proLan\\save\\ips.txt");
static File consol;
static BufferedWriter bw,ipw;
static PrintWriter files,ipp;
public static void main(String[] args){
File fin = new File("C:\\Users\\김성래\\Desktop\\proLan\\users.txt");
gui.gui();
try{
consol = new File("C:\\Users\\김성래\\Desktop\\proLan\\save\\MainServer.txt");
bw = new BufferedWriter(new FileWriter(consol, true));
files = new PrintWriter(bw,true);
ipw = new BufferedWriter(new FileWriter(ips, true));
ipp = new PrintWriter(ipw,true);
}catch(Exception e){
e.printStackTrace();
}
try{
server = new ServerSocket(25566);
}catch(Exception e){
System.err.println(e);
}
Calendar cal = Calendar.getInstance();
String day = (cal.get(Calendar.YEAR)+"년 "+cal.get(Calendar.MONTH)+"월 "+cal.get(Calendar.DAY_OF_MONTH)+"일 "+cal.get(Calendar.HOUR_OF_DAY)+"시 : Server Start!").toString();
System.out.println(cal.get(Calendar.YEAR)+"년 "+cal.get(Calendar.MONTH)+"월 "+cal.get(Calendar.DAY_OF_MONTH)+"일 "+cal.get(Calendar.HOUR_OF_DAY)+"시 : Server Start!");
files.write("\r\n\r\n\r\n"+cal.get(Calendar.YEAR)+"년 "+cal.get(Calendar.MONTH)+"월"+cal.get(Calendar.DAY_OF_MONTH)+"일 "+cal.get(Calendar.HOUR_OF_DAY)+"시 : Server Start!\r\n");
files.flush();
gui.change(day);
while(true){
int points = 0;
try{
soc = server.accept();
System.out.println("client cannect! :" + soc.getInetAddress().getHostAddress());
gui.change("client cannect! :" + soc.getInetAddress().getHostAddress());
files.write("client cannect! :" + soc.getInetAddress().getHostAddress()+"\r\n");
files.flush();
in=soc.getInputStream();
din = new DataInputStream(in);
boolean point=false;
for(int i=0; i <100; i++){
if(client[i]==null){
point=true;
points = i;
break;
}
}
if(!point){
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
dout.writeInt(0);
System.out.println("not have list");
gui.change("not have list");
files.write("not have list"+"\r\n");
files.flush();
}
boolean firstconn=true;
try{
String ips = soc.getLocalAddress().getHostAddress();
Scanner finss = new Scanner(fin);
String read;
while(finss.hasNextLine()){
read=finss.nextLine();
if(ips.equalsIgnoreCase(read)){
firstconn=false;
break;
}
}
}catch(Exception e){
}
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
if(firstconn){
ipp.write(soc.getLocalAddress().getHostAddress()+"\r\n");
ipp.flush();
System.out.println(soc.getLocalAddress().getHostAddress()+": welecome! first guest!");
gui.change(soc.getLocalAddress().getHostAddress()+": welecome! first guest!");
files.write(soc.getLocalAddress().getHostAddress()+": welecome! first guest!"+"\r\n");
files.flush();
firstconns();
int check = din.readInt();
dout.writeInt(6);
firstconn();
check = din.readInt();
firstconnss();
check = din.readInt();
}
}catch(Exception e){
System.err.println(e);
try {
soc.close();
continue;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
headlist();
} catch (Exception e2) {
e2.printStackTrace();
}
try{
InputStream in = soc.getInputStream();
DataInputStream din = new DataInputStream(in);
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
String client_Ver = din.readUTF();
if(client_Ver.equalsIgnoreCase(ver)){
System.out.println(soc.getLocalAddress().getHostAddress()+": Ver is same");
gui.change(soc.getLocalAddress().getHostAddress()+": Ver is same");
files.write(soc.getLocalAddress().getHostAddress()+": Ver is same"+"\r\n");
files.flush();
}else{
System.out.println("Client Ver :"+client_Ver);
dout.writeInt(5);
update();
}
}catch(Exception e){
System.err.println(e);
}
try{
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
System.out.println(soc.getLocalAddress().getHostAddress()+": gui open!");
gui.change(soc.getLocalAddress().getHostAddress()+": gui open!");
files.write(soc.getLocalAddress().getHostAddress()+": gui open!"+"\r\n");
files.flush();
dout.writeInt(10);
}catch(Exception e){
}
client[points] = soc;
client_Thread s = new client_Thread(soc,points);
s.run();
}
}
public static void update()throws Exception{
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
dout.writeInt(5);
FileInputStream verfin = new FileInputStream(javafile);
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int data=0;
while((len = verfin.read(buffer))>0){
data++;
}
verfin.close();
in = new FileInputStream(javafile);
dout.writeInt(data);
long datas=0;
while((len = verfin.read(buffer))>0){
out.write(buffer,0,len);
bytes += len;
datas++;
}
System.out.println("약 "+datas+" kbps");
gui.change("약 "+datas+" kbps");
files.write("약 "+datas+" kbps"+"\r\n");
files.flush();
out.flush();
in.close();
out.flush();
}
public static void firstconns()throws Exception{
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
FileInputStream in = new FileInputStream(share);
dout.writeInt(1);
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int data=0;
while((len = in.read(buffer))>0){
data++;
}
in.close();
in = new FileInputStream(share);
dout.writeInt(data);
len = 0;
long datas=0;
while((len = in.read(buffer))>0){
out.write(buffer,0,len);
bytes += len;
datas++;
}
System.out.println("약 "+datas+" kbps");
gui.change("약 "+datas+" kbps");
files.write("약 "+datas+" kbps"+"\r\n");
files.flush();
out.flush();
in.close();
out.flush();
}
public static void firstconn()throws Exception{
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
FileInputStream in = new FileInputStream(img1);
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int data=0;
while((len = in.read(buffer))>0){
data++;
}
in.close();
in = new FileInputStream(img1);
dout.writeInt(data);
long datas=0;
while((len = in.read(buffer))>0){
out.write(buffer,0,len);
bytes += len;
datas++;
}
System.out.println("약 "+datas+" kbps");
gui.change("약 "+datas+" kbps");
files.write("약 "+datas+" kbps"+"\r\n");
files.flush();
out.flush();
in.close();
out.flush();
}
public static void firstconnss()throws Exception{
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
FileInputStream in = new FileInputStream(img2);
dout.writeInt(7);
int bytes = 0;
byte[] buffer = new byte[1024];
int len;
int data=0;
while((len = in.read(buffer))>0){
data++;
}
in.close();
in = new FileInputStream(img2);
dout.writeInt(data);
long datas=0;
while((len = in.read(buffer))>0){
out.write(buffer,0,len);
bytes += len;
datas++;
}
System.out.println("약 "+datas+" kbps");
gui.change("약 "+datas+" kbps");
files.write("약 "+datas+" kbps"+"\r\n");
files.flush();
out.flush();
in.close();
}
public static void headlist()throws Exception{
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
FileInputStream fin = new FileInputStream("C:\\Users\\김성래\\Desktop\\proLan\\headlist.txt");
dout.writeInt(2);
Scanner s = new Scanner(fin);
String header;
int count = 0;
while(s.hasNext()){
s.next();
count++;
}
fin.close();
s.close();
dout.writeInt(count);
FileInputStream fins = new FileInputStream("C:\\Users\\김성래\\Desktop\\proLan\\headlist.txt");
Scanner s2 = new Scanner(fins);
for(int i = count;i>0;i--){
header=s2.next();
dout.writeUTF(header);
}
dout.writeUTF("exit");
fins.close();
s2.close();
}
}
server-Client_Thread
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.io.DataOutputStream;
public class client_Thread extends Thread{
Socket client;
int point;
String ip;
client_Thread(Socket c, int po){
client = c;
point = po;
ip = client.getLocalAddress().getHostAddress();
}
public void run(){
int input;
InputStream in=null;
DataInputStream din = null;
OutputStream out=null;
DataOutputStream dout=null;
try{
in = client.getInputStream();
din = new DataInputStream(in);
out = client.getOutputStream();
dout = new DataOutputStream(out);
}catch(Exception e){
System.err.println(e);
return;
}
try{
while(true){
input = din.readInt();
if(input==1){
filein();
}
}
}catch(Exception e){
System.out.println(ip+": exit");
gui.change(ip+": exit");
ServerMain.files.write(ip+": exit"+"\r\n");
ServerMain.files.flush();
ServerMain.client[point] = null;
return;
}
}
public void filein(){
try{
System.out.println(ip+": filein");
gui.change(ip+": filein");
ServerMain.files.write(ip+": filein"+"\r\n");
ServerMain.files.flush();
InputStream in=null;
DataInputStream din = null;
in = client.getInputStream();
din = new DataInputStream(in);
String filename,head;
filename = din.readUTF();
head = din.readUTF();
FileOutputStream fo = new FileOutputStream("C:\\Users\\김성래\\Desktop\\proLan\\"+head+"\\"+filename);
int data = din.readInt();
int bytes = 0;
byte[] buffer = new byte[1024];
long datas=0;
int len;
for(;data >0;data--){
len=in.read(buffer);
fo.write(buffer,0,len);
bytes += len;
datas++;
}
System.out.println(ip+": "+head+"\\"+filename);
gui.change(ip+": "+head+"\\"+filename);
ServerMain.files.write(ip+": "+head+"\\"+filename+"\r\n");
ServerMain.files.flush();
System.out.println("약 "+datas+" kbps");
gui.change("약 "+datas+" kbps");
ServerMain.files.write("약 "+datas+" kbps"+"\r\n");
ServerMain.files.flush();
fo.flush();
fo.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
client-gui
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class gui {
static JFrame main;
static boolean scrollpanemove = false;
static JScrollPane scrollPane;
static JTextArea textArea;
private static JButton btnNewButton;
/**
* @wbp.parser.entryPoint
*/
public static void gui(){
main = new JFrame("Pro Lan FileGate - Server");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.getContentPane().setLayout(null);
main.setSize(450, 300);
textArea = new JTextArea();
textArea.setEnabled(false);
scrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
scrollpanemove=true;
}
});
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent arg0) {
// TODO Auto-generated method stub
if(scrollpanemove){
scrollpanemove = false;
}else{
JScrollBar src = (JScrollBar)arg0.getSource();
src.setValue(src.getMaximum());
}
}
});
scrollPane.setViewportView(textArea);
scrollPane.setBounds(12, 10, 410, 206);
main.getContentPane().add(scrollPane);
btnNewButton = new JButton("\uC11C\uBC84\uC885\uB8CC");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
btnNewButton.setBounds(12, 226, 410, 23);
main.getContentPane().add(btnNewButton);
main.setVisible(true);
}
public static void change(String i){
textArea.append("\n"+i);
scrollPane.contains(0, 10);
}
}
'잡담 > 포트폴리오' 카테고리의 다른 글
브포의 알림 서비스 (419) | 2016.02.11 |
---|---|
대덕 소프트웨어 마이스터고 안드로이드 앱 (409) | 2015.12.04 |
java - 채팅프로그램 (395) | 2015.05.31 |
java - 영어단어 암기 프로그램 (399) | 2015.04.01 |
java - 영어단어 프로그램! (404) | 2015.03.21 |