JAVA 채팅 프로그램 소스

Programming/JAVA,JSP 2013. 8. 23. 14:09 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

클라이언트


import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.TextArea;

import java.awt.TextField;

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.nio.ByteOrder;


import javax.swing.JFrame;


class MyFrame extends JFrame implements ActionListener

{

public TextField tf;

public TextArea ta;

PrintWriter out;

public MyFrame(PrintWriter out)

{

this.out = out;

tf = new TextField();

ta = new TextArea();

setSize(600, 400);

setVisible(true);

add(tf, BorderLayout.SOUTH);

add(ta, BorderLayout.CENTER);

tf.addActionListener(this);

}


@Override

public void actionPerformed(ActionEvent arg0)

{

if (arg0.getSource() == tf)

{

out.println(tf.getText().toString());

tf.setText("");

}

}

}


public class client

{

public static void main(String[] args)

{

BufferedReader user = new BufferedReader(new InputStreamReader(

System.in));

Socket socket;

try

{

socket = new Socket("115.20.247.142", 1251);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

MyFrame mf = new MyFrame(out);

ReceiveThread rt = new ReceiveThread(socket, mf.ta);

rt.start();

} catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


class ReceiveThread extends Thread

{

Socket socket;

TextArea ta;


public ReceiveThread(Socket socket, TextArea ta)

{

this.socket = socket;

this.ta = ta;

}


@Override

public void run()

{

BufferedReader in;

try

{

in = new BufferedReader(new InputStreamReader(

socket.getInputStream()));

while (true)

{

ta.setText(ta.getText() +"\n"+ in.readLine());

}


} catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

super.run();

}

}



서버


package com.tansanc.tistory;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyApplet2
{
public static void main(String[] args) throws IOException
{
int i = 1;
ServerSocket server = null;
server = new ServerSocket(1251, 5);
while (true)
{
System.out.println("Accept 대기");
Socket socket = server.accept();
System.out.println("Accept 성공");
PerClientReceiveThread pcrt = new PerClientReceiveThread(socket);
pcrt.start();
}

// Socket socket = new Socket("115.20.247.142",1251);
// InputStream is = socket.getInputStream();
// byte i = (byte)is.read();
// System.out.println(i);

}
}



package com.tansanc.tistory;

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

public class PerClientReceiveThread extends Thread
{
static ArrayList<PrintWriter> arGeneral = new ArrayList<PrintWriter>();
Socket socket;

public PerClientReceiveThread(Socket socket)
{
this.socket = socket;
}

@Override
public void run()
{
InputStream os;
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
arGeneral.add(out);

while (true)
{
String msg = in.readLine();
System.out.println(msg);
for (int i = 0; i < arGeneral.size(); i++)
{
arGeneral.get(i).println(msg);
}
}

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


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

JAVA 버블 정렬  (0) 2013.11.30
JAVA JTable 실습 예제  (0) 2013.08.24
JAVA 채팅 클라이언트  (0) 2013.08.23
JAVA 계산기 프로그램 부분완성  (0) 2013.08.16
JAVA 계산기 레이아웃  (0) 2013.08.16