●IP주소 : 네트워크주소(3자리) + 호스트주소(1자리)

 - IPv4 (a.b.c.d) : 4byte(32bit)로 각 자리는 1byte(8bit)로써 0~255 정수 범위, IP주소&서브넷마스트 = 네트워크주소

  IP주소 : 192.168.10.90

  서브넷마스크 : 255.255.255.0 

  네트워크주소 : 192.168.10.0

   ->네트워크주소가 같은 호스트들은 같은 네트워크를 사용함

   ->호스트주소가 0: 네트워크 자신, 255: 브로드캐스트 주소 --> 254개의 호스트 연결가능

 - IPv6 (a:b:c:d:e:f:g:h) : 128bit = 16bit씩 8자리

 

●InetAddress 클래스 : IP주소를 다루기 위한 클래스

public static void main(String[] args) throws IOException {
		InetAddress ip = InetAddress.getByName("www.google.com");
		System.out.println("hostname : " + ip.getHostName());		// 호스트이름
		System.out.println("hostaddress : " + ip.getHostAddress()); 	// 호스트주소
		
		InetAddress[] ips = InetAddress.getAllByName("www.google.com");
		for (InetAddress i : ips) {
			System.out.println("ip 주소 : " + i);
		}	
		
		InetAddress local = InetAddress.getLocalHost();
		System.out.println("내컴퓨터 : " + local);
	}

 

●URL (Uniform Resource Locator)

 - 프로토콜://호스트명:포트번호/경로명/파일명?쿼리#참조

public static void main(String[] args) throws Exception {
		URL url = new URL("https://news.naver.com/main/read.nhn?mode=LSD&mid=shm&sid1=101&oid=011&aid=0003753620");
		
		System.out.println("url.getAuthority() : " + url.getAuthority());
		System.out.println("url.getContent() : " + url.getContent());
		System.out.println("url.getDefaultPort() : " + url.getDefaultPort());
		System.out.println("url.getPort() : " + url.getPort());
		System.out.println("url.getFile() : " + url.getFile());
		System.out.println("url.getHost() : " + url.getHost());
		System.out.println("url.getPath() : " + url.getPath());
		System.out.println("url.getProtocol() : " + url.getProtocol());
		System.out.println("url.getQuery() : " + url.getQuery());
		System.out.println("url.getRef() : " + url.getRef());
		System.out.println("url.getUserInfo() : " + url.getUserInfo());
		System.out.println("url.toExternalForm() : " + url.toExternalForm());
		System.out.println("url.toURI() : " + url.toURI());
	}

 

●TCP/IP 프로토콜

 - 연결기반(1:1), 신뢰성 있는 데이터 전송

 - 클라이언트의 OutputStream -> 서버의 InputStream 연결

 - 서버의 OutputStream -> 클라이언트의 InputStream 연결

 - 구현할때 보통 간단한 Client -> 복잡한 Server 순서

 - localhost : 127.0.0.1

 

☆:클라이언트 / ★:서버

1. server 실행(main)

2. ServerSocket serverSocket = new ServerSocket(7777); -> 포트번호 7777로 서버소켓 오픈 

3. Socket socket = new Socket("127.0.0.1", 7777); -> 클라이언트가 서버소켓으로 연결요청 

4. Socket socket = serverSocket.accept(); -> 클라이언트의 연결을 수락해 서버에 새로운 소켓 생성 후 client와 연결 

5. OutputStream out = socket.getOutputStream(); / InputStream in = socket.getInputStream() 

    -> 입출력을 위한 입력스트림,출력스트림을 클라이언트와 서버에 구현  

6. socket.close() & stream.close() -> 입출력스트림 사용 후 닫음 

package ch16;

public class TcpIpServer {

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(7777);
			System.out.println("서버가 준비되었습니다.");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		while (true) {
			try {
				Socket socket = serverSocket.accept();
				System.out.println(socket.getInetAddress() + "로부터 연결요청이 들어왔습니다.");
                
				OutputStream out = socket.getOutputStream();
				DataOutputStream dos = new DataOutputStream(out);
//				DataOutputStream out = new DateOutputStream(socket.getOutputStream());
				InputStream in = socket.getInputStream();
				DataInputStream dis = new DataInputStream(in);
//				DataInputStream in = new DataInputStream(socket.getInputStream));
				
				dos.writeUTF(getTime() + "접속을 환영합니다.");
				
				dos.close(); out.close();
				dis.close(); in.close();
				socket.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
public class TcpIpClient {

	public static void main(String[] args) {
		String serverIp = "127.0.0.1";
		int port = 7777;

		System.out.println("서버에 연결중입니다. 서버IP : " + serverIp);
		Socket socket;
		try {
			socket = new Socket(serverIp, port);

			OutputStream out = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(out);
//			DataOutputStream out = new DateOutputStream(socket.getOutputStream());
			InputStream in = socket.getInputStream();
			DataInputStream dis = new DataInputStream(in);
//			DataInputStream in = new DataInputStream(socket.getInputStream));

			System.out.println("서버로부터 받은 메시지 : " + dis.readUTF());
			System.out.println("연결을 종료합니다.");

			dos.close(); out.close();
			dis.close(); in.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

'Langauge > JAVA' 카테고리의 다른 글

Eclipse(이클립스) UTF-8 한글 인코딩 설정  (0) 2020.10.01
Formatter - printf, String.format 옵션  (0) 2020.06.11
입출력(I/O)  (0) 2020.06.10
스트림(Stream)  (0) 2020.06.04
람다식  (0) 2020.06.04

+ Recent posts