(분산 통신 실습) : 소켓( Sockets). 소켓 : 저수준 패킷 스트림 전송 유닉스 소켓 (multi.incheon.ac.kr 117.16.244.53 및 117.16.244.59 에서 프로그램 ) ( 실습 1) Hello 프로그램 helloserver.c helloclient.c 컴파일 & 실행 ( 실습 2) 일대일 채팅 talk 프로그램 talk_server.c talk_client.c 컴파일 & 실행 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1.1
( 분산 통신 실습 ) : 소켓 (Sockets)
소켓 : 저수준 패킷 스트림 전송
유닉스 소켓 (multi.incheon.ac.kr 117.16.244.53 및 117.16.244.59 에서 프로그램 ) ( 실습 1) Hello 프로그램 helloserver.c helloclient.c 컴파일 & 실행 ( 실습 2) 일대일 채팅 talk 프로그램 talk_server.c talk_client.c 컴파일 & 실행 ( 실습 3) 다대다 채팅 chat 프로그램 chat_server.c chat_client.c 컴파일 & 실행 ( 과제 ) 다대다 채팅 프로그램 디버깅 ( 채팅 참가자가 exit 해도 진행되도록 )
자바 소켓 ( 실습 1) 시간 (Time-of-Day) 서버 소스 코드 분석 ( 실습 2) 유닉스 inettime 클라이언트와 혼합해서 inettime 서비스를 실행해 보세요 . ( 과제 ) 자바 응용 채팅 프로그램 JavaChatServer.java 와 JavaChatClient.java 및 JavaChatClient.html 자바 애플릿 채팅
프로그램을 자바 응용 채팅 프로그램으로 수정하세요 .
윈도우 소켓 (winsock) ( 실습 1) 윈도우 소켓 chat 프로그램으로 컴파일 & 실행 ( 과제 ) 윈도우 소켓 talk 프로그램 유닉스 talk 프로그램의 winsock 버전을 작성하세요 .
[Sockets 실습 과제 ] 유닉스 소켓 /자바 소켓 /윈도우 소켓 과제 중 택 1 http://marvel.incheon.ac.kr/ 의 Information 의 Unix 의 Socket Programming 참조
인터넷 talk 프로그램 netprog 의 talk_server.c talk_client.c 소스코드 참조 실행
% talk_server 포트번호
% talk_client 서버호스트 IP 주소 포트번호 인터넷 chat 프로그램
하나의 프로세스가 채팅 참가 신청 처리와 클라이언트들의 통신 처리를 다 할 수 있도록 accept() 또는 read() 에서 계속 기다리는 동기(synchronous, blocking) 모드 대신 select() 시스템 호출을 이용하여 소켓을 비동기 (asynchronous, non-blocking) 모드로 이용
netprog 의 chat_server.c chat_client.c 소스 코드 참조 실행
% chat_server 포트번호
% chat_client 서버호스트 IP 주소 포트번호
유닉스 네트워크 프로그래밍 8
select()
select() 소켓에서 발생하는 I/O 변화를 기다리다가 지정된 /O 변화가 발생하면 리턴 됨 초기 소켓 s 와 참가한 클라이언트들의 소켓의 I/O 변화 감지
select() 시스템 호출 형식int select (
int maxfdp1, /* chleo 파일 ( 및 소켓 ) 번호 크기 +1 */
fd_set *readfds, /* 읽기 상태 변화를 감지할 소켓 지정 */
fd_set *writefds, /* 쓰기 상태 변화를 감지할 소켓 지정 */
fd_set *exceptfds, /* 예외 상태 변화를 감지할 소켓 지정 */
struct timeval *tvptr); /*select() 시스템 호출이 기다리는 시간 */ FD_ZERO(fd_set *fdset) 매크로로 모든 비트 지움 FD_SET(int fd, fd_set *fdset) 매크로로 소켓 fd 의 fdset 변화를
감지하기 위해 fdset 중 fd 에 해당하는 비트를 1 로 지정 FD_ISSET(int fd, fd_set *fdset) 매크로는 fdset 의 해당 fd 비트가 1
로 지정되어 있으면 양수값 리턴하므로 fd 에게 fdset 변화가 발생했는지 판단
1.9
Time-of-Day: Server.javaimport java.net.*;
import java.io.*;
public class DateServer
{
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(6013);
// now listen for connections
while (true) {
Socket client = sock.accept();
// we have a connection
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
// write the Date to the socket
pout.println(new java.util.Date().toString());
// close the socket and resume listening for more connections
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
1.10
Time-of-Day: Client.javaimport java.net.*;
import java.io.*;
public class DateClient
{
public static void main(String[] args) {
try {
// this could be changed to an IP name or address other than the localhost
Socket sock = new Socket("127.0.0.1",6013);
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String line;
while( (line = bin.readLine()) != null)
System.out.println(line);
sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
EchoServer.java
import java.net.*;
import java.io.*;
public class EchoServer
{
public static final int DEFAULT_PORT = 6007;
public static final int BUFFER_SIZE = 256;
public static void main(String[] args) throws IOException {
ServerSocket sock = null;
byte[] buffer = new byte[BUFFER_SIZE];
InputStream fromClient = null;
OutputStream toClient = null;
try {
// establish the socket
sock = new ServerSocket(DEFAULT_PORT);
while (true) {
/**
* now listen for connections
*/
Socket client = sock.accept();
/**
* get the input and output streams associated with the socket.
*/
fromClient = new BufferedInputStream(client.getInputStream());
toClient = new BufferedOutputStream(client.getOutputStream());
1.11
int numBytes;
/** continually loop until the client closes the connection */
while ( (numBytes = fromClient.read(buffer)) != -1) {
toClient.write(buffer,0,numBytes);
toClient.flush();
}
fromClient.close();
toClient.close();
client.close();
}
}
catch (IOException ioe) { }
finally {
if (sock != null)
sock.close();
}
}}
1.12
talk & chat Winsock versions
윈 도 우 소 켓 (Window socket) 은 유 닉 스 에 서 사 용 되 는 BSD 소 켓 을 계승하기 때문에 윈속에서 사용되는 대부분의 함수와 구조체는 유닉스 버전과 동일하다 . 그러나 윈속을 사용하기 전에 유닉스와 달리 윈속 라이브러리를 초기화 해주고 사용이 끝나면 해제를 시켜줘야 한다 .
초기화 : WSAStartup, 해제 : WSACleanup
talk_client, talk_server 유닉스 버전 : 키보드 입력과 데이터 수신 처리를 fork 를 이용해서 분기 윈도우 버전 : 키보드 입력은 메인에서 , 데이터수신 처리는 쓰레드를
이용
chat_client, chat_server 유닉스 버전 : select 를 사용하여 데이터 I/O 를 비동기적으로 처리 ,
키보드 입력은 fork 를 이용 윈도우 버전 : I/O 방식은 쓰레드에서 select 를 사용 , 키보드 입력은