Chat

응용 SoftWare/JAVA 2016. 12. 28. 13:20

server 화면


import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;


public class ServerChat implements Runnable{

ArrayList<ServerService> arrayList = new ArrayList<ServerService>();

ServerSocket ss;

public ServerChat() {

try{

ss = new ServerSocket(9999);

}catch(Exception e){

System.out.println(e.getMessage());

}

Thread t = new Thread(this);

t.start();

}

//접속자 대기

public void run(){

System.out.println("Server Start!");

while(true){

try{

Socket s = ss.accept(); //접속 대기

//System.out.println("접속됨");

ServerService sService = new ServerService(s);

arrayList.add(sService);

messageAll(s.getInetAddress()+" 님이 접속하였습니다.");

sService.start();

}catch(Exception e){

System.out.println(e.getMessage());

}

}

}

//모든 접속자에게 접속 메세지 보내기

public void messageAll(String message){

for(int i=0; i<arrayList.size(); i++){

ServerService sss = arrayList.get(i);

sss.pw.println(message);

sss.pw.flush();

}

}

//내부클래스

class ServerService extends Thread{

Socket s;

InputStreamReader isr;

BufferedReader br;

OutputStreamWriter os;

PrintWriter pw;

ServerService(Socket s){

try{

isr = new InputStreamReader(s.getInputStream());

br = new BufferedReader(isr);

os = new OutputStreamWriter(s.getOutputStream());

pw = new PrintWriter(os);

}catch(Exception e){

System.out.println(e.getMessage());

}

}

public void run(){

}

public static void main(String[] args) {

new ServerChat();

}


}

=====================================================================
client 화면

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ClientChat extends JFrame implements ActionListener, Runnable{
JPanel northPane = new JPanel();
JLabel urlLbl = new JLabel("URL ");
JTextField connTf = new JTextField(20);
JButton connBtn = new JButton("접속");
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
JPanel southPane = new JPanel(); 
JTextField sendTf = new JTextField(20);
JButton sendBtn = new JButton("보내기");
//////
InetAddress ia;
Socket s;
InputStreamReader isr;
BufferedReader br;
OutputStreamWriter osw;
PrintWriter pw;
public ClientChat() {
northPane.add(urlLbl);
northPane.add(connTf);
northPane.add(connBtn);
add(northPane, "North");
Font fnt = new Font("Serif", Font.BOLD, 15);
ta.setFont(fnt);
ta.setBackground(Color.white);
add(ta, "Center");
southPane.add(sendTf);
southPane.add(sendBtn);
add(southPane, "South");
setSize(400, 500);
setVisible(true);
//이벤트 등록
connTf.addActionListener(this);
connBtn.addActionListener(this);
sendTf.addActionListener(this);
sendBtn.addActionListener(this);
}
//오버라이딩
public void actionPerformed(ActionEvent ae){
Object obj = ae.getSource();
if(obj == connTf || obj == connBtn){ //서버와 연결
connectProcess();
}else if(obj == sendTf || obj == sendBtn){ //서버로 문자 보내기
sendProcess();
}
}
//서버 연결
public void connectProcess(){
try{
ia = InetAddress.getByName(connTf.getText().trim());
s = new Socket(ia, 9999); //서버와 연결
isr = new InputStreamReader(s.getInputStream()); //InputStream
br = new BufferedReader(isr);
osw = new OutputStreamWriter(s.getOutputStream()); //OutputStream
pw = new PrintWriter(osw);
pw.flush();
Thread t = new Thread(this);
t.start();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//문자 서버로 보내기
public void sendProcess(){
pw.println(sendTf.getText());
pw.flush();
sendTf.setText("");
}
//Thread 구현
public void run(){
while(true){
try{
String msg = br.readLine(); //서버로부터 문자가 들어올 때까지 대기중...
if(msg == null) return;
ta.append(msg+"\n");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
new ClientChat();
}

}


'응용 SoftWare > JAVA' 카테고리의 다른 글

Excel Write/Read  (0) 2016.12.28
SocketTest  (0) 2016.12.28
[예제] Source View  (0) 2016.12.23
URL Test  (0) 2016.12.23
InetAddress Test  (0) 2016.12.23
Posted by Hyun CHO
,