Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 클래스
- AssertJ
- Swing
- JavaScript
- class
- Docker Desktop
- 자바
- synchronized
- JS
- thread
- 파이썬
- Python
- 프로그래머스스쿨
- 저장소
- 객체
- StringBuilder
- event
- array
- Dict
- c#
- GIT
- SpringBoot
- Java
- SSL
- 배열
- 메소드
- join()
- 자바스크립트
- 스프링부트
- docker
Archives
- Today
- Total
정리노트
[자바/java] 간단 채팅 프로그램 / UDP 서버, 클라이언트 본문
사용자 1
package chatUDP;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UDPchat1 {
protected JTextField textField;
protected JTextArea textArea;
DatagramSocket socket; // UDP프로토콜 생성 클래스
DatagramPacket packet; // 주고 받을 데이터를 담는 클래스
InetAddress address = null;
final int myPort = 5000; // 수신용 포트번호
final int otherPort = 6000; // 송신용 포트번호
public UDPchat1() throws IOException {
MyFrame f = new MyFrame();
address = InetAddress.getByName("127.0.0.1"); // IP주소를 얻는다.
// 나의 포트번호로 DatagramSocket 객체 생성 (상대방은 나의 포트번호로 packet 사용)
socket = new DatagramSocket(myPort);
}
public void process() {
while (true) {
try {
byte buf[] = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); // Socket의 메소드로 패킷을 받고
textArea.append("Receive: " + new String(buf) + "\n"); // 내용 표시
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
class MyFrame extends JFrame implements ActionListener {
public MyFrame() {
super("Link");
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField(30);
textField.addActionListener(this);
textArea = new JTextArea(10, 30);
textArea.setEditable(false);
add(textField, BorderLayout.PAGE_END);
add(textArea, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
String s = textField.getText();
byte[] buffer = s.getBytes(); // 내용입력 후 byte 변환
DatagramPacket packet;
// DataPacket생성자 (byte로 변환 한 내용, 길이, 나의IP, 상대 포트)
packet = new DatagramPacket(buffer, buffer.length, address, otherPort);
try {
socket.send(packet); // Socket 객체의 메소드로 packet 보내기.
} catch (IOException e) {
e.printStackTrace();
}
textArea.append("Send: " + s + "\n");
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
public static void main(String[] args) throws IOException {
UDPchat1 c = new UDPchat1();
c.process();
}
}
사용자 2
package chatUDP;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UDPchat2 {
protected JTextField textField;
protected JTextArea textArea;
DatagramSocket socket; // UDP프로토콜 생성 클래스
DatagramPacket packet; // 주고 받을 데이터를 담는 클래스
InetAddress address = null;
final int myPort = 6000; // 수신용 포트번호
final int otherPort = 5000; // 송신용 포트번호
public UDPchat2() throws IOException {
MyFrame f = new MyFrame();
address = InetAddress.getByName("127.0.0.1");
socket = new DatagramSocket(myPort);
}
public void process() {
while (true) {
try {
byte buf[] = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); // 패킷을 받고
textArea.append("Receive: " + new String(buf) + "\n"); // 내용 표시
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
class MyFrame extends JFrame implements ActionListener {
public MyFrame() {
super("Zelda");
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField(30);
textField.addActionListener(this);
textArea = new JTextArea(10, 30);
textArea.setEditable(false);
add(textField, BorderLayout.PAGE_END);
add(textArea, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
String s = textField.getText();
byte[] buffer = s.getBytes();
DatagramPacket packet;
packet = new DatagramPacket(buffer, buffer.length, address, otherPort);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
textArea.append("Send: " + s + "\n");
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
public static void main(String[] args) throws IOException {
UDPchat2 c = new UDPchat2();
c.process();
}
}
728x90
'프로그래밍 > Java' 카테고리의 다른 글
[Java/연습] 프로그래머스 / 택배 상자 꺼내기 (0) | 2025.03.05 |
---|---|
[Java/연습] 프로그래머스 / 유연근무제 (0) | 2025.03.05 |
[자바/java] 서버, 클라이언트 접속 (간단한 채팅) / TCP 프로토콜 (0) | 2023.02.07 |
[자바/java] IP주소 찾기 / 사이트 내용 받기 (0) | 2023.02.07 |
[자바/java] synchronized 데이터 중복사용 방지, 좌석예약 (0) | 2023.02.05 |