Microsoft PowerPoint - 04-UDP Programming.ppt

Similar documents
Microsoft PowerPoint - Supplement-03-TCP Programming.ppt [호환 모드]

Microsoft PowerPoint - 03-TCP Programming.ppt

<4D F736F F F696E74202D20C1A63235C0E520B3D7C6AEBFF6C5A920C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

자바-11장N'1-502

13-Java Network Programming

제1장 자바 언어 소개

Chap12

gnu-lee-oop-kor-lec10-1-chap10

9장.key

PowerPoint 프레젠테이션

rmi_박준용_final.PDF

Java Programing Environment

11장.key

12-file.key

Microsoft PowerPoint - 14주차 강의자료

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Presentation

자바GUI실전프로그래밍2_장대원.PDF

02 C h a p t e r Java

JMF3_심빈구.PDF

05-class.key

<4D F736F F F696E74202D20C1A63230C0E520BDBAC0AE20C4C4C6F7B3CDC6AE203128B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - Java7.pptx

Java

No Slide Title

10장.key

<4D F736F F F696E74202D E20B3D7C6AEBFF6C5A920C7C1B7CEB1D7B7A1B9D62E >

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint 프레젠테이션

강의자료

09-interface.key

Microsoft PowerPoint - RMI.ppt

JAVA PROGRAMMING 실습 09. 예외처리

PowerPoint Presentation

Microsoft Word - java18-1-final-answer.doc

<4D F736F F F696E74202D20C1A63138C0E520C0CCBAA5C6AE20C3B3B8AE28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

2009년 상반기 사업계획

FileMaker ODBC and JDBC Guide

목차 INDEX JSON? - JSON 개요 - JSONObject - JSONArray 서울시공공데이터 API 살펴보기 - 요청인자살펴보기 - Result Code - 출력값 HttpClient - HttpHelper 클래스작성 - JSONParser 클래스작성 공공

비긴쿡-자바 00앞부속

The Pocket Guide to TCP/IP Sockets: C Version

untitled

PowerPoint Presentation

4장.문장

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

01-OOPConcepts(2).PDF

제8장 자바 GUI 프로그래밍 II

FileMaker ODBC and JDBC Guide

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밊

ch09

PowerPoint Presentation

<C0CCBCBCBFB52DC1A4B4EBBFF82DBCAEBBE7B3EDB9AE2D D382E687770>

<4D F736F F F696E74202D20C1A63236C0E520BED6C7C3B8B428B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Cluster management software

Connection 8 22 UniSQLConnection / / 9 3 UniSQL OID SET

PowerPoint Presentation

03-JAVA Syntax(2).PDF

Network Programming

Design Issues

Microsoft PowerPoint - EEL2 Lecture10 -Swing and Event Handling.pptx

학습목표 네트워크개요 TCP PORT URL/InetAddress Socket/TCP 을이용한네트워크 2

PowerPoint Presentation

JAVA PROGRAMMING 실습 08.다형성

Spring Boot/JDBC JdbcTemplate/CRUD 예제

JMF2_심빈구.PDF

PowerPoint Presentation

<4D F736F F F696E74202D20B3D7C6AEBFF6C5A9C7C1B7CEB1D7B7A1B9D65F FBCD2C4CF5FC3A4C6C35FBFA1C4DA2E BC8A3C8A

3ÆÄÆ®-11

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

쉽게 풀어쓴 C 프로그래밍

10-Java Applet

The Pocket Guide to TCP/IP Sockets: C Version

bn2019_2

중간고사

슬라이드 1

제11장 프로세스와 쓰레드

Microsoft PowerPoint - Lecture_Note_5.ppt [Compatibility Mode]

Microsoft PowerPoint - 2강

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

Microsoft PowerPoint 자바-기본문법(Ch2).pptx

<4D F736F F F696E74202D20C1A63234C0E520C0D4C3E2B7C228B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

5장.key

11-GUI.key

(Microsoft PowerPoint - AUTCHEYREGRG.ppt [\310\243\310\257 \270\360\265\345])

신림프로그래머_클린코드.key

gnu-lee-oop-kor-lec06-3-chap7

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

MasoJava4_Dongbin.PDF

슬라이드 1

PowerPoint 프레젠테이션

public class FlowLayoutPractice extends JFrame { public FlowLayoutPractice() { super("flowlayout Practice"); this. Container contentpane = getcontentp

iii. Design Tab 을 Click 하여 WindowBuilder 가자동으로생성한 GUI 프로그래밍환경을확인한다.

fundamentalOfCommandPattern_calmglow_pattern_jstorm_1.0_f…

Microsoft PowerPoint - CSharp-10-예외처리

class InetAddress3{ public static void main(string[] args) throws Exception{ String url = null ; Scanner reader = new Scanner(System.in); System.out.p

(Microsoft PowerPoint - java1-lecture11.ppt [\310\243\310\257 \270\360\265\345])

Transcription:

Chapter 4. UDP Dongwon Jeong djeong@kunsan.ac.kr http://ist.kunsan.ac.kr/ Dept. of Informatics & Statistics 목차 UDP 1 1

UDP 개념 자바 UDP 프로그램작성 클라이언트와서버모두 DatagramSocket 클래스로생성 상호간통신은 DatagramPacket 클래스를이용하여 packet 단위로전송 UDP Client UDP Server DatagramSocket() DatagramSocket() 데이터 ( 요청 ) send() receive() 클라이언트요청처리 데이터 ( 응답 ) receive() send() close() UDP 2 UDP 개념 (cont.) DatagramSocket 클래스생성자 DatagramSocket() 로컬컴퓨터의사용가능한임의의포트사용 DatagramSocket(int port) 로컬컴퓨터의주어진포트사용 DatagramSocket(int port, InetAddress addr) 주어진주소와포트번호를사용 UDP 3 2

UDP 개념 (cont.) DatagramPacket 클래스 UDP 는 TCP 와는달리 DatagramPacket 클래스를이용하여데이터를상호주고받음 즉, DatagramPacket 클래스를데이터송신과수신을위한용도로사용 TCP 의경우읽기와쓰기를반복 : read(), write() DatagramPacket 클래스생성자 데이터를받을때이용되는생성자와보낼때이용되는생성자로구분 데이터를받을때이용되는생성자 DatagramPacket(byte[] buf, int length) buf 배열에 length 크기의데이터를받기위한객체생성 DatagramPacket(byte[] buf, int offset, int length) buf 배열의 offset 위치부터 length 개의공간에데이터를받기위한객체생성 데이터를보낼때이용되는생성자 DatagramPacket(byte[] buf, int length, InetAddress address, int port) DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) UDP 4 UDP 프로그램작성 : ConsoleDatagramServer import java.net.*; public class ConsoleDatagramServer { public static void main (String args[]){ DatagramSocket socket; byte[] buf = new byte[1024]; DatagramPacket packet; InetAddress address; socket = new DatagramSocket (1248); System.out.println("DatagramSever is ready."); while (true){ packet = new DatagramPacket (buf, buf.length); socket.receive(packet); int len = packet.getlength(); System.out.print(">>" + new String(buf, 0, len)); address = packet.getaddress(); System.out.print(": from [" + address + ":"); int port = packet.getport(); System.out.println(port + "]"); catch (Exception e) { System.out.println(e); UDP 5 3

UDP 프로그램작성 : ConsoleDatagramClient import java.net.*; import java.io.*; public class ConsoleDatagramClient { public static void main (String args[]){ DatagramSocket socket; DatagramPacket packet; InetAddress address; if (args.length == 0) { System.out.println("Usage: java " + "ConsoleDatagramClient <Server address>"); return; address = InetAddress.getByName(args[0]); socket = new DatagramSocket (1248); while (true){ System.out.print("=>"); String msg = reader.readline(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); byte[] buf = msg.getbytes(); packet = new DatagramPacket (buf, buf.length, address, 1248); socket.send(packet); catch (Exception e) { System.out.println(e); UDP 6 UDP 프로그램작성 : 실행결과 UDP 7 4

UDP 프로그램작성 : MessageServer import java.net.*; public class MessageServer { public static void main (String args[]){ DatagramSocket socket; byte[] buf = new byte[1024]; DatagramPacket packet; InetAddress address; int port; String dstring = null; socket = new DatagramSocket (1268); System.out.println("MessageServer is ready."); while (true){ packet = new DatagramPacket (buf, buf.length); socket.receive(packet); int len = packet.getlength(); catch (Exception e) { System.err.println(e); address = packet.getaddress(); port = packet.getport(); System.out.println(">>" + new String (buf, 0, len)); new MessageWindow (new String (buf, 0, len)); UDP 8 UDP 프로그램작성 : MessageClient import java.net.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MessageClient extends JFrame implements ActionListener { private DatagramSocket socket=null; private DatagramPacket packet; private JTextField servername; private JTextArea input; private JButton send, clear; public MessageClient(){ super ("Message Client"); JPanel top = new JPanel (new BorderLayout()); top.add(new JLabel ("Server Name"), "West"); servername = new JTextField ("localhost"); top.add(servername, "Center"); getcontentpane().add(top, "North"); input = new JTextArea(); getcontentpane().add(new JScrollPane(input), "Center"); JPanel bottom = new JPanel(); send = new JButton ("Transport"); send.addactionlistener(this); clear = new JButton("Cancel"); clear.addactionlistener(this); bottom.add(send); bottom.add(clear); getcontentpane().add(bottom, "South"); setdefaultcloseoperation(exit_on_close); setsize(400, 200); setvisible(true); UDP 9 5

UDP 프로그램작성 : MessageClient (cont.) protected void sendmessage(string server, String msg){ InetAddress address = null; address = InetAddress.getByName(server); socket = new DatagramSocket(); if (socket!= null) { byte[] buf = msg.getbytes(); packet = new DatagramPacket(buf, buf.length, address, 1268); socket.send(packet); catch (Exception e) { System.out.println(e); finally { socket.close(); String s = servername.gettext(); if (s.trim().length() == 0) return; public void actionperformed (ActionEvent e){ Object b = e.getsource(); if (b == send){ sendmessage (s, input.gettext()); input.settext(""); else { input.settext(""); public static void main (String args[]){ new MessageClient(); // End Program UDP 10 UDP 프로그램작성 : MessageWindow import javax.swing.*; public class MessageWindow extends JFrame { JTextArea data; public MessageWindow (String msg){ super ("Message"); data = new JTextArea (msg); data.seteditable (false); getcontentpane().add(new JScrollPane(data), "Center"); setsize (300, 200); setvisible (true); UDP 11 6

UDP 프로그램작성 : 실행결과 UDP 12 Ref., http://www.shinyoungbok.pe.kr/ UDP 13 7