import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.Inet4Address;

import java.net.InetAddress;

import java.util.Random;


public class dailyCode{

public static void main(String[] args){

try {

// InetAddress addr = Inet4Address.getByName("0.0.0.0");

InetAddress addr = Inet4Address.getLocalHost();

Random ran = new Random();

int port = ran.nextInt(65535) + 0;

System.out.println("ip = " + addr + " port = " + port);

DatagramSocket socket = new DatagramSocket(port, addr);

System.out.println("getLocalAddress = " + socket.getLocalAddress() + " getLocalPort = " + socket.getLocalPort());

System.out.println("getInetAddress = " + socket.getInetAddress() + " getLocalSocketAddress = " + socket.getLocalSocketAddress());

System.out.println("getRemoteSocketAddress = " + socket.getRemoteSocketAddress() + " getReuseAddress = " + socket.getReuseAddress());

String str = "111";

DatagramPacket sendPacket = new DatagramPacket(str.getBytes(), str.getBytes().length, addr, port);

String msg = new String(sendPacket.getData(), 0, sendPacket.getLength());

System.out.println("To Server : " + msg);

socket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket(str.getBytes(), str.getBytes().length);

socket.receive(receivePacket);

msg = new String(receivePacket.getData(), 0, receivePacket.getLength());

System.out.println("From Client : " + msg);

} catch (Exception e) {

e.printStackTrace();

}

}



'프로그래밍 > JAVA' 카테고리의 다른 글

자바의 객체 (2)  (0) 2020.07.20
자바의 객체 (1)  (0) 2019.03.19
원형 큐(Circular Queue)  (0) 2016.12.08
Implement Stack  (0) 2016.12.01
LinkedList Implementation  (0) 2016.11.29
How to get google server time (URLConnection Example)  (0) 2016.11.27
URL class Example  (0) 2016.11.27

+ Recent posts