티스토리 뷰
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("보낼 자료 입력 : ");
String msg;
try {
msg = in.readLine();
DatagramSocket dsoc = new DatagramSocket();
InetAddress ia = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(msg.getBytes(),
msg.getBytes().length, ia, 7777);
dsoc.send(dp);
System.out.println("전송 완료");
dsoc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DatagramSocket dsoc = new DatagramSocket(7777);
byte[] data = new byte[65508];
DatagramPacket dp = new DatagramPacket(data, data.length);
System.out.println("서버 서비스 시작...");
while (true) {
dsoc.receive(dp);
System.out.println("보낸 곳 주소 : "
+ dp.getAddress().getHostAddress());
System.out.println("자료 크기 : " + dp.getLength());
System.out.println("내용 : " + new String(dp.getData()).trim());
}
} catch (Exception e) {
System.out.println("오류 : " + e);
}
}
}
'java,web study > 4주차 (7월 22일~28일)' 카테고리의 다른 글
PreparedStatement를 이용한 CRUD Test (0) | 2013.07.26 |
---|---|
TCP test (0) | 2013.07.24 |
WebSocket (0) | 2013.07.24 |
IPDisplay (0) | 2013.07.24 |
ObjectSerialize (0) | 2013.07.22 |