Search

'JAVA 채팅 프로그램'에 해당되는 글 1건

  1. 2014.01.21 JAVA 채팅 프로그램

JAVA 채팅 프로그램

Programming/JAVA,JSP 2014. 1. 21. 17:38 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

서버




import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;


import javax.swing.DefaultListModel;

import javax.swing.JFrame;

import javax.swing.JList;


class MyFrame extends JFrame {

DefaultListModel listModel ;

MyFrame(DefaultListModel listModel){

this.listModel = listModel;

setSize(600, 400);

setDefaultCloseOperation(this.EXIT_ON_CLOSE);


JList list = new JList(listModel);


add(list);


setVisible(true);

}

}


public class Server {

public static DefaultListModel listModel = new DefaultListModel();

public static void main(String[] args) {

try {


ServerSocket ss = new ServerSocket(5555);

Socket s;


MyFrame mf = new MyFrame(listModel);

while (true) {


s = ss.accept();


listModel.addElement( s.getInetAddress() );

System.out.println("입장 : " + s.getInetAddress());

PerClientThread pc = new PerClientThread();

pc.s = s;

pc.start();


}


} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}

}





import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

public class PerClientThread extends Thread {
public static ArrayList<PrintWriter> printWriterList = new ArrayList<PrintWriter>();
public Socket s;

@Override
public void run() {
PrintWriter out = null;
BufferedReader in = null;
try {
out = new PrintWriter(s.getOutputStream(), true);
printWriterList.add(out);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null)
{
System.out.println( s.getInetAddress() + " : " +  inputLine);
for( int i = 0 ; i < printWriterList.size() ; i++ )
{
printWriterList.get(i).println(inputLine);
}
}
Server.listModel.removeElement(s.getInetAddress());
in.close();
out.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("발생자 : " + s.getInetAddress());
e.printStackTrace();
Server.listModel.removeElement(s.getInetAddress());
}
Server.listModel.removeElement(s.getInetAddress());
}

}







클라이언트


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client {
static JTextField textField;
static JTextArea textArea;
static PrintWriter out;
static BufferedReader in;

public static void main(String[] args) {
try {
Socket s = new Socket("115.20.247.170", 5555);

out = new PrintWriter(s.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
JFrame f = new JFrame("채팅");
f.setSize(600, 400);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
textArea = new JTextArea();
JScrollPane sp = new JScrollPane(textArea);
textField = new JTextField(10);
textField.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
out.println("TSC : " + textField.getText());
textField.setText("");
}
}

);
f.add(textField, BorderLayout.SOUTH);
f.add(sp, BorderLayout.CENTER);
f.setVisible(true);
ReceiveMSG rMSG = new ReceiveMSG();
rMSG.textArea = textArea;
rMSG.in = in;
rMSG.start();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}






import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.BufferedReader;
import java.io.IOException;

import javax.swing.JTextArea;

public class ReceiveMSG extends Thread {
public JTextArea textArea;

public BufferedReader in;

@Override
public void run() {

while (true) {
try {
String msg;
msg = in.readLine();
textArea.setText(textArea.getText()+ "\n" + msg);
textArea.setCaretPosition(textArea.getDocument().getLength());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}


'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA 채팅 프로그램 export zip  (1) 2014.01.24
JAVA DML  (0) 2014.01.22
JAVA 채팅 예제 #2  (0) 2014.01.20
JAVA 채팅 Server 예제 #1  (0) 2014.01.20
JAVA 채팅 Client 예제 #1  (0) 2014.01.20