클라이언트
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();
}
}
'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 |