728x90
2020년 12월 03일 9시 ~ 15시 30분 zoom으로 수업 진행
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<!-- 주석 -->
<div style="color:blue;">
<form action="res.jsp" method="get">
이름 : <input type="text" name="tname" maxlength="30" />
주소 : <input type="text" name="taddr" maxlength="30" />
<br>
<input type="button" value="버튼">
<input type="submit" value="전송">
<input type="reset" value="취소">
</form>
</div>
<pre>
웹은 서버에 페이지를 요청(HTTP Request)할 때 두가지 방식을 취한다
1. queryString 을 포함한 GET 방식
2. 주소줄에 요청 페이지만 있고 queryString은 없는 POST 방식(데이터를 히든으로 처리)
queryString : ?를 포함한 변수=값&변수=값& ... 을 말한다
</pre>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1><%=request.getParameter("tname") %>님의 주소는<%=request.getParameter("taddr") %></h1>
</body>
</html>
package com.test01;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetTest {
public static void main(String[] args) throws UnknownHostException{
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr); //실제 내 컴퓨터의 이름과 ip를 얻어옴
//localhost : 내 컴퓨터 127.0.0.1
System.out.println("localhost : " + addr.getHostAddress());//내 ip주소
System.out.println("host name : " + addr.getHostName());//내 컴터 이름
System.out.println(" InetAddress.getByName(\"localhost\") : " + InetAddress.getByName("localhost"));
//localhost라는 이름으로 아이피를 받아오기 때문ㅇ[ㅔ
//localhost/127.0.0.1 가 출력된다
InetAddress[] addrs = InetAddress.getAllByName("www.naver.com");
for (int i = 0; i < addrs.length; i++) {
System.out.println(addrs[i].getHostName() + " : " + addrs[i].getHostAddress() );
//www.naver.com의 이름으로 확인할 수 있는 아이피 2개를 얻어온다
}
}
}
package com.test02;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class MyServer {
//서버 생성 UDP
//udp는 연결 지향적이지 않아서 서버 소켓을 따로 만들 필요가 없다
public static void main(String[] args) throws IOException{
//소켓 생성
DatagramSocket ds = new DatagramSocket();
System.out.println("[서버]");
//보낼 data
byte[] buff = "연습입니다".getBytes();
//datagramPacket 생성 - 생성 한 다음 클라이언트로 보냄 바이트 배열을 , 바이트배열 길이만큼, 해당 ip에 해당 port로
DatagramPacket sendP = new DatagramPacket(buff, buff.length, InetAddress.getByName("localhost"), 9999);
//로컬호스트의 아이피를 가져오고 해당 아이피의 9999포트로 접근하겠다는 의미
//전송
ds.send(sendP);
System.out.println("전송완료");
//종료
ds.close();
ds.disconnect();
}
}
package com.test02;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class MyClient {
public static void main(String[] args) throws IOException {
//소켓 생성
DatagramSocket ds = new DatagramSocket(9999);//서버에서 9999로 포트 연결헀으니까 같이 통신할 친구도 포트 9999로 연결
System.out.println("[클라이언트]");
//데이터를 받을 배열
byte[] buff = new byte[1024];
//패킷 받음
DatagramPacket receiveP = new DatagramPacket(buff, buff.length);
ds.receive(receiveP);
//소켓이 패킷을 받는다~
System.out.println(new String(receiveP.getData()));//받은 데이터를 String으로 변환해서 출력
//종료
ds.close();
ds.disconnect();
//서버와 클라이언트 두개가 같이 실행되어야 결과 출력된다~
}
}
package com.test03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer {
public static void main(String[] args) {
//서버 생성
ServerSocket serverSocket = null;
//접속된 클라이언트 소켓
Socket serviceSocket = null;
//출력 객체
PrintWriter out = null;
//클라이언트로부터 읽어온 내용을 담을 객체
BufferedReader in = null;
try {
serverSocket = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
System.out.println("Client 를 기다립니다.");
try {
serviceSocket = serverSocket.accept();
System.out.println("Client가 접속했습니다.");
System.out.println("[서버]");
//1. 클라이언트에게 받은 내용을 라인단위로 읽자.
//바이트 단위인 BufferedReader를 문자 단위인 InputStreamReader는 변환해주는 친구
in = new BufferedReader(new InputStreamReader(serviceSocket.getInputStream()));
//2. 클라이언트에게 받은 내용을 보내주자.
out = new PrintWriter(serviceSocket.getOutputStream(), true);//true는 자동플러시
String inputLine;
while((inputLine = in.readLine()) != null) {
//데이터 받고
System.out.println("클라이언트가 보낸 메세지 : " + inputLine);
//받았다고 대답해주는거
out.println(inputLine);
//out.print(inputLine);으로 하면 한번만 통신하고 끝나는데 이유는?
//클라이언트에서 out.print(inputLine);으로 회신받는 걸 readLine()으로 받는데 얘는 엔터를 기준으로 받았다 처리가 되는거기 때문에
//out.print(inputLine); 같이 print로 보내면 엔터없이 데이터가 전송되니까 클라에서 회신 받는중인걸로 착각함ㅋㅋ
}
out.close();
in.close();
serverSocket.close();
serviceSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.test03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SimpleClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket clientSocket = null;
String serverIP = InetAddress.getLocalHost().getHostAddress();
//서버로 데이터를 전송 할 스트림
PrintWriter out = null;
//서버에서 데이터를 받을 스트림
BufferedReader in = null;
//키보드로 입력 받을 스트림
BufferedReader stdIn = null;
System.out.println("서버에 접속합니다.");
clientSocket = new Socket(serverIP, 8888);
//서버로 데이터를 넘기자~
out = new PrintWriter(clientSocket.getOutputStream(), true);
//서버에서 데이터를 받아오자~~
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//콘솔과 연결되어있고 키보드의 입력을 받아온다~
stdIn = new BufferedReader(new InputStreamReader(System.in));
//세가지 스트림 생성해서 연결해따~
//----------------------
String inputLine;
System.out.println("[클라이언트]");
while((inputLine=stdIn.readLine()) != null) {
//서버로 전송
out.println(inputLine);
System.out.println("Server로 부터 : " + in.readLine());
}
stdIn.close();
in.close();
out.close();
clientSocket.close();
}
}
'기록 > 자바_국비' 카테고리의 다른 글
[배운내용정리] 1207 자바 국비교육 (0) | 2020.12.13 |
---|---|
[배운내용정리] 1204 자바 국비교육 (0) | 2020.12.12 |
[배운내용정리] 1202 자바 국비교육 (2) | 2020.12.10 |
[배운내용정리] 1201 자바 국비교육 (0) | 2020.12.02 |
[배운내용정리] 1130 자바 국비교육 (0) | 2020.12.02 |