336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(5555);
Socket s;
while (true) {
s = ss.accept();
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);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("발생자 : " + s.getInetAddress());
e.printStackTrace();
}
}
}
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 java.util.Scanner;
import javax.swing.JFrame;
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();
textField = new JTextField(10);
textField.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
out.println(textField.getText());
textField.setText("");
}
}
);
f.add(textField, BorderLayout.SOUTH);
f.add(textArea, 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.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
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);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
'Programming > JAVA,JSP' 카테고리의 다른 글
JAVA DML (0) | 2014.01.22 |
---|---|
JAVA 채팅 프로그램 (0) | 2014.01.21 |
JAVA 채팅 Server 예제 #1 (0) | 2014.01.20 |
JAVA 채팅 Client 예제 #1 (0) | 2014.01.20 |
JAVA FIle 실습 #1 (0) | 2014.01.17 |