public class InetAddressTest {
public static void main(String[] args) {
try {
InetAddress ia = InetAddress.getByName(args[0]);
System.out.println("ia.getHostName()=>"+ia.getHostName());
System.out.println("ia.getHostAddress()=>"+ia.getHostAddress());
System.out.println("InetAddress.getLocalHost()=>"+InetAddress.getLocalHost());
System.out.println("=================");
InetAddress[] iaArr = InetAddress.getAllByName(args[0]);
for(InetAddress ip : iaArr) {
System.out.println("ip.getHostName()=>"+ip.getHostName());
System.out.println("ip.getHostAddress()=>"+ip.getHostAddress());
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
->네이버는 주소가 4개.. 구글/다음은 1개

public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("https://n.news.naver.com/article/018/0005705744");
System.out.println(url);
System.out.println("url.getHost()==>"+url.getHost());
System.out.println("url.getPath()==>"+url.getPath());
//port정보를 리턴
// -1을 리턴하는 것은 -1포트로 접속했다라는 의미가 아니라, 프로토콜에 등록되어있는 기본포트로 접속했다는 의미
//http프로토콜의 기본포트는 80
System.out.println("url.getPort()==>"+url.getPort());
System.out.println("url.getProtocol()==>"+url.getProtocol());
System.out.println("url.getFile()==>"+url.getFile());

// InputStream in = url.openStream();
// InputStreamReader isr = new InputStreamReader(in);
// BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader(
new InputStreamReader(url.openStream()));
while(true) {
String data = br.readLine();
if(data==null) {
break;
}
System.out.println(data);
}

public class URLExam {
public static void main(String[] args) {
String res = "https://search.pstatic.net/common/?src=http%3A%2F%2Fblogfiles.naver.net%2FMjAyMzExMjlfMTI1%2FMDAxNzAxMjU1Njc0NTY5.WpSfeXbN3jP2NrTHnsHe0qTkokSKAh_mOQrwzw0lxo0g.RfoRPTjEiybNNvVFkCQe0FBjAVOOH0CjlqxK9LlTJZUg.PNG.zpfhfhqn%2F%25C8%25AD%25B8%25E9_%25C4%25B8%25C3%25B3_2023-11-29_200101.png&type=sc960_832";
//이미지는 바이트단위로 입출력해야한다
BufferedInputStream bis = null;
//웹상의 리소스인 이미지와 연결해서 데이터를 읽기위한 스트림클래스
FileOutputStream fos = null;
//읽은 데이터를 파일로 저장하기 위해서 필요한 스트림 클래스
try {
URL url = new URL(res);
//1.URL에 전달한 자원에 접속해서 데이터를 읽기
bis = new BufferedInputStream(url.openStream());
fos = new FileOutputStream("src/data/myImg.jpg");
//2.읽어온 스트림을 파일로 저장
int data = 0;
while ((data=bis.read())!=-1) {
fos.write(data);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bis!=null) bis.close();
if(fos!=null) fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public class MyNetServer01 {
public static void main(String[] args) {
try {
//1. 클라이언트와 통신할 수 있도록 준비
// =>ServerSocket객체를 생성하기
ServerSocket server = new ServerSocket(50000);//65535번까지 사용가능?
System.out.println("서버준비완. 클라이언트 접속기다림");
/*2. 클라이언트가 접속하면 클라이언트 정보를 가져와서 통신준비를 해야한다.
접속한 클라이언트 정보를 객체로 만들어서 리턴
클라이언트가 접속할때까지 대기하면서 클라이언트가 접속하면 클라이언트와 통신할 수 있도록 Socket객체를 만들어서 리턴*/
while(true) {
Socket client = server.accept();
InetAddress clientIp = client.getInetAddress();
System.out.println("접속한 클라이언트=>"+clientIp.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class MyNetClient01 {
public static void main(String[] args) {
try {
Socket socket = new Socket("43.202.143.132",50000);
System.out.println("서버에 접속완료");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class DataOutputStreamTest {
public static void main(String[] args) throws Exception{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("src/data/dos.txt"));
dos.writeInt(1000);
dos.writeDouble(10.5);
dos.writeUTF("문자열");
dos.close();
}
}
public class DataInputStreamTest {
public static void main(String[] args) throws Exception{
DataInputStream dis = new DataInputStream(new FileInputStream("src/data/dos.txt"));
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readUTF());
dis.close();
}
}
출력 :
1000
10.5
문자열
public class MyNetServerTest02 {
public static void main(String[] args) {
Socket client = null;
InputStream in = null;//클라이언트가 보내오는 데이터를 읽기위한 스트림객체
DataInputStream dis = null;//클라이언트가 보내오는 데이터를 읽기 위한 보조스트림
OutputStream os = null; //클라이언트에게 보낼 데이터를 출력하기 위한 스트림객체
DataOutputStream dos = null;//클라이언트에게 보낼 데이터를 출력하기 위한 보조스트림
try {
//1.ServerSocket을 생성해서 port를 열고 클라이언트 접속을 기다림
ServerSocket server = new ServerSocket(12345);
System.out.println("클라이언트 접속을 기다림...");
while (true) {
//2.클라이언트가 접속하면 접속한 클라이언트와 통신할 수 있는 소켓객체를 생성
client = server.accept();
String ip = client.getInetAddress().getHostAddress();
System.out.println(ip+"접속");
// 3.클라이언트와 통신할 수 있도록 스트림객체를 생성
// 1) 클라이언트가 전송하는 데이터를 읽기 위한 스트림객체를 클라이언트소켓객체를 통해 생성
in = client.getInputStream();
dis = new DataInputStream(in);
//2)클라이언트에게 전송할 데이터를 보내기 위한 스트림객체를 클라이언트소켓객체를 통해 생성
os = client.getOutputStream();
dos = new DataOutputStream(os);
//===========================통신하기
//4.서버(output 2번)-> 클라이언트 (순서가 중요)
dos.writeUTF(ip+"님 접속을 환영합니다.");
dos.writeInt(20000);
//5.서버(input) <- 클라이언트
String clientMsg = dis.readUTF();
System.out.println("클라이언트가 전송한 메세지>>>>"+clientMsg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class MyNetClientTest02 {
public static void main(String[] args) {
Socket socket = null;
InputStream in = null;//서버가 보내오는 데이터를 읽기위한 스트림객체
DataInputStream dis = null;//서버가 보내오는 데이터를 읽기 위한 보조스트림
OutputStream os = null; //서버에게 보낼 데이터를 출력하기 위한 스트림객체
DataOutputStream dos = null;//서버에게 보낼 데이터를 출력하기 위한 보조스트림객체
try {
//1. 서버에 접속
socket = new Socket("192.168.0.4",12345);
//2.서버와 통신하기 위한 input/output스트림객체 생성
in = socket.getInputStream();
dis = new DataInputStream(in);
os = socket.getOutputStream();
dos = new DataOutputStream(os);
//3.클라이언트(input) <- 서버(서버가 보내온 메세지를 읽기 -2번 연속 읽기)
String serverMsg1 = dis.readUTF();
int serverMsg2 = dis.readInt();
System.out.println("서버가 전송한 메세지1>>>"+serverMsg1);
System.out.println("서버가 전송한 메세지2>>>"+serverMsg2);
//4.클라이언트 -> 서버
dos.writeUTF("안녕하세요 서버님...클라이언트입니다.");
} catch (IOException e) {
e.printStackTrace();
}
}
}

public class MyNetServerExam01 {
public static void main(String[] args) {
Socket client = null;
DataInputStream dis = null;
DataOutputStream dos = null;
ServerSocket server = null;
try {
server = new ServerSocket(12321);
while(true) {
client = server.accept();
String ip = client.getInetAddress().getHostAddress();
System.out.println(ip+"접속");
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
dos.writeUTF("안녕, 환영합니다. "+ip+"님");
dos.writeUTF("출력할 단:");
int dan = dis.readInt();
for(int i=1; i<=9;i++) {
System.out.println(dan+"*"+i+"="+(dan*i));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class MyNetClientExam01 {
public static void main(String[] args) {
Socket sock = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
sock = new Socket("192.168.0.4",12321);
dis = new DataInputStream(sock.getInputStream());
dos = new DataOutputStream(sock.getOutputStream());
String serM = dis.readUTF();
String serM2 = dis.readUTF();
System.out.println(serM);
System.out.println(serM2);
dos.writeInt(7);
} catch (IOException e) {
e.printStackTrace();
}
}
}


BufferedReader와 PrintWriter로 성능향상
public class MyNetServerTest03 {
public static void main(String[] args) {
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
try {
ServerSocket server = new ServerSocket(12321);
System.out.println("클라이언트 접속을 기다림");
while(true) {
client = server.accept();
String ip = client.getInetAddress().getHostAddress();
System.out.println(ip+"접속");
in = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
out = new PrintWriter(
client.getOutputStream(),true);
out.println("안녕, 환영합니다. "+ip+"님");
out.println("출력할 단:");
int dan = Integer.parseInt(in.readLine());
System.out.println("클라이언트가 전송한 메세지 : "+dan);
for(int i=1; i<=9;i++) {
System.out.println(dan+"*"+i+"="+(dan*i));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class MyNetServerTest03 {
public static void main(String[] args) {
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
try {
ServerSocket server = new ServerSocket(12321);
System.out.println("클라이언트 접속을 기다림");
while(true) {
client = server.accept();
String ip = client.getInetAddress().getHostAddress();
System.out.println(ip+"접속");
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
out.println("안녕, 환영합니다. "+ip+"님");
out.println("출력할 단:");
int dan = Integer.parseInt(in.readLine());
System.out.println("클라이언트가 전송한 메세지 : "+dan);
for(int i=1; i<=9;i++) {
System.out.println(dan+"*"+i+"="+(dan*i));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class EchoServerTest01 {
public static void main(String[] args) {
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
try {
ServerSocket server = new ServerSocket(12321);
System.out.println("클라이언트 접속을 기다림");
while(true) {
client = server.accept();
String ip = client.getInetAddress().getHostAddress();
System.out.println(ip+"접속");
//클라이언트와 통신할 수 있는 Input/Output 스트림 클래스를 소켓에서 가져오기
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
//클라이언트가 보내는 데이터를 계속 읽어서 클라이언트에게 다시 보내주는 작업을 처리
String reM = ""; //클라이언트가 보내오는 메세지를 저장할 변수
while(true) {
//서버 <- 클라이언트
reM = in.readLine();
if(reM==null) {
break;
}
System.out.println("클라이언트>>"+reM);
//서버 -> 클라이언트
out.println(reM+"^^");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class EchoClientTest01 {
public static void main(String[] args) {
Socket sock = null;
BufferedReader in = null;
PrintWriter out = null;
BufferedReader keyin = null;
try {
//1.서버접속
sock = new Socket("192.168.0.4",12321);
//2.서버와 통신할 입출력스트림과 키보드로 입력하는 내용을 읽을 스트림을 생성
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream(),true);
keyin = new BufferedReader(new InputStreamReader(System.in));
String senM = "";//서버로 보낼 메세ㅣ
String reM = "";//서버에서 받는 메세지
while((senM=keyin.readLine())!=null) {
//클라이언트 -> 서버(키보드입력한 데이터 보내기)
out.println(senM);
//클라이언트 <- 서버
reM = in.readLine();
System.out.println("서버>>"+reM);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


public class EchoServerTest02 {
public static void main(String[] args) {
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
try {
ServerSocket server = new ServerSocket(13579);
System.out.println("클라이언트 접속을 기다림");
while(true) {
client = server.accept();
String ip = client.getInetAddress().getHostAddress();
System.out.println(ip+"접속");
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
out.println("안녕하세요 클라이언트님?");
String reM ="";
String today = LocalDate.now().toString();
while(true) {
reM = in.readLine();
if(reM==null) {
break;
}
if(reM.equals("안녕하세요") | reM.equals("하이")) {
System.out.println("클라이언트>>"+reM);
out.println(ip+"님 반가워요");
}else if(reM.equals("오늘 날짜는")) {
System.out.println("클라이언트>>"+reM);
out.println(today);
}else{
System.out.println("클라이언트>>"+reM);
out.println(ip+"님 어여 가~~");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class EchoClientTest02 {
public static void main(String[] args) {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
BufferedReader keyin = null;
try {
socket = new Socket("192.168.0.4",13579);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
keyin = new BufferedReader(new InputStreamReader(System.in));
String reM = in.readLine();
System.out.println(reM);
String reC = "";
String senM = "";
while((senM=keyin.readLine())!=null) {
out.println(senM);
reC = in.readLine();
System.out.println("서버>>"+reC);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

본 포스팅은 멀티캠퍼스의 멀티잇 백엔드 개발(Java)의 교육을 수강하고 작성되었습니다.