※ 단, 사전관리는 txt 메모장으로 관리
//서버
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class DictionaryServer {
public static void main(String args[]){
SThreadforD SThread;
ServerSocket server;
Socket sock;
try {
server = new ServerSocket(5000);
try {
while(true) {
sock = server.accept();
SThread = new SThreadforD(sock);
SThread.start();
}
}catch(IOException ioe) {
server.close();
ioe.printStackTrace();
}
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
class SThreadforD extends Thread{
Socket connection;
FileInputStream fin;
OutputStream out;
InputStream is;
BufferedReader fread,cread;
BufferedWriter writer;
String fbuf,cbuf;
public SThreadforD(Socket connection) throws UnsupportedEncodingException {
this.connection = connection;
}
public void run() {
try {
fin = new FileInputStream("dictionary.txt");
fread = new BufferedReader(new InputStreamReader(fin,"UTF-8"));//파일 내용 읽기
is = connection.getInputStream();
cread = new BufferedReader(new InputStreamReader(is,"UTF-8")); //클라이언트 전송 메세지 읽기
cbuf = cread.readLine();
out = connection.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
while(true) {
try {
while((fbuf = fread.readLine())!=null) {
int idx=fbuf.indexOf(" ");
String pre = fbuf.substring(0,idx);
String last = fbuf.substring(idx+1,fbuf.length());
int n = last.length();
//만약 파일에서 단어를 찾았다면
if(pre.equals(cbuf)) {
writer.write(last+"\r\n");
writer.flush();
break;
}
}
if(fbuf == null) {
writer.write("찾으실 단어가 없습니다."+"\r\n");
writer.flush();
break;
}
}catch(IOException e) {
System.out.println(e);
}
}
}catch(IOException e) {
System.out.println(e);
}finally {
try {
if(connection !=null)
connection.close();
}catch(IOException e) {
System.out.println(e);
}
}
}
}
//클라이언트
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class DictionaryClinet extends Frame implements ActionListener{
TextField eng,kor;
Label text;
Socket theSocket;
String host;
BufferedReader reader;
OutputStream out;
InputStream in,in2;
BufferedWriter writer;
public DictionaryClinet(){
super("Dictionary");
text = new Label();
text.setText("검색할 단어 : ");
add(text,BorderLayout.WEST);
eng = new TextField(20);
add(eng,BorderLayout.EAST);
eng.addActionListener(this);
kor = new TextField(30);
kor.setEditable(false);
add(kor,BorderLayout.SOUTH);
addWindowListener(new WinListener());
setSize(300,100);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
try {
theSocket = new Socket("localhost",5000);
out = theSocket.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(out));
String what = eng.getText()+"\r\n";
writer.write(what);
writer.flush();
in = theSocket.getInputStream();
reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String korword = reader.readLine();
kor.setText(korword);
}catch(UnknownHostException e) {
System.err.println("호스트를 찾을 수 없습니다.");
}catch(IOException e) {
System.err.println(e);
}
}
public static void main(String args[]) {
new DictionaryClinet();
}
class WinListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
//결과

'JAVA' 카테고리의 다른 글
TCP를 이용한 가위바위보 게임 (0) | 2019.12.23 |
---|---|
자바채팅프로그래밍 -UDP를 이용한 채팅프로그래밍 (0) | 2019.11.24 |
자바채팅프로그래밍 -다중채팅 (0) | 2019.11.10 |
자바채팅프로그래밍 -UDP소켓이용 계산기 (0) | 2019.11.03 |
자바채팅프로그래밍 -UDP소켓이용 채팅프로그래밍 (0) | 2019.11.03 |