\

해병 코딩

728x90
반응형

2021.03.29 - [JAVA/JAVA(자바)설치 및 툴 이용(ECLIPSE)] - [JAVA] 자바 설치 및 환경 하기 JDK 1.8 버전

 

[JAVA] 자바 설치 및 환경 하기 JDK 1.8 버전

www.oracle.com/kr/java/technologies/javase/javase-jdk8-downloads.html 위 링크 클릭 하면 여기로 온다 각자의 환경에 맞게 설치 하자 나는 윈도우 64 비트 그전 오라클 로그인 필수 1. 다운로든 된거를 실행..

marine1188.tistory.com

개발 환경

Window 10 

JDK 1.8

이클립스 2020_03 버전


Network UDP

일반적으로 서버에 보는 체팅 

Server class 

서버 코드

package ex01.ne.udp;
import java.io.*;
import java.net.*;
public class DatagramServer {
	public static void main(String[] args) {
		
		//일반적으로 메세지를 받기만 한다
		
		
		DatagramSocket ds = null;
		DatagramPacket dp = null;
		DataOutputStream dos = null;
		
		int port = 5000;
		String str;
		File file = null;
		// 
		try {
			System.out.println("@@@ UDP File Server @@@");
			//소켓 생성
			ds = new DatagramSocket(port);
			
			while(true) {
				dp = new DatagramPacket(new byte[65536],65536);
				ds.receive(dp);//수신
				
				//생성자 만들기 // 객체 자체가 달라 문자열로 출력 해야해서 
				//trim 함수 뒤 공백 제거
				str = new String(dp.getData(),0,dp.getLength()).trim();
				
				//start 문자가들어오면 실행 
				if(str.equalsIgnoreCase("start")) {
					System.out.println("전송되고 있음...");
					file = new File("test.txt");
					
					dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
					
				//end 면 프로그램을 바져 나옴			
				}else if (str.equalsIgnoreCase("end")) {
					System.out.println("end");
					break;
				}else if(file != null) {
					System.out.println(str);
					//파일로 출력하는 코드
					dos.write(dp.getData(),0,dp.getLength());
				}//end if
				
			}// end while
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {dos.close();}catch(Exception e) {e.printStackTrace();}
		}//end try
	}
}





 

 

client class

package ex01.ne.udp;
import java.io.*;
import java.net.*;
public class DatagramClient {
	public static void main(String[] args) {
		DatagramSocket ds = null;
		DatagramPacket dp = null;
		//읽어 드릴 것
		DataInputStream dis = null;
		//읽어 드릴 그릇
		BufferedReader br = null;
		int port = 5000;
		String str;
		byte[]b;
		
		try {
			System.out.println("@@@ UDP File Client @@@");
			br = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("전송 대상(server ip) =");
			String ipAddress =br.readLine();
			System.out.println("전송 파일(파일명 확장자) =");
			String fileName = br.readLine();
			
			File file = new File(fileName);
			
			if(!file.exists()) {
				System.out.println("파일이 존재 하지 않습니다");
				System.exit(0);
			}//end if
			
			
			ds = new DatagramSocket();
			InetAddress ip = InetAddress.getByName(ipAddress);
			str= "start";
			b= str.getBytes();
			
			dp = new DatagramPacket(b,b.length,ip,port);
			ds.send(dp);//송신
			
			dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
			//그릇 선언
			b= new byte[65536];
			
			while(true) {
				int count = dis.read(b,0,b.length);
				if(count == -1)break;
				dp = new DatagramPacket(b,count,ip,port);
				ds.send(dp);
			}//end while
			
			str = "end";
			b= str.getBytes();
			dp = new DatagramPacket(b,b.length,ip,port);
			ds.send(dp);
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {br.close();dis.close();}catch(Exception e) {e.printStackTrace();}
		}//end try
	}
}

 

내 프레젝트 경로에 텍스트 파일 만들고 그안에 내용을 입력 한다

서버 클래스 를 먼저 실행 

 

client class 실행

서버 콘솔에서 

내가 만들었던 sample.txt 의 내용을 읽어서 나타 나낟

 

 


728x90
반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band