UDP 및 배열 관련 toString 방법
public class chatDialog {
public static void main(String[] args) throws Exception, IOException {
System.out.println(" :");
// System.out.println(InetAddress.getByName("download.filesfrog.com")
// .getHostAddress());
// byte[] buf = {1,1,2,2,3,2};
// System.out.println(new String(buf));
// System.out.println(Arrays.toString(buf));
// System.out.println(buf.toString());
DatagramSocket ds = new DatagramSocket(1233);
new Thread(new receive(ds)).start();
// IP
InetAddress ia = InetAddress.getByName("yun-pc");//yun-pc localHost 127.0.0.1
new Thread(new send(ia)).start();
}
}
class receive implements Runnable{
DatagramSocket ds ;
public receive(DatagramSocket ds) {
super();
this.ds = ds;
}
public void run() {
try {
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
// IP IP127.0.0.1
String IP = dp.getAddress().getHostAddress();
byte[] get = dp.getData();
System.out.println(IP + ":\r
" + new String(get));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class send implements Runnable{
InetAddress ia;
private DatagramSocket s;
send(InetAddress ia){
this.ia = ia;
}
public void run() {
try {
s = new DatagramSocket();
while (true) {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String line = br.readLine();
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, ia,
1233);
s.send(dp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
배열을 문자열 바이트 배열로 변환하는 방법은 new String(byte[],start,end) 하나뿐입니다.
문자 배열에는 두 가지 new String(char[], start, end)과 String이 있습니다.valueOf(char[] 이름);
배열 도구 클래스 Arrays의 toString(byte[]) 방법 및 배열 자체의 toString() 방법
System.out.println(Arrays.toString("안녕하세요.getBytes()));//[-60, -29, -70, -61]
byte[] buf = {-60, -29, -70, -61};
System.out.println(new String(buf));//안녕하세요.
System.out.println(Arrays.toString(buf));//[-60, -29, -70, -61]
System.out.println(buf.toString());//[B@15bdc50
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java oop 22 Object 클래스Object 클래스 - 모든 클래스의 최고 조상 - 부모가 없는 클래스는 자동적으로 Object 클래스를 상속받게 된다. - 모든 클래스는 Object클래스에 정의된 11개의 메서드를 상속받는다 toString(),...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.