서버
more..
package Sever;
import java.io.*;
import java.net.*;
import java.util.*;
class Server extends Thread {
private ServerSocket ss;
private Socket soc;
private Vector vc = new Vector();
public Server() {
try {
ss = new ServerSocket(22345);
System.out.println("Server 레디...!");
} catch (IOException ee) {
System.err.println("해당 포트는 열수없습니다.");
System.exit(1);
}
this.start();
}
public void run() {
while (true) {
try {
soc = ss.accept();
UserInfo ui = new UserInfo(soc);
System.out.println("접근자 (" + ui.getnickname() + ") "
+ soc.toString());
for (int i = 0; i < vc.size(); i++) {
UserInfo imsi = (UserInfo) vc.elementAt(i);
imsi.getOut().write(
("/p" + ui.getnickname() + "\n").getBytes());
String message = "☆ " + ui.getnickname()
+ "님께서 입장하셨습니다^^.";
imsi.getOut().write((message + "\n").getBytes());
}
vc.add(ui);
for (int i = 0; i < vc.size(); i++) {
UserInfo imsi = (UserInfo) vc.elementAt(i);
ui.getOut().write(
("/p" + imsi.getnickname() + "\n").getBytes());
}
} catch (IOException ee) {
System.err.println("accept에러 : " + ee.toString());
}// end of catch
}// end of for
}// end of try
class UserInfo extends Thread {
private Socket socket;
private OutputStream out;
private BufferedReader in;
private String nickname;
public UserInfo(Socket s) {
socket = s;
try {
out = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
nickname = in.readLine();
} catch (IOException ee) {
}
this.start();
}
public void run() {
String str = null;
while (true) {
try {
str = in.readLine();
} catch (IOException ee) {
}
if (str == null) {
continue;
}
if (str.charAt(0) == '/') {
if(str.charAt(1) == 'w'){
String user = str.substring(2, str.indexOf(":"));
String message = str.substring(str.indexOf(":")+1);
for(int i =0; i < vc.size(); i++){
UserInfo imsi = (UserInfo)vc.elementAt(i);
if(imsi.getnickname().equals(user)){
String imsimessage = nickname + "(귓속말)> " + message;
try{
imsi.getOut().write((imsimessage+ "\n").getBytes());
out.write((imsimessage+ "\n").getBytes());
}catch(IOException ee){}
}
}
}
else if(str.charAt(1) == 'q'){
for(int i =0; i < vc.size() ; i++){
UserInfo imsi = (UserInfo)vc.elementAt(i);
if(imsi.getnickname().equals(nickname)){
vc.removeElementAt(i);
break;
}
}
for(int i =0; i < vc.size(); i++){
UserInfo imsi = (UserInfo)vc.elementAt(i);
String sss = "/q" + nickname;
try{
imsi.getOut().write((sss + "\n").getBytes());
}catch(IOException ee){}
}
break;
}
} else {
for (int i = 0; i < vc.size(); i++) {
UserInfo imsi = (UserInfo) vc.elementAt(i);
String message = nickname + " >> " + str;
try {
imsi.getOut().write((message + "\n").getBytes());
} catch (IOException ee) {
}
}
}
}
}
public String getnickname() {
return nickname;
}
public OutputStream getOut() {
return out;
}
}
}
public class Ex02_13 {
public static void main(String[] args) {
Server ser = new Server();
}
}
import java.io.*;
import java.net.*;
import java.util.*;
class Server extends Thread {
private ServerSocket ss;
private Socket soc;
private Vector vc = new Vector();
public Server() {
try {
ss = new ServerSocket(22345);
System.out.println("Server 레디...!");
} catch (IOException ee) {
System.err.println("해당 포트는 열수없습니다.");
System.exit(1);
}
this.start();
}
public void run() {
while (true) {
try {
soc = ss.accept();
UserInfo ui = new UserInfo(soc);
System.out.println("접근자 (" + ui.getnickname() + ") "
+ soc.toString());
for (int i = 0; i < vc.size(); i++) {
UserInfo imsi = (UserInfo) vc.elementAt(i);
imsi.getOut().write(
("/p" + ui.getnickname() + "\n").getBytes());
String message = "☆ " + ui.getnickname()
+ "님께서 입장하셨습니다^^.";
imsi.getOut().write((message + "\n").getBytes());
}
vc.add(ui);
for (int i = 0; i < vc.size(); i++) {
UserInfo imsi = (UserInfo) vc.elementAt(i);
ui.getOut().write(
("/p" + imsi.getnickname() + "\n").getBytes());
}
} catch (IOException ee) {
System.err.println("accept에러 : " + ee.toString());
}// end of catch
}// end of for
}// end of try
class UserInfo extends Thread {
private Socket socket;
private OutputStream out;
private BufferedReader in;
private String nickname;
public UserInfo(Socket s) {
socket = s;
try {
out = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
nickname = in.readLine();
} catch (IOException ee) {
}
this.start();
}
public void run() {
String str = null;
while (true) {
try {
str = in.readLine();
} catch (IOException ee) {
}
if (str == null) {
continue;
}
if (str.charAt(0) == '/') {
if(str.charAt(1) == 'w'){
String user = str.substring(2, str.indexOf(":"));
String message = str.substring(str.indexOf(":")+1);
for(int i =0; i < vc.size(); i++){
UserInfo imsi = (UserInfo)vc.elementAt(i);
if(imsi.getnickname().equals(user)){
String imsimessage = nickname + "(귓속말)> " + message;
try{
imsi.getOut().write((imsimessage+ "\n").getBytes());
out.write((imsimessage+ "\n").getBytes());
}catch(IOException ee){}
}
}
}
else if(str.charAt(1) == 'q'){
for(int i =0; i < vc.size() ; i++){
UserInfo imsi = (UserInfo)vc.elementAt(i);
if(imsi.getnickname().equals(nickname)){
vc.removeElementAt(i);
break;
}
}
for(int i =0; i < vc.size(); i++){
UserInfo imsi = (UserInfo)vc.elementAt(i);
String sss = "/q" + nickname;
try{
imsi.getOut().write((sss + "\n").getBytes());
}catch(IOException ee){}
}
break;
}
} else {
for (int i = 0; i < vc.size(); i++) {
UserInfo imsi = (UserInfo) vc.elementAt(i);
String message = nickname + " >> " + str;
try {
imsi.getOut().write((message + "\n").getBytes());
} catch (IOException ee) {
}
}
}
}
}
public String getnickname() {
return nickname;
}
public OutputStream getOut() {
return out;
}
}
}
public class Ex02_13 {
public static void main(String[] args) {
Server ser = new Server();
}
}
클라
more..
package Chat;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Ex02_12Sub extends JFrame implements ActionListener, Runnable {
private Label nicknamelb = new Label("대화명 : ", Label.RIGHT);
private TextField nicknametf = new TextField();
private Button connectbt = new Button("접속");
private TextArea viewta = new TextArea();
private Label talklb = new Label("대화 : ", Label.RIGHT);
private TextField talktf = new TextField();
private Button sendbt = new Button("전송");
private Label inwonlb = new Label("대화 참여자 : ", Label.RIGHT);
private Label inwonlb1 = new Label(" 0 ", Label.CENTER);
private Label inwonlb2 = new Label("명", Label.LEFT);
private List inwonli = new List(5, false);
private CheckboxGroup cg = new CheckboxGroup();
private Checkbox hidecb = new Checkbox("귓속말 설정", cg, false);
private Checkbox voidcb = new Checkbox("귓속말 해제", cg, true);
private Button disconnbt = new Button("나가기");
private Socket soc;
private OutputStream out;
private BufferedReader in;
private Thread currentTh;
public Ex02_12Sub(String str) {
super(str);
this.init();
this.start();
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
this.setLayout(new BorderLayout());
Panel p = new Panel(new BorderLayout());// 중앙
Panel p1 = new Panel(new BorderLayout());// 우측
this.add("Center", p);
this.add("East", p1);
Panel p_1 = new Panel(new BorderLayout());
p_1.add("West", nicknamelb);
p_1.add("Center", nicknametf);
p_1.add("East", connectbt);
p.add("North", p_1);
p.add("Center", viewta);
viewta.setEditable(false);
Panel p_2 = new Panel(new BorderLayout());
p_2.add("West", talklb);
p_2.add("Center", talktf);
p_2.add("East", sendbt);
p.add("South", p_2);
Panel p1_1 = new Panel(new FlowLayout());
p1_1.add(inwonlb);
p1_1.add(inwonlb1);
p1_1.add(inwonlb2);
p1.add("North", p1_1);
p1.add("Center", inwonli);
Panel p1_2 = new Panel(new BorderLayout());
Panel p1_2_1 = new Panel(new GridLayout(2, 1));
p1_2_1.add(hidecb);
p1_2_1.add(voidcb);
p1_2.add("East", p1_2_1);
Panel p1_2_2 = new Panel(new FlowLayout(FlowLayout.RIGHT));
p1_2_2.add(disconnbt);
p1_2.add("Center", p1_2_2);
p1.add("South", p1_2);
}
public void start() {
nicknametf.addActionListener(this);
connectbt.addActionListener(this);
talktf.addActionListener(this);
sendbt.addActionListener(this);
disconnbt.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nicknametf || e.getSource() == connectbt) {
String str = nicknametf.getText().trim();
if (str == null || str.length() == 0) {
nicknametf.setText("");
nicknametf.requestFocus();
viewta.setText("대화명을 적으세요 ^^");
return;
}
try {
soc = new Socket("220.81.20.109", 22345);
out = soc.getOutputStream();
in = new BufferedReader(new InputStreamReader(soc
.getInputStream()));
out.write((str + "\n").getBytes());
currentTh = new Thread(this);
currentTh.start();
} catch (IOException ee) {
viewta.setText("서버와 연결이 되지 않습니다. ");
return;
}
}
if (e.getSource() == talktf || e.getSource() == sendbt) {
String message = talktf.getText().trim();
if (message == null || message.length() == 0) {
talktf.setText("");
talktf.requestFocus();
return;
}
if (hidecb.getState() == true) {
String user = ((String) inwonli.getSelectedItem()).trim();
String mymessage = "/w" + user + ":" + message;
try{
out.write((mymessage + "\n").getBytes());
}catch(IOException ee){}
} else {
try {
out.write((message + "\n").getBytes());
} catch (IOException ee) {
}
}
talktf.setText("");
talktf.requestFocus();
}
else if(e.getSource() == disconnbt){
currentTh.interrupt();
try{
out.write(("/q" + "\n").getBytes());
}catch(IOException ee){}
System.exit(0);
}
}
public void run() {
nicknametf.setEnabled(false);
connectbt.setEnabled(false);
talktf.requestFocus();
viewta.setText("☆ 대화에 참여하셨습니다 !!!\n\n");
String str = null;
while (true) {
try {
str = in.readLine();
} catch (IOException ee) {
}
if (str == null) {
continue;
}
if (str.charAt(0) == '/') {
if (str.charAt(1) == 'p') {
String imsi = str.substring(2);
imsi = imsi.trim();
inwonli.addItem(imsi);
int xx = Integer.parseInt(inwonlb1.getText().trim());
xx++;
inwonlb1.setText(String.valueOf(xx));
}
else if (str.charAt(1) == 'q'){
String imsi = str.substring(2);
for(int i = 0; i < inwonli.getItemCount(); i++){
String imsi1 = (String)inwonli.getItem(i);
imsi1 = imsi1.trim();
if(imsi1.equals(imsi)){
inwonli.remove(i);
break;
}
}
int xx = Integer.parseInt(inwonlb1.getText().trim());
xx--;
inwonlb1.setText(String.valueOf(xx));
viewta.append("##### " + imsi + "님께서 퇴장하셨습니다. #####\n\n");
}
} else {
viewta.append(str + "\n");
}
}
}
}
public class Ex02_12 {
public static void main(String[] args) {
Ex02_12Sub es = new Ex02_12Sub(" 채팅 클라이언트 ");
}
}
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Ex02_12Sub extends JFrame implements ActionListener, Runnable {
private Label nicknamelb = new Label("대화명 : ", Label.RIGHT);
private TextField nicknametf = new TextField();
private Button connectbt = new Button("접속");
private TextArea viewta = new TextArea();
private Label talklb = new Label("대화 : ", Label.RIGHT);
private TextField talktf = new TextField();
private Button sendbt = new Button("전송");
private Label inwonlb = new Label("대화 참여자 : ", Label.RIGHT);
private Label inwonlb1 = new Label(" 0 ", Label.CENTER);
private Label inwonlb2 = new Label("명", Label.LEFT);
private List inwonli = new List(5, false);
private CheckboxGroup cg = new CheckboxGroup();
private Checkbox hidecb = new Checkbox("귓속말 설정", cg, false);
private Checkbox voidcb = new Checkbox("귓속말 해제", cg, true);
private Button disconnbt = new Button("나가기");
private Socket soc;
private OutputStream out;
private BufferedReader in;
private Thread currentTh;
public Ex02_12Sub(String str) {
super(str);
this.init();
this.start();
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
this.setLayout(new BorderLayout());
Panel p = new Panel(new BorderLayout());// 중앙
Panel p1 = new Panel(new BorderLayout());// 우측
this.add("Center", p);
this.add("East", p1);
Panel p_1 = new Panel(new BorderLayout());
p_1.add("West", nicknamelb);
p_1.add("Center", nicknametf);
p_1.add("East", connectbt);
p.add("North", p_1);
p.add("Center", viewta);
viewta.setEditable(false);
Panel p_2 = new Panel(new BorderLayout());
p_2.add("West", talklb);
p_2.add("Center", talktf);
p_2.add("East", sendbt);
p.add("South", p_2);
Panel p1_1 = new Panel(new FlowLayout());
p1_1.add(inwonlb);
p1_1.add(inwonlb1);
p1_1.add(inwonlb2);
p1.add("North", p1_1);
p1.add("Center", inwonli);
Panel p1_2 = new Panel(new BorderLayout());
Panel p1_2_1 = new Panel(new GridLayout(2, 1));
p1_2_1.add(hidecb);
p1_2_1.add(voidcb);
p1_2.add("East", p1_2_1);
Panel p1_2_2 = new Panel(new FlowLayout(FlowLayout.RIGHT));
p1_2_2.add(disconnbt);
p1_2.add("Center", p1_2_2);
p1.add("South", p1_2);
}
public void start() {
nicknametf.addActionListener(this);
connectbt.addActionListener(this);
talktf.addActionListener(this);
sendbt.addActionListener(this);
disconnbt.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nicknametf || e.getSource() == connectbt) {
String str = nicknametf.getText().trim();
if (str == null || str.length() == 0) {
nicknametf.setText("");
nicknametf.requestFocus();
viewta.setText("대화명을 적으세요 ^^");
return;
}
try {
soc = new Socket("220.81.20.109", 22345);
out = soc.getOutputStream();
in = new BufferedReader(new InputStreamReader(soc
.getInputStream()));
out.write((str + "\n").getBytes());
currentTh = new Thread(this);
currentTh.start();
} catch (IOException ee) {
viewta.setText("서버와 연결이 되지 않습니다. ");
return;
}
}
if (e.getSource() == talktf || e.getSource() == sendbt) {
String message = talktf.getText().trim();
if (message == null || message.length() == 0) {
talktf.setText("");
talktf.requestFocus();
return;
}
if (hidecb.getState() == true) {
String user = ((String) inwonli.getSelectedItem()).trim();
String mymessage = "/w" + user + ":" + message;
try{
out.write((mymessage + "\n").getBytes());
}catch(IOException ee){}
} else {
try {
out.write((message + "\n").getBytes());
} catch (IOException ee) {
}
}
talktf.setText("");
talktf.requestFocus();
}
else if(e.getSource() == disconnbt){
currentTh.interrupt();
try{
out.write(("/q" + "\n").getBytes());
}catch(IOException ee){}
System.exit(0);
}
}
public void run() {
nicknametf.setEnabled(false);
connectbt.setEnabled(false);
talktf.requestFocus();
viewta.setText("☆ 대화에 참여하셨습니다 !!!\n\n");
String str = null;
while (true) {
try {
str = in.readLine();
} catch (IOException ee) {
}
if (str == null) {
continue;
}
if (str.charAt(0) == '/') {
if (str.charAt(1) == 'p') {
String imsi = str.substring(2);
imsi = imsi.trim();
inwonli.addItem(imsi);
int xx = Integer.parseInt(inwonlb1.getText().trim());
xx++;
inwonlb1.setText(String.valueOf(xx));
}
else if (str.charAt(1) == 'q'){
String imsi = str.substring(2);
for(int i = 0; i < inwonli.getItemCount(); i++){
String imsi1 = (String)inwonli.getItem(i);
imsi1 = imsi1.trim();
if(imsi1.equals(imsi)){
inwonli.remove(i);
break;
}
}
int xx = Integer.parseInt(inwonlb1.getText().trim());
xx--;
inwonlb1.setText(String.valueOf(xx));
viewta.append("##### " + imsi + "님께서 퇴장하셨습니다. #####\n\n");
}
} else {
viewta.append(str + "\n");
}
}
}
}
public class Ex02_12 {
public static void main(String[] args) {
Ex02_12Sub es = new Ex02_12Sub(" 채팅 클라이언트 ");
}
}
http://dynast.tistory.com/trackback/135


Server.exe
이올린에 북마크하기
이올린에 추천하기



