|
SSISO Community검색 |
|
SSISO Community메뉴 |
|
SSISO Community카페 |
|
블로그 카테고리 |
|
|
Synchronized로 검색한 결과 |
|
등록일:2008-04-15 16:55:18 작성자: 제목:자바를 공부하면서 만들어본 간단한 채팅서버와 클라이언트 |
|
/*
VChatServer.class
*/
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
public class VChatServer
{
Frame f = null;
TextArea ta = null;
ServerSocket serverSocket = null;
Socket socket = null;
VChatServerThread serverThread = null;
public VChatServer()
{
f = new Frame("VChatServer");
ta = new TextArea();
ta.setEditable(false);
ta.setBackground(Color.WHITE);
f.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
f.add(ta);
f.setSize(500, 400);
Dimension d = f.getToolkit().getScreenSize();
f.setLocation( (d.width - f.getWidth())/2, (d.height - f.getHeight())/2 );
f.setResizable(false);
f.setVisible(true);
}
public void startServer()
{
try
{
serverSocket = new ServerSocket(7777);
while(true)
{
socket = serverSocket.accept();
serverThread = new VChatServerThread(socket, ta);
serverThread.start();
}
}
catch(IOException ie)
{
System.out.println(ie);
}
}
public static void main(String[] args)
{
VChatServer server = new VChatServer();
server.startServer();
}
}
/*
VChatServerThread.class
*/
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class VChatServerThread extends Thread
{
public static Vector clientList = new Vector(1, 1);
Socket socket = null;
TextArea ta = null;
DataOutputStream dos = null;
DataInputStream dis = null;
String userName = null;
public VChatServerThread(Socket socket, TextArea ta)
{
this.socket = socket;
this.ta = ta;
}
public void run()
{
try
{
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
clientList.addElement(this);
userName = dis.readUTF();
ta.append("[" + userName + "]님이 접속하셨습니다.\n");
messageCompare("LOGIN/" + userName);
while(true)
{
messageCompare(dis.readUTF());
}
}
catch(IOException ie)
{
//접속해제 처리
ta.append("[" + userName + "]님이 접속을 해제 하였습니다.\n");
messageCompare("LOGOUT/" + userName);
disConnect();
}
}
public void messageCompare(String message)
{
StringTokenizer st = new StringTokenizer(message, "/");
String state = st.nextToken();
String recvMessage = st.nextToken();
if(state.equals("LOGIN"))
{
sendMessage("[" + userName + "]님이 접속하셨습니다.\n");
}
else if(state.equals("LOGOUT"))
{
sendMessage("[" + userName + "]님이 접속을 해제 하셨습니다.\n");
}
else
{
sendMessage("[" + state + "]" + recvMessage + "\n"); //state는 대화명으로 출력된다.
}
}
public void sendMessage(String message)
{
Synchronized(clientList)
{
try
{
Enumeration e = clientList.elements();
while(e.hasMoreElements())
{
VChatServerThread temp = (VChatServerThread)e.nextElement();
temp.dos.writeUTF(message);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public void disConnect()
{
try
{
clientList.removeElement(this);
dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
VChatClient.class
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class VChatClient implements Runnable, ActionListener
{
Frame f = null;
TextArea ta = null;
TextField idField = null;
TextField messageField = null;
Button connectBtn = null;
Panel p = null;
Socket socket = null;
DataOutputStream dos = null;
DataInputStream dis = null;
Thread t = null;
public VChatClient()
{
f = new Frame("VChatClient");
f.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
disConnect();
System.exit(0);
}
}
);
ta = new TextArea();
ta.setEditable(false);
ta.setBackground(Color.WHITE);
f.add(ta, BorderLayout.CENTER);
idField = new TextField(5);
messageField = new TextField(25);
messageField.addActionListener(this);
connectBtn = new Button("Connect");
connectBtn.addActionListener(this);
p = new Panel();
p.add(new Label("ID", Label.CENTER));
p.add(idField);
p.add(new Label("Message", Label.CENTER));
p.add(messageField);
p.add(connectBtn);
f.add(p, BorderLayout.SOUTH);
f.setSize(500, 400);
Dimension d = f.getToolkit().getScreenSize();
f.setLocation( (d.width - f.getWidth())/2, (d.height - f.getHeight())/2 );
f.setVisible(true);
}
public void run()
{
try
{
while(true)
{
ta.append(dis.readUTF());
}
}
catch(Exception e)
{
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == connectBtn)
{
try
{
socket = new Socket("127.0.0.1", 7777);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
t = new Thread(this);
t.start();
dos.writeUTF(idField.getText());
idField.setEditable(false);
idField.setBackground(Color.GRAY);
connectBtn.setEnabled(false);
}
catch(Exception e)
{
System.out.println(e);
}
}
else if(ae.getSource() == messageField)
{
try
{
dos.writeUTF(idField.getText() + "/" + messageField.getText());
messageField.setText("");
}
catch(IOException ie)
{
System.out.println(ie);
}
}
}
public void disConnect()
{
try
{
socket.close();
dis.close();
dos.close();
socket = null;
dis = null;
dos = null;
t = null;
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
VChatClient client = new VChatClient();
}
} |
|
|
|
|
|