2020년 9월 11일 금요일

(자바네트워크)JAVA RMI(Remote Method Invocation) “원격 메소드 호출” 이란!, RMI HelloWorld

 (자바네트워크)JAVA RMI(Remote Method Invocation) “원격 메소드 호출” 이란!, RMI HelloWorld


(자바네트워크)JAVA RMI(Remote Method Invocation) “원격 메소드 호출” 이란!, RMI HelloWorld 

 

 

0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
 

0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
0de90730f2f78cde6662151133faa091_1599843
 

 

 

#자바동영상, #자바, #자바RMI, #JAVA, #자바RMI강의, #RMO강의, #RMI강좌, #RMI란, #자바강좌, #자바강의, #자바네트워크강의, #자바네트워크강좌

 

(자바소켓프로그래밍, 콘솔 기반 JAVA채팅 프로그램 동영상)Socket, ServerSocket을 이용한 콘솔기반의 Chatting Program

 


(자바소켓프로그래밍, 콘솔 기반 JAVA채팅 프로그램 동영상)Socket, ServerSocket을 이용한 콘솔기반의 Chatting Program

 

이전 강좌에서 작성한 멀티쓰레드 채팅 예제를 저금 수정하여 콘솔 기반의 채팅 예제를 만들어 보겠습니다. 

 

서버

클라이언트가 접속을 할 때  accept를 하면 클라이언트 상대용 Socket이 만들어 지는데 이를 자바의  ArrayList 에 저장해주고 글을 쓸 때 현재 상대하고 있는 하나의 클라이언트에만 쓰는 것이 아니라 ArrayList에 보관중인 모든 Socket을 꺼내 글을 씁니다.

 

클라이언트

수시로 날아오는 메시지 처리를 위해 글을 읽는 부분을 쓰레드로 빼서 처리합니다.

이렇게 하지 않으면 글을 쓰고 있을 때 상대방이 던지는 메시지를  리얼타임으로 받지 못하고 글을 다 쓴 후 받게 된다.

 

0de90730f2f78cde6662151133faa091_1599832
 


package chat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

class ConsoleChatServer extends Thread {
protected Socket sock;
private static ArrayList<Socket> clients = new ArrayList<Socket>(5);

// ------------------------------------
// Constructor
// ------------------------------------
ConsoleChatServer(Socket sock) {
this.sock = sock;
}

// ------------------------------------
// ArrayList에서 클라이언트 소켓 제거
// 접속 후 나가버리는 경우 클릉 쓸 때 오류가 발생
// ------------------------------------
public void remove(Socket socket) {
for (Socket s : ConsoleChatServer.clients) {
if (socket == s) {
ConsoleChatServer.clients.remove(socket);
break;
}
}
}

// ------------------------------------
// Thread 실행 메소드
// ------------------------------------
public void run() {
InputStream fromClient = null;
OutputStream toClient = null;

try {
System.out.println(sock + ": 연결됨");

fromClient = sock.getInputStream();

byte[] buf = new byte[1024];
int count;
while ((count = fromClient.read(buf)) != -1) {
for (Socket s : ConsoleChatServer.clients) {
if (sock != s) {
toClient = s.getOutputStream();
toClient.write(buf, 0, count);
toClient.flush();
}
}
System.out.write(buf, 0, count);
}
} catch (IOException ex) {
System.out.println(sock + ": 에러(" + ex + ")");
} finally {
try {
if (sock != null) {
sock.close();
// 접속 후 나가버린 클라이언트인 경우 ArrayList에서 제거
remove(sock);
}
fromClient = null;
toClient = null;
} catch (IOException ex) {
}
}
}

// ------------------------------------
// 채팅 서버 메인
// ------------------------------------
public static void main(String[] args) throws IOException {
ServerSocket serverSock = new ServerSocket(9999); // 9999번 포트에 서버생성
System.out.println(serverSock + ": 서버소켓생성");
while (true) {
Socket client = serverSock.accept();
// 접속한 클라이언트 상대용 Socket을 ArrayList에 저장
clients.add(client);
//---------------------------- Thread Start
// 쓰레드는 run 메소드에서 accept로 만들어진 Socket을 이용하여 
// InputStream, OutputStream을 이용하여 글을 읽거나 쓴다.
// -----------------------------------------------
ConsoleChatServer myServer = new ConsoleChatServer(client);
myServer.start();
}
}
}


package chat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

//----------------------------------
// 서버에서 보내오는 값을 받기 위한 쓰레드
//----------------------------------
class ServerHandler extends Thread {
Socket sock = null;

public ServerHandler(Socket sock) {
this.sock = sock;
}

public void run() {
InputStream fromServer = null;
try {
fromServer = sock.getInputStream();

byte[] buf = new byte[1024];
int count;

while ((count = fromServer.read(buf)) != -1)
System.out.write(buf, 0, count);

} catch (IOException ex) {
System.out.println("연결 종료 (" + ex + ")");
} finally {
try {
if (fromServer != null)
fromServer.close();
if (sock != null)
sock.close();
} catch (IOException ex) {
}
}
}
}

//------------------------------
// 클라이언트 메인
//------------------------------
public class ConsoleChatClient {
public static void main(String[] args) throws IOException {
Socket sock = null;
try {
sock = new Socket("localhost", 9999);
System.out.println(sock + ": 연결됨");
OutputStream toServer = sock.getOutputStream();

// ---- 서버에서 보내오는 값을 받기위한 쓰레드
ServerHandler chandler = new ServerHandler(sock);
chandler.start();

byte[] buf = new byte[1024];
int count;
// ---- 콘솔에서 읽은 값을 서버에 보냄
// 키보드 입력을 바이트 단위로 읽어 buf 라는 바이트배열에 기록 후 읽은 바이트수를 리턴
        // 읽을것이 없는 경우 read 메소드는 대기하고 Ctrl + C등 눌러 종료하면 -1이 리턴한다.
        // 스트림의 끝인 경우 -1을 리턴한다.
while ((count = System.in.read(buf)) != -1) {
toServer.write(buf, 0, count);
toServer.flush();
}
} catch (IOException ex) {
System.out.println("연결 종료 (" + ex + ")");
} finally {
try {
if (sock != null)
sock.close();
} catch (IOException ex) {
}
}
}
}



 

#자바동영상, #JAVA동영상, #자바채팅, #JAVA채팅,  #자바채팅프로그램, #채팅소스, #자바채팅소스, #자바멀티쓰레드, #자바채팅예제, #채팅소스다운, #JAVA채팅소스, #자바채팅동영상, #자바채팅영상, #자바강의, #자바강좌, #자바교육, 

2020년 9월 10일 목요일

(동영상,자바네트워크)멀티쓰레드 Echo Client, Server, 자바소켓프로그래밍

(동영상,자바네트워크)멀티쓰레드 Echo Client, Server, 자바소켓프로그래밍 

 

이전 예제인 EchoServer의 경우 동시에 여러 개의 클라이언트를 처리하는데 있어서는 read 메소드의 Blocking으로 인해 어려움이 있다.

즉 동시에 여러 클라이언트의 요구를 처리 하지 못한다.

이문제를 해결하기 위해 다중 스레딩(Multi-Threading)을 구현한 서버를 사용하는 것이다.

다중 스레드 서버는 클라이언트가 접속 할때 마다 1개 이상의 스레드를 만들어 돌리기 때문에 블록킹 I/O 문제를 해결해 준다.

즉 메인 스레드는 클라이언트의 연결을 받기만 하고 클라이언트와 데이터를 주고 받는 일은 별도의 Thread에서 처리 하도록 구성한다.

 

이전 예제의 클라이언트는 그대로 두고 서버만 다시 작성하자.

 

package socket;

 

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

 

class MultiThreadEchoServer extends Thread {  

protected Socket sock;

//----------------------- Constructor

MultiThreadEchoServer (Socket sock) { this.sock = sock;}

public void run() {

try   {                

                System.out.println(sock + ": 연결됨");

 

         InputStream fromClient = sock.getInputStream();

                OutputStream toClient = sock.getOutputStream();

                byte[] buf = new byte[1024];         int count;

                while( (count = fromClient.read(buf)) != -1 ) {

                    toClient.write( buf, 0, count );

        System.out.write(buf, 0, count);

   }

                toClient.close();

                System.out.println(sock + ": 연결 종료");

catch( IOException ex )   {

System.out.println(sock + ": 연결 종료 (" + ex + ")");

 

finally {

try   {

if ( sock != null )  sock.close();

catch( IOException ex ) {}

}        

}

 

//------------------------------------

public static void main( String[] args )   throws IOException    {

        ServerSocket serverSock = new ServerSocket( Integer.parseInt(args[0]) );

        System.out.println(serverSock + ": 서버 소켓 생성");

 

while(true)        {

Socket client = serverSock.accept();

MultiThreadEchoServer myServer = new MultiThreadEchoServer(client);

myServer.start();

        }

    }

}

 

 

#멀티쓰레드, #자바멀티쓰레드, #자바소켓, #자바소켓프로그래밍, #자바동영상, #JAVA, #자바, #멀티쓰레드서버, #MultiThread, #자바멀티쓰레드서버, #자바채팅, #자바채팅프로그램, #자바강의, #자바강좌, #자바네트워크, #자바네트워크강의 

(동영상, 자바네트워크)자바 소켓(Socket, ServerSocket)이란, EchoClient, EchoServer 실습, 클라이언트/서버 소켓프로그래밍 실습

 


(동영상, 자바네트워크)자바 소켓(Socket, ServerSocket)이란, EchoClient, EchoServer 실습, 클라이언트/서버 소켓프로그래밍 
http://ojc.asia/bbs/board.php?bo_table=LecJavaNet&wr_id=111 

 50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742 

50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742
50c4891dcb1f3b1f3f28bb025c42f28d_1599742 package socket;

 

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

 

class EchoServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSock = new ServerSocket(Integer.parseInt(args[0]));

System.out.println(serverSock + ": 서버 소켓 생성");

 

while (true) {

Socket sock = null;

 

try {

sock = serverSock.accept();

System.out.println(sock + ": 연결됨");

InputStream inputStram = sock.getInputStream();

OutputStream outputStreasm = sock.getOutputStream();

byte[] buf = new byte[1024];

int count;

while ((count = inputStram.read(buf)) != -1) {

outputStreasm.write(buf, 0, count);

System.out.write(buf, 0, count);

}

outputStreasm.close();

System.out.println(sock + ": 연결 종료");

} catch (IOException ex)

 

{

System.out.println(sock + ": 연결 종료 (" + ex + ")");

} finally {

try {

if (sock != null)

sock.close();

} catch (IOException ex) {

}

}

}

}

}

 

 

 

package socket;

 

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

 

class EchoClient

{   public static void main( String[] args )

        throws IOException

    {

        Socket sock = null;

        try

        {

            sock = new Socket(args[0], Integer.parseInt(args[1]));

            System.out.println(sock + ": 연결됨");

            OutputStream outputStram = sock.getOutputStream();

            InputStream inputStream = sock.getInputStream();

 

            byte[] buf = new byte[1024];

            int count;

            while( (count = System.in.read(buf)) != -1 )

            {

             outputStram.write( buf, 0, count );

                count = inputStream.read( buf );

                System.out.write( buf, 0, count );

            }

            outputStram.close();

            System.out.println(sock + ": 연결 종료");

        } catch( IOException ex )

        {

            System.out.println("연결 종료 (" + ex + ")");

        } finally 

        {

            try

            {

                if ( sock != null )

                    sock.close();

            } catch( IOException ex ) {}

        }

    }

}      

 

 

 

 

 

 

 

#자바동영상, #자바네트워크, #JAVA동영상, #자바강좌, #자바케트워크강좌, #자바케트워크강의, #자바케트워크교육, #자바소켓, #소켓프로그래밍, #자바소켓프로그래밍, #JAVA소켓, #JAVASocket, #ServerSocket, # 자바Socket

2020년 9월 8일 화요일

(동영상)자바네트워크, URL, URLConnection 클래스 웹페이지 긁어오기

 


(동영상)URL, URLConnection 클래스 웹페이지 긁어오기 동영상 강좌 입니다. 

 

38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
38f67699d2b4f5d7ad0550a49ea025d8_1599602
 #자바동영상, #자바네트워크, #자바네트워크동영상, #자바네트워크강의, #자바네트워크강좌, #자바네트워크교육, #자바네트워크영상, #JAVA강의, #JAVA동영상, #자바URL, #자바URLConnection, #IRLConnection, #openConnection, #JAVA, #JAVANetwork

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...