자바 소형 채 팅 방 소스 코드
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
public class UDPChat extends JFrame implements ActionListener{
public TextArea textmessage = null;
public TextArea sendtext = null;
public DatagramSocket socket;
public JScrollBar vsBar;
public UDPChat()
{
super();
setTitle(" ");
setBounds(200,150,350,280);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.BLACK);
textmessage = new TextArea();
sendtext = new TextArea();
textmessage.setEditable(false);
textmessage.setColumns(35);
textmessage.setRows(10);
// textmessage.setLineWrap(true);
sendtext.setColumns(35);
sendtext.setRows(2);
JButton buttonOK = new JButton(" ");
JButton buttonclean = new JButton(" ");
JButton buttonquit = new JButton(" ");
GridLayout grid = new GridLayout(3,1);
grid.setHgap(10);
grid.setVgap(10);
panel1.setLayout(grid);
// panel1.setLayout(new GridLayout(3,1));
panel1.add(buttonOK);
panel1.add(buttonclean);
panel1.add(buttonquit);
panel2.setLayout(new FlowLayout());
panel2.add(textmessage);
panel2.add(sendtext);
getContentPane().add(panel1,BorderLayout.EAST);
getContentPane().add(panel2,BorderLayout.CENTER);
setVisible(true);
buttonOK.addActionListener(this);
buttonclean.addActionListener(this);
buttonquit.addActionListener(this);
server();
}
public void server()
{
try{
socket = new DatagramSocket(9527);
byte[] buf = new byte[1024];
final DatagramPacket dp1 = new DatagramPacket(buf,buf.length);
Runnable run = new Runnable(){
public void run()
{
while(true)
{
try{
Thread.sleep(100);
socket.receive(dp1);
int len = dp1.getLength();
String message = new String(dp1.getData(),0,len);
String ip = dp1.getAddress().getHostAddress();
System.out.println(ip);
if(!InetAddress.getLocalHost().getHostAddress().equals(ip))
textmessage.append(ip+"
"+message+'
');
}catch(IOException e)
{
e.printStackTrace();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
};
new Thread(run).start();
}catch(SocketException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
UDPChat udp = new UDPChat();
}
@Override
public void actionPerformed(ActionEvent e) {
// JButton button = (JButton) e.getSource();
String buttonname =e.getActionCommand();
if(buttonname.equals(" "))
System.exit(0);
if(buttonname.equals(" "))
{
textmessage.setText("");
}
if(buttonname.equals(" "))
{
String iP = "192.168.8.15";
try {
InetAddress address =InetAddress.getByName(iP);
byte []data = sendtext.getText().getBytes();
DatagramPacket dp = new DatagramPacket(data,data.length,address,9527);
String myip = InetAddress.getLocalHost().getHostAddress();
textmessage.append(myip +"
"+sendtext.getText() +"
");
socket.send(dp);
sendtext.setText("");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.