diff --git a/Chat.java b/Chat.java new file mode 100644 index 0000000..a2daa97 --- /dev/null +++ b/Chat.java @@ -0,0 +1,143 @@ +// Chat.java +// �}���`�L���X�g���g���ă`���b�g�@�\��� +// �g���� > java Chat + +import java.net.*; +import java.io.*; +//============================================================================== +public class Chat { +// ��M�X���b�h���쐬�E���s���C���M��S�� + + final byte TTL = 1; //����Z�O�����g�����̂ݓ��B�”\�Ƃ���D + final String MULTICASTADDRESS = ("224.0.0.1") ; + // �}���`�L���X�g�A�h���X224.0.0.1��, + // ���[�^�𒴂��Ȃ��ꍇ�̃A�h���X�D + int port = 6000; + // �`���b�g�p�̃|�[�g�ԍ�,�w�肪�Ȃ����6000 �ԂƂ��܂� + byte[] buff = new byte[1024]; //���M�p�o�b�t�@ + String myname ="" ; // ���p�Җ� + int nameLength = 0; //���p�Җ��̒��� + MulticastSocket soc = null; // �}���`�L���X�g�\�P�b�g + InetAddress chatgroup = null; //�`���b�g�p�A�h���X + + // �R���X�g���N�^���p�Җ��Ȃǂ�ݒ肵�܂� + public Chat(int portno){ + port = portno ; //�|�[�g�ԍ��̐ݒ� + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + System.out.println("Input your name: ") ; + try{ + myname = lineread.readLine() ; + }catch(Exception e){ + e.printStackTrace() ; + System.exit(1) ; + } + System.out.println("Welcome" + myname + "!") ; + myname = myname + ">" ; + nameLength = (myname.getBytes()).length ; + for(int i = 0;i < nameLength;++i) + buff[i] = (myname.getBytes())[i] ; + } + +//----------------------------------------------------------------------------- + public void makeMulticastSocket() { + // MULTICASTADDRESS�ɑ΂��ă}���`�L���X�g�\�P�b�g���쐬�D + try{ + chatgroup + = InetAddress.getByName(MULTICASTADDRESS) ; + soc = new MulticastSocket(port) ; + soc.joinGroup(chatgroup) ; + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } // end of makeMulticastSocket +//----------------------------------------------------------------------------- + public void startListener() { + // �X���b�h�p�N���XListenPacket�̃I�u�W�F�N�g�𐶐����C�N���D + try{ + ListenPacket lisner = + new ListenPacket(soc); + Thread lisner_thread = new Thread(lisner); + lisner_thread.start();//��M�X���b�h�̊J�n + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } // end of startListener +//----------------------------------------------------------------------------- + public void sendMsgs() { + // �}���`�L���X�g�p�P�b�g�̑��M��S��. + try{ + // ���M���[�v + while(true){ + int n = System.in.read( + buff,nameLength ,1024 - nameLength ) ; + if(n > 0){ + DatagramPacket dp + = new DatagramPacket( + buff,n + nameLength,chatgroup,port) ; + soc.send(dp) ; + } + else break ;// ���[�v�I�� + } + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } // end of sendMsgs +//----------------------------------------------------------------------------- + public void quitGroup() { + // �ڑ����I�� + try{ + System.out.println("Connection Closed.") ; + soc.leaveGroup(chatgroup) ; + System.exit(0) ; + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } +//----------------------------------------------------------------------------- + public static void main(String[] arg){ + Chat c = null ; + int portno = 6000 ; +// if(arg.length >= 1) portno = Integer.parseInt(arg[0]) ; + c = new Chat(portno) ; + c.makeMulticastSocket() ; + c.startListener() ; + c.sendMsgs() ; + c.quitGroup() ; + } +} // end of class Chat +//============================================================================== +// ListenPacket�N���X +// �}���`�L���X�g�p�P�b�g����M���܂� +class ListenPacket implements Runnable { + MulticastSocket s = null; + // �R���X�g���N�^�}���`�L���X�g�X���b�h���󂯎��܂� + public ListenPacket(MulticastSocket soc){ + s = soc; + } + // �����̖{�� + public void run(){ + byte[] buff = new byte[1024] ; + try{ + while(true){ + DatagramPacket recv + = new DatagramPacket(buff,buff.length) ; + s.receive(recv) ; + if(recv.getLength() > 0){ + System.out.write(buff,0,recv.getLength()) ; + } + } + }catch(Exception e){ + e.printStackTrace() ; + System.exit(1) ; + } + } +} // end of class ListenPacket diff --git a/ChatServer.java b/ChatServer.java new file mode 100644 index 0000000..f351235 --- /dev/null +++ b/ChatServer.java @@ -0,0 +1,119 @@ +// �`���b�g�T�[�oChatServer.java +// ���̃v���O������,�`���b�g�̃T�[�o�v���O�����ł� +// �g����java ChatServer [�|�[�g�ԍ�] +// �|�[�g�ԍ����ȗ������,�|�[�g�ԍ�6000 �Ԃ��g���܂� +// �N���̗�java ChatServer +// �I���ɂ̓R���g���[��C ����͂��Ă������� + +// ���̃T�[�o�ւ̐ڑ��ɂ�Telnet.java�Ȃǂ��g���Ă������� +// �ڑ����~�߂����Ƃ��ɂ�,�s����quit�Ɠ��͂��Ă������� + +// ���C�u�����̗��p +import java.io.*; +import java.net.*; +import java.util.*; + +// ChatServer�N���X +public class ChatServer { + static final int DEFAULT_PORT = 6000;//�|�[�g�ԍ��ȗ�����6000 �Ԃ��g���܂� + static ServerSocket serverSocket; + static Vector connections; + + // sendAll���\�b�h + // �e�N���C�A���g�Ƀ��b�Z�[�W�𑗂�܂� + public static void sendAll(String s){ + if (connections != null){// �R�l�N�V����������Ύ��s���܂� + for (Enumeration e = connections.elements(); + e.hasMoreElements() ;) { + try { + PrintWriter pw = new PrintWriter(( + (Socket) e.nextElement()).getOutputStream()); + pw.println(s); + pw.flush(); + }catch (IOException ex){} + } + } + System.out.println(s); + } + + // addConnection���\�b�h + // �N���C�A���g�Ƃ̐ڑ���lj����܂� + public static void addConnection(Socket s){ + if (connections == null){ + connections = new Vector(); + } + connections.addElement(s); + } + + // deleteConnection���\�b�h + // ����N���C�A���g�Ƃ̃R�l�N�V�������폜���܂� + public static void deleteConnection(Socket s){ + if (connections != null){ + connections.removeElement(s); + } + } + + // main���\�b�h + // �T�[�o�\�P�b�g�����,�N���C�A���g����̐ڑ���҂��󂯂܂� + public static void main(String[] arg){ + int port = DEFAULT_PORT ; + if (arg.length > 0) port = Integer.parseInt(arg[0]) ; + try { + serverSocket = new ServerSocket(port); + }catch (IOException e){ + System.err.println(e); + System.exit(1); + } + while (true) { + try { + Socket cs = serverSocket.accept(); + addConnection(cs); + Thread ct = new Thread(new clientProc(cs)); + ct.start(); + }catch (IOException e){ + System.err.println(e); + } + } + } +} + +// clientProc�N���X +// �N���C�A���g�����p�X���b�h�̂ЂȌ`�ł� +class clientProc implements Runnable { + Socket s; + BufferedReader in; + PrintWriter out; + String name = null; + ChatServer server = null ; + + //�R���X�g���N�^ + public clientProc(Socket s) throws IOException { + this.s = s; + in = new BufferedReader(new InputStreamReader( + s.getInputStream())); + out = new PrintWriter(s.getOutputStream()); + } + + // �X���b�h�̖{�� + // �e�N���C�A���g�Ƃ̐ڑ��������s���܂� + public void run(){ + try { + while (name == null){ + out.print("�����O�́H: "); + out.flush(); + name = in.readLine(); + } + String line = in.readLine(); + while (!"quit".equals(line)){ + ChatServer.sendAll(name + "> " +line); + line = in.readLine(); + } + ChatServer.deleteConnection(s); + s.close(); + }catch (IOException e){ + try { + s.close(); + }catch (IOException e2){} + } + } +} \ No newline at end of file diff --git a/Clock.java b/Clock.java new file mode 100644 index 0000000..e7f2095 --- /dev/null +++ b/Clock.java @@ -0,0 +1,15 @@ +// RMI�ɂ��NetClock�v���O�����̎����� +// (1)�����[�g�T�[�r�X�̃C���^�t�F�[�X��`�t�@�C�� + +// Clock.java Clock�C���^�t�F�[�X +// ���̃C���^�t�F�[�X�́AClockImpl�N���X�̂��߂̃C���^�t�F�[�X�ł� +// ClockImpl���͂��߂Ƃ��āANetClock��RMI�łɕK�{�̃C���^�t�F�[�X�ł� + +// ���C�u�����̗��p +import java.rmi.Remote; +import java.rmi.RemoteException ; + +// Clock�C���^�t�F�[�X +public interface Clock extends Remote{ + String putTime() throws RemoteException; +} diff --git a/ClockClient.java b/ClockClient.java new file mode 100644 index 0000000..a7ea48a --- /dev/null +++ b/ClockClient.java @@ -0,0 +1,29 @@ +// RMI�ɂ��NetClock�v���O�����̎����� +// (5)�N���C�A���g�v���Z�X�����̃N���X�t�@�C�� + +// ClockClient.java +// ���̃N���X�́A�N���C�A���g�v���Z�X�̃N���X�ł� +// NetClock��RMI�ŃV�X�e���ɂ�����N���C�A���g�̋@�\���L�q���܂� +// RMI���W�X�g���ɂ����閼�O�̎擾��A�T�[�o�ւ̎d���̈˗����s���܂� +// �g�p���@ +// java ClockClient +// �Ȃ��A�N���C�A���g�N���̑O�ɁA�T�[�o�ƃ��W�X�g�����N�����Ă������� + +// ���C�u�����̗��p +import java.rmi.Naming; +import java.rmi.RemoteException; +import java.net.MalformedURLException; +import java.rmi.NotBoundException; + +// ClockClient�N���X +public class ClockClient{ + // main���\�b�h + public static void main(String args[]){ + try{ + Clock c = (Clock)Naming.lookup("//localhost/ClockService") ; + System.out.println(c.putTime()) ; + }catch(Exception e){ + System.out.println(e) ; + } + } +} \ No newline at end of file diff --git a/ClockImpl.java b/ClockImpl.java new file mode 100644 index 0000000..8674eff --- /dev/null +++ b/ClockImpl.java @@ -0,0 +1,26 @@ +// RMI�ɂ��NetClock�v���O�����̎����� +// (2)�����[�g�T�[�r�X�̎����̃N���X�t�@�C�� +// ClockImpl.java +// �����[�g�T�[�r�X�̎����N���X�ł� +// NetClock��RMI�ŃV�X�e���ɂ�����@�\���L�q +// ������Ԃ����\�b�hputTime()��� + +// import java.io.* ; +import java.rmi.RemoteException ; +import java.rmi.server.UnicastRemoteObject ; +import java.util.Date ; + +/// Clock�C���^�t�F�[�X�̎��� +public class ClockImpl extends UnicastRemoteObject + implements Clock{ +//----------------------------- + public ClockImpl() throws RemoteException{ // �R���X�g���N�^ + super(); + } +//----------------------------- + public String putTime(){ // ���݂̎�����Ԃ� + Date d = new Date() ; +// System.out.println(d); + return d.toString() ; + } +} // end of ClockImpl diff --git a/ClockServer.java b/ClockServer.java new file mode 100644 index 0000000..102a6af --- /dev/null +++ b/ClockServer.java @@ -0,0 +1,31 @@ +// RMI�ɂ��NetClock�v���O�����̎����� +// (4)�T�[�o�v���Z�X�����̃N���X�t�@�C�� + +// ClockServer.java +// ���̃N���X�́A�T�[�o�v���Z�X�̃N���X�ł� +// NetClock��RMI�ŃV�X�e���ɂ�����T�[�o�̋@�\���L�q���܂� +// RMI���W�X�g���ɂ����閼�O�̓o�^��A�T�[�o�̋N�����s���܂� +// �g�p���@ +// java ClockServer +// �Ȃ��A�T�[�o�N���̑O�ɁA���W�X�g�����N�����Ă������� +// RMI���W�X�g���̋N���͈ȉ��̂悤�ɂ��܂� +// rmiregistry + +// ���C�u�����̗��p +import java.rmi.Naming; +// ClockServer�N���X +public class ClockServer{ + // �R���X�g���N�^ + public ClockServer(){ + try{ + Clock c = new ClockImpl() ; + Naming.rebind("//localhost/ClockService",c) ; + } catch(Exception e) { + System.out.println(e); + } + } + // main���\�b�h + public static void main(String args[]){ + new ClockServer() ; + } +} diff --git a/DPiClient.java b/DPiClient.java new file mode 100644 index 0000000..aba7c77 --- /dev/null +++ b/DPiClient.java @@ -0,0 +1,119 @@ +// RMI�ɂ�镪�U�����v���O�����̎����� +// (5)�N���C�A���g�v���Z�X�����̃N���X�t�@�C�� + +// DPiClient.java +// ���̃N���X�́A�N���C�A���g�v���Z�X�̃N���X�ł� +// ���U�����V�X�e���ɂ�����N���C�A���g�̋@�\���L�q���܂� +// RMI���W�X�g���ɂ����閼�O�̎擾��A�T�[�o�ւ̎d���̈˗����s���܂� +// �g�p���@ +// java DPiClient �T�[�o��1 �T�[�o��2 �E�E�E +// �Ȃ��A�N���C�A���g�N���̑O�ɁA�T�[�o�ƃ��W�X�g�����N�����Ă������� + +// ���C�u�����̗��p +import java.rmi.Naming; +import java.rmi.RemoteException; +import java.net.MalformedURLException; +import java.rmi.NotBoundException; +import java.util.* ; + +//DPiClient�N���X +public class DPiClient{ + + // main���\�b�h + public static void main(String args[]){ + long result=0 ;//�T�[�o�̌v�Z���ʂ��i�[���� + long millis ;//�o�ߎ��� + long maxloopcount=10000000 ;//�_�̌� + int i ;//�T�[�o�̐� + + //�X���b�h���\�����邽�߂̔z���錾���� + // DPiClient�ł́A�T�[�o�̐������X���b�h���쐬���܂� + // �e�X���b�h�̓T�[�o�̌v�Z�����I����҂��A + // �I����Result�N���X�̃I�u�W�F�N�g�Ɍ��ʂ�񍐂��܂� + launchPiServer l[] = new launchPiServer[args.length]; + Thread t[] = new Thread [args.length]; + + //�����̖{�� + try{ + //�������Ԍv���J�n + millis = System.currentTimeMillis() ; + //�����Ŏw�肳�ꂽ�T�[�o�ɏ������˗����܂� + for(i=0;i +// �N����: java MyFTP ftp.ne.jp + +import java.net.*; +import java.io.*; + +public class MyFTP { + Socket ctrlSocket; //����p�\�P�b�g + public PrintWriter ctrlOutput; //����o�͗p�X�g���[�� + public BufferedReader ctrlInput;// �����͗p�X�g���[�� + + final int CTRLPORT = 21 ;// ftp �̐���p�|�[�g + //------------------------------------------------------------------ + //�A�h���X�ƃ|�[�g�ԍ�����\�P�b�g����萧��p�X�g���[�����쐬 + public void openConnection(String host) + throws IOException,UnknownHostException { + ctrlSocket = new Socket(host, CTRLPORT); + ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream()); + ctrlInput + = new BufferedReader(new + InputStreamReader(ctrlSocket.getInputStream())); + } // end of openConnection + //------------------------------------------------------------------ + //����p�̃\�P�b�g��‚��� + public void closeConnection() throws IOException { + ctrlSocket.close() ; + } // end of closeConnection + //------------------------------------------------------------------ + // ftp �T�[�o�Ƀ��O�C�� + public void doLogin() { + String loginName = "" ; + String password = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + + try { + System.out.println("���O�C��������͂��Ă�������") ; + loginName = lineread.readLine() ; + // USER�R�}���h�ɂ�郍�O�C�� + ctrlOutput.println("USER " + loginName) ; + ctrlOutput.flush() ; + // PASS�R�}���h�ɂ��p�X���[�h�̓��� + System.out.println("�p�X���[�h����͂��Ă�������") ; + password = lineread.readLine() ; + ctrlOutput.println("PASS " + password) ; + ctrlOutput.flush() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doLogin + //------------------------------------------------------------------ + // ftp �T�[�o���烍�O�A�E�g + public void doQuit() { + try { + ctrlOutput.println("QUIT ") ;// QUIT�R�}���h�̑��M + ctrlOutput.flush() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doQuit + //------------------------------------------------------------------ + // �f�B���N�g����ύX + public void doCd() { + String dirName = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + + try { + System.out.println("�f�B���N�g��������͂��Ă�������") ; + dirName = lineread.readLine() ; + ctrlOutput.println("CWD " + dirName) ;// CWD�R�}���h + ctrlOutput.flush() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doCd + //------------------------------------------------------------------ + // �f�B���N�g�������擾 + public void doLs(){ + int n ; + byte[] buff = new byte[1024] ; + try { + // �f�[�^�p�R�l�N�V�������쐬 + Socket dataSocket = dataConnection("LIST") ; + + // �f�[�^�ǂݎ��p�X�g���[����p�� + BufferedInputStream dataInput + = new BufferedInputStream(dataSocket.getInputStream()) ; + + // �f�B���N�g������ǂ� + while((n = dataInput.read(buff)) > 0){ + System.out.write(buff,0,n) ; + } + dataSocket.close() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doLs + //------------------------------------------------------------------ + // �T�[�o�Ƃ̃f�[�^�����p�Ƀ\�P�b�g�����܂� + // �܂�,�T�[�o�ɑ΂���port�R�}���h�Ń|�[�g��ʒm���܂� + public Socket dataConnection(String ctrlcmd) + { + String cmd = "PORT " ; //PORT�R�}���h�ő���f�[�^�̊i�[�p�ϐ� + int i ; + Socket dataSocket = null ;// �f�[�^�]���p�\�P�b�g + try { + // �����̃A�h���X�����߂� + byte[] address = InetAddress.getLocalHost().getAddress() ; + + // �K���ȃ|�[�g�ԍ��̃T�[�o�\�P�b�g���쐬 + ServerSocket serverDataSocket = new ServerSocket(0,1) ; + + // PORT�R�}���h�p�̑��M�f�[�^���쐬 + for(i = 0; i < 4; ++i) + cmd = cmd + (address[i] & 0xff) + "," ; + cmd = cmd + (((serverDataSocket.getLocalPort()) / 256) & 0xff) + + "," + + (serverDataSocket.getLocalPort() & 0xff) ; + + // PORT�R�}���h�𐧌�p�X�g���[����ʂ��đ��� + ctrlOutput.println(cmd) ; + ctrlOutput.flush() ; + + // �����ΏۃR�}���h(LIST,RETR,�����STOR�j���T�[�o�ɑ��� + ctrlOutput.println(ctrlcmd) ; + ctrlOutput.flush() ; + + // �T�[�o����̐ڑ����󂯕t���� + dataSocket = serverDataSocket.accept() ; + serverDataSocket.close() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + return dataSocket ; + } // end of dataConnection + //------------------------------------------------------------------ + // �T�[�o��̃t�@�C�����擾 + public void doGet() { + String fileName = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + int n ; + byte[] buff = new byte[1024] ; + + try { + // �T�[�o��t�@�C���̃t�@�C�������w�肵�܂� + System.out.println("�t�@�C��������͂��Ă�������") ; + fileName = lineread.readLine() ; + + // �N���C�A���g��Ɏ�M�p�t�@�C�������� + FileOutputStream outfile = new FileOutputStream(fileName) ; + + // �t�@�C���]���p�f�[�^�X�g���[�����쐬 + Socket dataSocket = dataConnection("RETR " + fileName) ; + BufferedInputStream dataInput + = new BufferedInputStream(dataSocket.getInputStream()) ; + + // �T�[�o����f�[�^���󂯎��,�t�@�C���Ɋi�[ + while((n = dataInput.read(buff)) > 0){ + outfile.write(buff,0,n) ; + } + dataSocket.close() ; + outfile.close() ; + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doGet + //------------------------------------------------------------------ + // �T�[�o�փt�@�C���𑗂�܂� + public void doPut() { + String fileName = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + int n ; + byte[] buff = new byte[1024] ; + + try { + System.out.println("Input File Name: ") ; + fileName = lineread.readLine() ; + + // �N���C�A���g��̃t�@�C���̓ǂݏo���������s���܂� + FileInputStream sendfile = new FileInputStream(fileName) ; + + // �]���p�f�[�^�X�g���[����p�ӂ��܂� + Socket dataSocket = dataConnection("STOR " + fileName) ; + OutputStream outstr = dataSocket.getOutputStream(); + + // �t�@�C����ǂݏo��,�l�b�g���[�N�o�R�ŃT�[�o�ɑ��� + while((n = sendfile.read(buff)) > 0){ + outstr.write(buff,0,n) ; + } + dataSocket.close() ; + sendfile.close() ; + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doPut + //------------------------------------------------------------------ + // Ftp �̃R�}���h���j���[���o�� + public void showMenu() + { + System.out.println("Input Command: ") ; + System.out.print("1(login)") ; + System.out.print(" 2(ls)") ; + System.out.print(" 3(cd)") ; + System.out.print(" 4(get)") ; + System.out.print(" 5(put)") ; + System.out.println(" 9(quit)") ; + } // end of showMenu + //------------------------------------------------------------------ + // �R�}���h����� + public String getCommand() + { + String buf = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + while(buf.length() != 1){// �P�����̓��͂��󂯂�܂ŌJ��Ԃ� + try { + buf = lineread.readLine() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + return (buf) ; + } // end of getCommand + //------------------------------------------------------------------ + // �R�}���h�ɑΉ�����e�������Ăяo�� + public boolean execCommand(String command) + { + boolean cont = true ; + switch (Integer.parseInt(command)){ + case 1 : // login ���� + doLogin() ; + break ; + case 2 : // �T�[�o�̃f�B���N�g���\������ + doLs() ; + break ; + case 3 : // �T�[�o�̍�ƃf�B���N�g���ύX���� + doCd() ; + break ; + case 4 : // �T�[�o����̃t�@�C���擾���� + doGet() ; + break ; + case 5 : // �T�[�o�ւ̃t�@�C���]������ + doPut() ; + break ; + case 9 : // �����̏I�� + doQuit() ; + cont = false ; + break ; + default : //����ȊO�̓��� + System.out.println("�ԍ���I�����Ă�������") ; + } + return(cont) ; + } // end of execCommand + //------------------------------------------------------------------ + // Ftp �̃R�}���h���j���[���o�͂���,�e�������Ăяo���܂� + public void main_proc() + throws IOException + { + boolean cont = true ; + try { + while(cont){ + // ���j���[���o�͂��܂� + showMenu() ; + // �R�}���h���󂯎����s���܂� + cont = execCommand(getCommand()) ; + } + } + catch (Exception e) { + System.err.print(e); + System.exit(1); + } + } // end of main_proc + //------------------------------------------------------------------ + // ����X�g���[���̎�M�X���b�h���J�n���܂� + public void getMsgs(){ + try { + CtrlListen listener = new CtrlListen(ctrlInput) ; + Thread listenerthread = new Thread(listener) ; + listenerthread.start() ; + } catch (Exception e) { + e.printStackTrace() ; + System.exit(1) ; + } + } // end of getMsgs + //------------------------------------------------------------------ + // TCP�R�l�N�V�������J���ď������J�n���܂� + public static void main(String[] arg){ + if (arg.length != 1) { + System.out.println("Usage: MyFTP "); + System.exit(1); + } + try { + MyFTP f = null; + f = new MyFTP(); + f.openConnection(arg[0]); // ����p�R�l�N�V�����̐ݒ� + f.getMsgs() ; // ��M�X���b�h�̊J�n +System.out.println("309"); + f.main_proc(); // ftp ���� +System.out.println("311"); + + f.closeConnection() ; // �R�l�N�V�����̃N���[�Y + System.exit(0) ; // �v���O�����̏I�� + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of main +} // end of class MyFTP +//============================================================================= +// CtrlListen �N���X +class CtrlListen implements Runnable{ + BufferedReader ctrlInput = null ; + // �R���X�g���N�^�ǂݎ���̎w�� + public CtrlListen(BufferedReader in){ + ctrlInput = in ; + } + + public void run(){ + while(true){ + try { // �s��ǂݎ��C�W���o�͂ɃR�s�[ + System.out.println(ctrlInput.readLine()) ; + } catch (Exception e){ + System.exit(1) ; + } + } + } +} // end of class CtrlListen diff --git a/NetClock.java b/NetClock.java new file mode 100644 index 0000000..c88f033 --- /dev/null +++ b/NetClock.java @@ -0,0 +1,46 @@ +// �����𓚂���T�[�o�v���O����Netclock.java +// ���̃v���O�����̓|�[�g�ԍ�6000�Ԃœ��삷��T�[�o�ł� +// �N���C�A���g����̐ڑ��ɑ΂�,������Ԃ��܂� +// ���̃v���O�������~������ɂ̓R���g���[��C����͂��Ă������� +// �g����java Netclock + +// ���C�u�����̗��p +import java.io.* ; +import java.net.* ; +import java.util.* ; + +// Netclock�N���X +class NetClock{ + public static void main(String args[]){ + ServerSocket servsock = null ;// �T�[�o�p�\�P�b�g + Socket sock ;// �\�P�b�g�̓ǂݏ����p�I�u�W�F�N�g + OutputStream out ;// �o�̓X�g���[�� + String outstr ;// �o�̓f�[�^���i�[���镶���� + int i ;//�o�͂̌J��Ԃ�����p�ϐ� + Date d ;// ���t���������p�I�u�W�F�N�g + + try{ + // �T�[�o�\�P�b�g�̍쐬 + servsock = new ServerSocket(6000,300) ; + // �T�[�o�����̌J��Ԃ� + while(true){ + sock = servsock.accept() ;//�ڑ���t + // �o�͗p�f�[�^�̍쐬 + d = new Date() ; + outstr = "\n" + + "Hello, this is Netclock server." + + "\n" + d.toString() + "\n" + + "Thank you." + "\n"; + // �f�[�^�̏o�� + out = sock.getOutputStream() ; + for(i = 0; i < outstr.length();++i) + out.write((int)outstr.charAt(i)) ; + out.write('\n') ; + // �ڑ��I�� + sock.close() ; + } + }catch(IOException e){ + System.exit(1) ; + } + } +} \ No newline at end of file diff --git a/Phttpd.java b/Phttpd.java new file mode 100644 index 0000000..5a4cbce --- /dev/null +++ b/Phttpd.java @@ -0,0 +1,73 @@ +// ���񂿂�HTTP�T�[�oPhttpd.java +// ���̃v���O�����̓|�[�g�ԍ�80�Ԃœ��삷��T�[�o�ł� +// �g����java Phttp �f�[�^�t�@�C���� +// WWW�N���C�A���g����̐ڑ��ɑ΂�,�����Ŏw�肵���t�@�C����Ԃ��܂� + +// ���C�u�����̗��p +import java .io.* ; +import java.net.* ; +import java.util.* ; + +// Phttpd�N���X +class Phttpd{ + public static void main(String args[]){ + // �T�[�o�\�P�b�g + ServerSocket servsock = null ; + Socket sock ; + + // ���o�� + OutputStream out ; + BufferedReader in ; + FileInputStream infile = null; + byte[] buff = new byte[1024]; + + // ���̑� + boolean cont = true ; + int i ; + + try{ + // �T�[�o�p�\�P�b�g�̍쐬�i�|�[�g�ԍ�8000�ԁj + servsock = new ServerSocket(8000,300) ; + while(true){ + sock = servsock.accept() ;// �ڑ��v���̎�t + // �ڑ���̕\�� + System.out.println("Connection Requst from: " + + (sock.getInetAddress())) ; + // �I�u�W�F�N�ginfile�����,�t�@�C�����������܂� + try{ + infile = new FileInputStream(args[0]) ; + } + catch(Exception e){ + // �t�@�C�������̎��s + System.err.println("�t�@�C��������܂���") ; + System.exit(1) ; + } + // �ǂݏ����p�I�u�W�F�N�g�̐��� + in = new BufferedReader(new + InputStreamReader(sock.getInputStream())); + out = sock.getOutputStream() ; + // �Ƃ肠�������s���Q�“ǂݔ�΂� + for(i = 0; i < 2;++i) + System.out.println(in.readLine() ); + // �t�@�C���̏o�� + cont = true ; + while(cont){ + // �t�@�C������̓ǂݍ��݂ƃl�b�g���[�N�o�� + try{ + int n = infile.read(buff); + System.out.write(buff, 0, n); + out.write(buff,0,n) ; + } + catch(Exception e){ + cont = false ; + } + } + // �ڑ��I�� + sock.close() ; + infile.close() ; + } + }catch(IOException e){ + System.exit(1) ; + } + } +} \ No newline at end of file diff --git a/Pi.java b/Pi.java new file mode 100644 index 0000000..33e9437 --- /dev/null +++ b/Pi.java @@ -0,0 +1,14 @@ +// RMI�ɂ�镪�U�����v���O�����̎����� +// (1)�����[�g�T�[�r�X�̃C���^�t�F�[�X��`�t�@�C�� + +// Pi.java Pi�C���^�t�F�[�X +// ���̃C���^�t�F�[�X�́APiImpl�N���X�̂��߂̃C���^�t�F�[�X�ł� + +// ���C�u�����̗��p +import java.rmi.Remote; +import java.rmi.RemoteException ; + +// Pi�C���^�t�F�[�X +public interface Pi extends Remote{ + long putPi(long maxloopcount) throws RemoteException; +} \ No newline at end of file diff --git a/PiClient.java b/PiClient.java new file mode 100644 index 0000000..24d7d29 --- /dev/null +++ b/PiClient.java @@ -0,0 +1,48 @@ +// RMI�ɂ�镪�U�����v���O�����̎����� +// (5)�N���C�A���g�v���Z�X�����̃N���X�t�@�C�� + +// PiClient.java +// ���̃N���X�́A�N���C�A���g�v���Z�X�̃N���X�ł� +// ���U�����V�X�e���ɂ�����N���C�A���g�̋@�\���L�q���܂� +// RMI���W�X�g���ɂ����閼�O�̎擾��A�T�[�o�ւ̎d���̈˗����s���܂� +// �g�p���@ +// java PiClient �T�[�o�� +// �Ȃ��A�N���C�A���g�N���̑O�ɁA�T�[�o�ƃ��W�X�g�����N�����Ă������� + +// ���C�u�����̗��p +import java.rmi.Naming; +import java.rmi.RemoteException; +import java.net.MalformedURLException; +import java.rmi.NotBoundException; +import java.util.* ; + +//PiClient�N���X +public class PiClient{ + + // main���\�b�h + public static void main(String args[]){ + long result ;//�T�[�o�̕Ԃ��l + long millis ;//�o�ߎ��� + long maxloopcount=10000000 ;//��������_�̌� + + try{ + //�v�Z�J�n + System.out.println("Start") ; + //���݂̎����i�~���b�j + millis = System.currentTimeMillis() ; + //rmiregistry�ɂ��T�[�o�̌��� + Pi p = (Pi)Naming.lookup("//"+args[0]+"/PiService") ; + //�T�[�o��putPI���\�b�h�ɂ��΂̌v�Z + result=p.putPi(maxloopcount); + //�v�Z�I���A�o�ߎ��Ԃ̑��� + //�o�ߎ��Ԃɂ̓l�b�g���[�N�������܂܂�܂� + millis = System.currentTimeMillis() - millis ; + + //�΂̒l�ƌo�ߎ��Ԃ̏o�� + System.out.println((double)result/maxloopcount*4) ; + System.out.println((double)millis/1000 + "sec") ; + }catch(Exception e){ + System.out.println(e) ; + } + } +} \ No newline at end of file diff --git a/PiImpl.java b/PiImpl.java new file mode 100644 index 0000000..5fd01bd --- /dev/null +++ b/PiImpl.java @@ -0,0 +1,37 @@ +// RMI�ɂ�镪�U�����v���O�����̎����� +// (2)�����[�g�T�[�r�X�̎����̃N���X�t�@�C�� + +// PiImpl.java +// ���̃N���X�́A�����[�g�T�[�r�X�̎����N���X�ł� +// ���U�����V�X�e���ɂ�����@�\���L�q���܂� +// ��̓I�ɂ́A�����e�J�����@�ɂ���/4���v�Z���܂� + +// ���C�u�����̗��p +import java.rmi.RemoteException ; +import java.rmi.server.UnicastRemoteObject ; +import java.lang.Math ; + +// PiImpl�N���X +public class PiImpl extends UnicastRemoteObject + implements Pi{ + + // �R���X�g���N�^PiImpl + public PiImpl() throws RemoteException{ + super(); + } + + // putPi���\�b�h + // �����e�J�����@�ɂ���/4���v�Z���܂� + public long putPi(long maxloopcount){ + long i ; // �_�����̌J��Ԃ��� + long in=0 ; // ���a�P�̉~���ɓ_�����݂���ꍇ�̐� + double x,y ; // �����_���ɐ�������_��x,y���W + for(i=0;i.length �́C�z��̗v�f�̐���\���D +// �Ȃ��C< �� >�ň͂܂ꂽ���{���Java�̍\���̗v�f�������D + +public class PrintArgs{ + public static void main(String[] args){ + for(int i = 0; i< args.length; i += 1) + System.out.println("args["+ i+ "] = "+ args[i]); + } +} diff --git a/ReadFile.java b/ReadFile.java new file mode 100644 index 0000000..6f9090e --- /dev/null +++ b/ReadFile.java @@ -0,0 +1,39 @@ +// File: ReadFile.java +// �t�@�C���̓��e��ǂݎ��,���̂܂܉�ʂɏo�͂��� +// �g����: java Readfile <�t�@�C����> + +import java.io.*; + +public class ReadFile { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + FileInputStream infile = null; + + try{ + infile = new FileInputStream(args[0]) ; + } + catch(FileNotFoundException e){ // �t�@�C�������̎��s + + System.err.println("Error: File not found.") ; + System.exit(1) ; + } + + while (true) { + try { + int n = infile.read(buff); + System.out.write(buff, 0, n) ; + } + catch(Exception e){ // �ǂݏo���I�� + break; // �J��Ԃ����I�� + } + } + + try{ + infile.close() ; + } + catch(IOException e){ + System.err.println("Error(close).") ; + System.exit(1) ; + } + } +} diff --git a/ReadFile2.java b/ReadFile2.java new file mode 100644 index 0000000..5ccbbb3 --- /dev/null +++ b/ReadFile2.java @@ -0,0 +1,37 @@ +// File: ReadFile2.java +// �t�@�C���̓��e��ǂݎ��,���̂܂܉�ʂɏo�͂��� +// �g����: java Readfile2 <�t�@�C����> + +public class ReadFile2 { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + FileInputStream infile = null; + + try{ + infile = new java.io.FileInputStream(args[0]) ; + } + catch(java.io.FileNotFoundException e){ // �t�@�C�������̎��s + + System.err.println("Error: File not found.") ; + System.exit(1) ; + } + + while (true) { + try { + int n = infile.read(buff); + System.out.write(buff, 0, n) ; + } + catch(java.io.Exception e){ // �ǂݏo���I�� + break; // �J��Ԃ����I�� + } + } + + try{ + infile.close() ; + } + catch(java.io.IOException e){ + System.err.println("Error(close).") ; + System.exit(1) ; + } + } +} diff --git a/ReadNet.java b/ReadNet.java new file mode 100644 index 0000000..6bf1635 --- /dev/null +++ b/ReadNet.java @@ -0,0 +1,54 @@ +// ReadNet.java +// �l�b�g���[�N��̃T�[�o����f�[�^���󂯎��,���̂܂܉�ʂɏo�͂��܂� +// �g����java ReadNet DNS ���|�[�g�ԍ� +// ��java ReadNet kiku.fuis.fukui-u.ac.jp 6000 + +//���C�u�����̗��p +import java.io.*; +import java.net.* ; + +// Readnet�N���X +public class ReadNet { + // �v���O�����̖{��main + public static void main(String[] args){ + byte[] buff = new byte[1024];//�z��̒�` + Socket readsocket = null ;// �T�[�o�ڑ��p�\�P�b�g + InputStream instr = null;// �f�[�^�ǂݎ��p�I�u�W�F�N�g + boolean cont = true ; + // �w��̃|�[�g�ɑ΂���,�\�P�b�g���쐬���܂� + // �I�u�W�F�N�ginstr�����,�f�[�^�ǂݏo�����������܂� + try{ + readsocket + = new Socket(args[0],Integer.parseInt(args[1])) ; + instr = readsocket.getInputStream() ; + } + catch(Exception e){ + System.err.println("�l�b�g���[�N�G���[�ł�") ; + System.exit(1) ; + } + + // �f�[�^�̏I���܂�,�ȉ��̃��[�v���J��Ԃ��܂� + while (cont) { + try { + // �ǂݍ��� + int n = instr.read(buff); + // System.out�ւ̏����o�� + System.out.write(buff, 0, n) ; + } + // �ȉ��͗�O�����ł� + catch(Exception e){ + // �ǂݏo���I�����Ƀ��[�v���I�����܂� + cont = false ; + } + } + // �R�l�N�V������‚��܂� + try{ + instr.close() ; + } + catch(Exception e){ + // �l�b�g���[�N�N���[�Y���s�ł� + System.err.println("�l�b�g���[�N�̃G���[�ł�") ; + System.exit(1) ; + } + } +} \ No newline at end of file diff --git a/ReadWrite.java b/ReadWrite.java new file mode 100644 index 0000000..3dcccbd --- /dev/null +++ b/ReadWrite.java @@ -0,0 +1,18 @@ +// Readwrite.java +// �L�[�{�[�h����̓��͂�,���̂܂܉�ʂɏo�͂��� +// ���̃v���O�������I������ɂ�,�R���g���[��C����͂��� +import java.io.*; +public class ReadWrite { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + while (true) { + try { + int n = System.in.read(buff); + System.out.write(buff, 0, n); + } + catch(Exception e){ + System.exit(1); + } + } + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java new file mode 100644 index 0000000..91db35f --- /dev/null +++ b/Sample.java @@ -0,0 +1,10 @@ +public class Sample { + public static void main(String[] args) { + open(args[0]); // �O���� + proc(); // �又�� + close(); // �㏈�� + } + static void open (String s) { } + void proc () { } + void close () { } +} diff --git a/SeaGameClient.java b/SeaGameClient.java new file mode 100644 index 0000000..15904ba --- /dev/null +++ b/SeaGameClient.java @@ -0,0 +1,278 @@ +// File: SeaGameClient.java +// �C�Q�[���̃N���C�A���g�v���O���� +// Usage: java SeaGameClient +// �N������login�{�^����������,�ڑ���T�[�o�̖��O�◘�p�҂̖��O��₢���킹�Ă��� +// �T�[�o���Ɨ��p�Җ�����͂��� +// ������OK �{�^����������,�|�[�g�ԍ�10000 �ԂŃT�[�o�Ɛڑ� +// +// �v���O�������~����ɂ�logout �{�^�������� + +import java.awt.*; // �O���t�B�b�N�X +import java.awt.event.*;// �C�x���g�֘A +import java.net.*; // �l�b�g���[�N�֘A +import java.io.*; +import java.util.*; + +public class SeaGameClient implements Runnable { + Frame f;// �N���C�A���g���\���p�E�B���h�E + Panel p;// �㉺���E�̈ړ��{�^���ƊC�̏�Ԃ�\������p�l�� + Canvas c;// �C�̏�Ԃ�\������L�����o�X + + //--------------------------------------------------------------------- + // GUI ��ʂ̏����z�u + public SeaGameClient () { + Button b; + f = new Frame();//�N���C�A���g���E�B���h�E�S�̂̕\�� + p = new Panel();//�C�\�������Ƒ���{�^���̕\�� + p.setLayout(new BorderLayout()); + + // up�{�^���̍쐬 + b = new Button("up"); + b.addActionListener(new ActionListener(){ + // up�{�^���������ꂽ��up�R�}���h�𑗐M + public void actionPerformed(ActionEvent e){ + sendCommand("up"); + } + }); + p.add(b, BorderLayout.NORTH); + + // left�{�^���̍쐬 + b = new Button("left"); + b.addActionListener(new ActionListener(){ + // left�{�^���������ꂽ��left�R�}���h�𑗐M + public void actionPerformed(ActionEvent e){ + sendCommand("left"); + } + }); + p.add(b, BorderLayout.WEST); + + // right�{�^���̍쐬 + b = new Button("right"); + b.addActionListener(new ActionListener(){ + // right�{�^���������ꂽ��right�R�}���h�𑗐M + public void actionPerformed(ActionEvent e){ + sendCommand("right"); + } + }); + p.add(b, BorderLayout.EAST); + + // down�{�^���̍쐬 + b = new Button("down"); + b.addActionListener(new ActionListener(){ + // down�{�^���������ꂽ��down�R�}���h�𑗐M + public void actionPerformed(ActionEvent e){ + sendCommand("down"); + } + }); + p.add(b, BorderLayout.SOUTH); + + // �C��̗l�q��\������Canvas���쐬 + c = new Canvas(); + c.setSize(256,256);// �傫���̐ݒ� + // �t���[���ɕK�v�ȕ��i�̎��t�� + p.add(c); + f.add(p); + + // �t���[��f��login�{�^���̎��t�� + b = new Button("login"); + b.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + // login�{�^���������ꂽ�ꍇ�̏��� + // �T�[�o���Z�b�g����Ă��Ȃ����login���� + if(server == null) login(); + } + }); + f.add(b,BorderLayout.NORTH); + + // �t���[��f��logout�{�^���̎��t�� + b = new Button("logout"); + b.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + logout(); + } + }); + f.add(b,BorderLayout.SOUTH); + + // �t���[��f��\�����܂� + f.setSize(335,345); + f.setVisible(true); + } // end of SeaGameClient + //-------------------------------------------------------------------- + // run���\�b�h�^ 500�~���b���Ƃɉ�ʂ��X�V + public void run(){ + while (true){ + try { + Thread.sleep(500); + }catch(Exception e){ + } + // repain���\�b�h��p����,�T�[�o��̏�����ʂɏo�͂��܂� + repaint(); + } + } // end of run + //--------------------------------------------------------------------- + // login�����֘A�̃I�u�W�F�N�g + int sx = 100; + int sy = 100; + TextField host, tf_name; + Dialog d; + + //--------------------------------------------------------------------- + // login�E�B���h�E��\�����C�K�v�ȏ��𓾂� + // ���ۂ�login�����́CrealLogin���\�b�h + void login(){ + // �E�B���h�E�̕\���ƃf�[�^�̓��� + d = new Dialog(f, true); + host = new TextField(10) ; + tf_name = new TextField(10) ; + d.setLayout(new GridLayout(3,2)); + d.add(new Label("host:")); + d.add(host); + d.add(new Label("name:")); + d.add(tf_name); + Button b = new Button("OK"); + b.addActionListener(new ActionListener(){ + // ���͂�����������,readlLogin���\�b�h���g���ăT�[�o��login���܂� + public void actionPerformed(ActionEvent e){ + realLogin(host.getText(), tf_name.getText()); + d.dispose(); + } + }); + d.add(b); + d.setResizable(true); + d.setSize(200, 150); + d.setVisible(true); + (new Thread(this)).start(); + } // end of login + //--------------------------------------------------------------------- + // realLogin�֘A�̃I�u�W�F�N�g + Socket server;// �Q�[���T�[�o�Ƃ̐ڑ��\�P�b�g + int port = 10000; // �ڑ��|�[�g + BufferedReader in; // ���̓X�g���[�� + PrintWriter out; // �o�̓X�g���[�� + String name; // �Q�[���Q���҂̖��O + + //--------------------------------------------------------------------- + // �T�[�o�ւ̃��O�C������ + void realLogin(String host, String name){ + try { + // �T�[�o�Ƃ̐ڑ� + this.name = name; + server = new Socket(host, port); + in = new BufferedReader(new InputStreamReader( + server.getInputStream())); + out = new PrintWriter(server.getOutputStream()); + + // login�R�}���h�̑��t + out.println("login " + name); + out.flush(); + repaint(); + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of realLogin + //--------------------------------------------------------------------- + //�@�T�[�o����̃��O�A�E�g + void logout(){ + try { + // logout�R�}���h�̑��t + out.println("logout"); + out.flush(); + server.close(); + }catch (Exception e){ + ; + } + System.exit(0); + } // end of logout + //--------------------------------------------------------------------- + // �T�[�o����Q�[���̏��𓾂�,�N���C�A���g�̉�ʍĕ`�� + void repaint(){ + // �T�[�o��stat�R�}���h�𑗕t��,�Ֆʂ̗l�q�Ȃǂ̏����擾 + out.println("stat"); + out.flush(); + + try { + String line = in.readLine();// �T�[�o����̓��͂̓ǂݍ��� + Graphics g = c.getGraphics();// Canvas c�ɊC�̗l�q��\�� + + // �C�̕`�� (�‚��l�p�`) + g.setColor(Color.blue); + g.fillRect(0, 0, 256, 256); + + //ship_info����n�܂�D�̏��̐擪�s��T�� + while (!"ship_info".equalsIgnoreCase(line)) + line = in.readLine(); + + // �D�̏��ship_info�̕\�� + // ship_info�̓s���I�h�݂̂̍s�ŏI�� + line = in.readLine(); + while (!".".equals(line)){ + StringTokenizer st = new StringTokenizer(line); + String obj_name = st.nextToken().trim(); // ���O��ǂݎ��܂� + + + // �����̑D�͐�(red)�ł�,���l�̑D�͗�(green)�ŕ\�� + if (obj_name.equals(name)) //�����̑D + g.setColor(Color.red); + else // ���l�̑D + g.setColor(Color.green); + + // �D�̈ʒu���W��ǂݎ�� + int x = Integer.parseInt(st.nextToken()) ; + int y = Integer.parseInt(st.nextToken()) ; + + + g.fillOval(x - 10, 256 - y - 10, 20, 20); // �D��\�� + g.drawString(st.nextToken(),x+10,256-y+10) ; // ���_��D�̉E���ɕ\�� + g.drawString(obj_name,x+10,256-y-10) ; // ���O��D�̉E��ɕ\�� + + line = in.readLine(); + } // end while (!".".equals(line)) + + // energy_info����n�܂�,�R���^���N�̏��̑҂��� + while (!"energy_info".equalsIgnoreCase(line)) + line = in.readLine(); + + // �R���^���N�̏��energy_info�̕\�� + line = in.readLine(); + while (!".".equals(line)){ // �s���I�h�݂̂̍s�łȂ��ԁC�J��Ԃ� + StringTokenizer st = new StringTokenizer(line); + + // �R���^���N�̈ʒu���W��ǂݎ�� + int x = Integer.parseInt(st.nextToken()) ; + int y = Integer.parseInt(st.nextToken()) ; + + // �R���^���N�𔒔����̐ԊۂŎ��� + g.setColor(Color.red); + g.fillOval(x - 5, 256 - y - 5, 10, 10); + g.setColor(Color.white); + g.fillOval(x - 3, 256 - y - 3, 6, 6); + + line = in.readLine(); + } // end while (!".".equals(line)) + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of repaint + //--------------------------------------------------------------------- + // �T�[�o�փR�}���h�𑗐M + void sendCommand(String s){ + if ("up".equals(s)){ + out.println("up"); + }else if ("down".equals(s)){ + out.println("down"); + }else if ("left".equals(s)){ + out.println("left"); + }else if ("right".equals(s)){ + out.println("right"); + } + out.flush(); + } + + // main���\�b�h + // SeaGameClient���N�����܂� + public static void main(String[] arg){ + new SeaGameClient(); + } +} // end of class SeaGameClient diff --git a/SeaGameServer.java b/SeaGameServer.java new file mode 100644 index 0000000..27fc837 --- /dev/null +++ b/SeaGameServer.java @@ -0,0 +1,320 @@ +// ��C�Q�[����T�[�o�v���O����SeaGameServer.java +// ���̃v���O������,�C�Q�[���̃T�[�o�v���O�����ł� +// �g����java SeaGameServer +// �N�������,�|�[�g�ԍ�10000 �Ԃɑ΂���N���C�A���g����̐ڑ���҂��܂� +// �v���O�������~����ɂ̓R���g���[��C ����͂��Ă������� + +import java.io.*; +import java.net.*; +import java.util.*; + +public class SeaGameServer { + static final int DEFAULT_PORT = 10000; + //SeaGameServer�ڑ��p�|�[�g�ԍ� + static ServerSocket serverSocket; + static Vector connections; + //�N���C�A���g�Ƃ̃R�l�N�V������ێ�����Vector�I�u�W�F�N�g + static Vector energy_v; // �R���^���N�̈ʒu��񃊃X�g + static Hashtable userTable = null; + // �N���C�A���g�֘A���o�^�p�e�[�u�� + static Random random = null; + + // addConnection���\�b�h + // �N���C�A���g�Ƃ̐ڑ���Vector�I�u�W�F�N�gconnections�ɓo�^���܂� + public static void addConnection(Socket s){ + if (connections == null){//���߂ẴR�l�N�V�����̏ꍇ��, + connections = new Vector();// connections���쐬���܂� + } + connections.addElement(s); + } + + // deleteConnection���\�b�h + // �N���C�A���g�Ƃ̐ڑ���connections����폜���܂� + public static void deleteConnection(Socket s){ + if (connections != null){ + connections.removeElement(s); + } + } + + // loginUser���\�b�h + // login�R�}���h�̏����Ƃ���,���p�҂̖��O��D�̈ʒu��o�^���܂� + public static void loginUser(String name){ + if (userTable == null){// �o�^�p�e�[�u�����Ȃ���΍쐬���܂� + userTable = new Hashtable(); + } + if (random == null){// �����̏��������܂� + random = new Random(); + } + // �D�̏����ʒu�𗐐��Ō��肵�܂� + int ix = Math.abs(random.nextInt()) % 256; + int iy = Math.abs(random.nextInt()) % 256; + + // �N���C�A���g�̖��O��D�̈ʒu��\�ɓo�^���܂� + userTable.put(name, new Ship(ix, iy)); + // �T�[�o���̉�ʂɂ��N���C�A���g�̖��O��\�����܂� + System.out.println("login:" + name); + System.out.flush(); + } + + // logoutUser���\�b�h + // �N���C�A���g�̃��O�A�E�g���������܂� + public static void logoutUser(String name){ + // �T�[�o����ʂɃ��O�A�E�g����N���C�A���g�̖��O��\�����܂� + System.out.println("logout:" + name); + System.out.flush(); + // �o�^�p�e�[�u�����獀�ڂ��폜���܂� + userTable.remove(name); + } + + // left���\�b�h + // �������̑D�����ɓ�������,�R���^���N���E���邩�ǂ������肵�܂� + // ����ɂ�calculation���\�b�h���g���܂� + public static void left(String name){ + Ship ship = (Ship) userTable.get(name); + ship.left(); + calculation(); + } + + // right���\�b�h + // �������̑D���E�ɓ�������,�R���^���N���E���邩�ǂ������肵�܂� + // ����ɂ�calculation���\�b�h���g���܂� + public static void right(String name){ + Ship ship = (Ship) userTable.get(name); + ship.right(); + calculation(); + } + + // up���\�b�h + // �������̑D����ɓ�������,�R���^���N���E���邩�ǂ������肵�܂� + // ����ɂ�calculation���\�b�h���g���܂� + public static void up(String name){ + Ship ship = (Ship) userTable.get(name); + ship.up(); + calculation(); + } + + // down���\�b�h + // �������̑D�����ɓ�������,�R���^���N���E���邩�ǂ������肵�܂� + // ����ɂ�calculation���\�b�h���g���܂� + public static void down(String name){ + Ship ship = (Ship) userTable.get(name); + ship.down(); + calculation(); + } + + // calculation���\�b�h + // �R���^���N�ƑD�̈ʒu�֌W�𒲂ׂ�,�R���^���N���E���邩�ǂ������肵�܂� + static void calculation(){ + if (userTable != null && energy_v != null){ + // ���ׂẴN���C�A���g�ɂ‚��Ĕ��肵�܂� + for (Enumeration users = userTable.keys(); + users.hasMoreElements();) { + // ���肷��N���C�A���g�̖��O�ƑD�̈ʒu�����o���܂� + String user = users.nextElement().toString(); + Ship ship = (Ship) userTable.get(user); + // �R���^���N���ׂĂɂ‚���,�D�Ƃ̈ʒu�֌W�𒲂ׂ܂� + for (Enumeration energys = energy_v.elements(); + energys.hasMoreElements();) { + // �R���^���N�̈ʒu�ƑD�̈ʒu�𒲂�,�������v�Z���܂� + int[] e = (int []) energys.nextElement(); + int x = e[0] - ship.x; + int y = e[1] - ship.y; + double r = Math.sqrt(x * x + y * y); + // ����"10"�ȓ��Ȃ�R���^���N����荞�݂܂� + if (r < 10) { + energy_v.removeElement(e); + ship.point++; + } + } + } + } + } + //--------------------------------------------------------------------- + // STAT�R�}���h������ + // �N���C�A���g�ɑD�̏��iship_info)��, + // �C���Y�����Ă���R���^���N�̏���(energy_info)�𑗐M + public static void statInfo(PrintWriter pw){ + // �D�̏��(ship_info)�̑��M + pw.println("ship_info"); + if (userTable != null){ + for (Enumeration users = userTable.keys(); + users.hasMoreElements();) { + String user = users.nextElement().toString(); + Ship ship = (Ship) userTable.get(user); + pw.println(user + " " + ship.x + " " + + ship.y + " " + ship.point); + } + } + pw.println(".");// ship_info�̏I�� + // �R���^���N�̏��ienergy_info�j�̑��M + pw.println("energy_info"); + if (energy_v != null){ + // ���ׂĂ̔R���^���N�̈ʒu�����N���C�A���g�ɑ��M���܂� + for (Enumeration energys = energy_v.elements(); + energys.hasMoreElements();) { + int[] e = (int []) energys.nextElement(); + pw.println(e[0] + " " + e[1]); + } + } + pw.println(".");// enegy_info�̏I�� + pw.flush(); + } + //--------------------------------------------------------------------- + // �R���^���N���P�‚����C��Ƀ����_���ɔz�u + public static void putEnergy(){ + if (energy_v == null){// ���߂Ĕz�u����ꍇ�̏��� + energy_v = new Vector(); + } + if (random == null){// ���߂ė������g���ꍇ�̏��� + random = new Random(); + } + // �����ňʒu�����߂ĊC��ɔz�u���܂� + int[] e = new int[2]; + e[0] = Math.abs(random.nextInt()) % 256; + e[1] = Math.abs(random.nextInt()) % 256; + + energy_v.addElement(e); + } // end of putEnergy + //--------------------------------------------------------------------- + // �T�[�o�\�P�b�g�̍쐬�ƃN���C�A���g�ڑ��̏��� + // ����ѓK���ȃ^�C�~���O�ł̔R���^���N�̒����lj����� + public static void main(String[] arg){ + try {// �T�[�o�\�P�b�g�̍쐬 + serverSocket = new ServerSocket(DEFAULT_PORT); + }catch (IOException e){ + System.err.println("can't create server socket."); + System.exit(1); + } + // �R���^���N�����ɒlj�����X���b�het�����܂� + Thread et = new Thread(){ + public void run(){ + while(true){ + try { + sleep(10000);// �X���b�het��10000�~���b�x�~�����܂� + }catch(InterruptedException e){ + break; + } + // �C��ɂP�”R���^���N��z�u���܂� + SeaGameServer.putEnergy(); + } + } + }; + // et���X�^�[�g���܂� + et.start(); + // �\�P�b�g�̎�t��,�N���C�A���g�����v���O�����̊J�n�������s���܂� + while (true) {// �������[�v + try { + Socket cs = serverSocket.accept(); + addConnection(cs);// �R�l�N�V������o�^���܂� + // �N���C�A���g�����X���b�h���쐬���܂� + Thread ct = new Thread(new clientProc(cs)); + ct.start(); + }catch (IOException e){ + System.err.println("client socket or accept error."); + } + } + } // end of main +} // end of class putEnergy +//============================================================================= +class clientProc implements Runnable { + Socket s; // �N���C�A���g�ڑ��p�\�P�b�g + BufferedReader in; // ���̓X�g���[�� + PrintWriter out; // �o�̓X�g���[�� + String name = null; // �N���C�A���g�̖��O + + //-------------------------------------------------------------------- + // �\�P�b�g���g���ē��o�̓X�g���[�����쐬���܂� + public clientProc(Socket s) throws IOException { + this.s = s; + in = new BufferedReader(new InputStreamReader( + s.getInputStream())); + out = new PrintWriter(s.getOutputStream()); + } + //-------------------------------------------------------------------- + public void run(){ + try { + //LOGOUT�R�}���h��M�܂ŌJ��Ԃ��܂� + while (true) { + // �N���C�A���g����̓��͂�ǂݎ��܂� + String line = in.readLine(); + // name����̏ꍇ�ɂ�LOGIN�R�}���h�݂̂��󂯕t���܂� + if (name == null){ + StringTokenizer st = new StringTokenizer(line); + String cmd = st.nextToken(); + if ("login".equalsIgnoreCase(cmd)){ + name = st.nextToken(); + SeaGameServer.loginUser(name); + }else{ + // LOGIN�R�}���h�ȊO�͖��� + } + }else{ + // name����łȂ��ꍇ�̓��O�C���ς݁D�R�}���h���󂯕t����D + StringTokenizer st = new StringTokenizer(line); + String cmd = st.nextToken();// �R�}���h�̎��o�� + // �R�}���h�ɑΉ����鏈�� + if ("STAT".equalsIgnoreCase(cmd)){ + SeaGameServer.statInfo(out); + } else if ("UP".equalsIgnoreCase(cmd)){ + SeaGameServer.up(name); + } else if ("DOWN".equalsIgnoreCase(cmd)){ + SeaGameServer.down(name); + } else if ("LEFT".equalsIgnoreCase(cmd)){ + SeaGameServer.left(name); + } else if ("RIGHT".equalsIgnoreCase(cmd)){ + SeaGameServer.right(name); + } else if ("LOGOUT".equalsIgnoreCase(cmd)){ + SeaGameServer.logoutUser(name); + // LOGOUT�R�}���h�̏ꍇ�ɂ͌J��Ԃ����I�� + break; + } + } + } + // �o�^�����폜��,�ڑ���ؒf + SeaGameServer.deleteConnection(s); + s.close(); + } catch (IOException e) { + try { + s.close(); + } catch (IOException e2){} + } + } // end of run +} // enf of class clientProc +//============================================================================= +// �D�̈ʒu��,�l�������R���^���N�̐����Ǘ� +class Ship { + + int x, y; // �D�̈ʒu���W + int point = 0; // �l�������R���^���N�̌� + + //------------------------------------------------------------------- + // �����ʒu���Z�b�g���܂� + public Ship(int x, int y){ + this.x = x; + this.y = y; + } + //------------------------------------------------------------------- + // �D�����ɓ����� + public void left(){ + x -= 10; + if (x < 0) x += 256; // ���̕ӂ͉E�̕ӂɂ‚Ȃ����Ă��� + } + //------------------------------------------------------------------- + // �D���E�ɓ����� + public void right(){ + x += 10; + x %= 256; // �E�̕ӂ͍��̕ӂɂ‚Ȃ����Ă��� + } + //------------------------------------------------------------------- + // �D����ɓ����� + public void up(){ + y += 10; + y %= 256; // ��̕ӂ͉��̕ӂɂ‚Ȃ����Ă��� + } + //------------------------------------------------------------------- + // �D�����ɓ����� + public void down(){ + y -= 10; + if (y < 0) y += 256; // ���̕ӂ͏�̕ӂɂ‚Ȃ����Ă��� + + } +} // end of class Ship diff --git a/T1.java b/T1.java new file mode 100644 index 0000000..59f2ff5 --- /dev/null +++ b/T1.java @@ -0,0 +1,99 @@ +// telnet�̌��`�ƂȂ�v���O����T1.java +// ���̃v���O������,�w�肳�ꂽ�A�h���X�̃|�[�g�ɕW�����o�͂�ڑ����܂� +// �g����java T1 �T�[�o�A�h���X�|�[�g�ԍ� +// �N���̗�java T1 kiku.fuis.fukui-u.ac.jp 80 +// �I���ɂ̓R���g���[��C ����͂��Ă������� + +// ���C�u�����̗��p +import java.net.*; +import java.io.*; + +// T1�N���X +// T1�N���X��,�l�b�g���[�N�ڑ��̊Ǘ����s���܂� +// StreamConnector�N���X��p���ăX���b�h�������s���܂� +public class T1 { + // �\�P�b�g�̏��� + protected Socket serverSocket;//�ڑ��p�\�P�b�g + public OutputStream serverOutput;//�l�b�g���[�N�o�͗p�X�g���[�� + public BufferedInputStream serverInput;// �����͗p�X�g���[�� + + // openConnection���\�b�h + //�A�h���X�ƃ|�[�g�ԍ�����\�P�b�g�����X�g���[�����쐬���܂� + public void openConnection(String host,int port) + throws IOException,UnknownHostException + { + serverSocket = new Socket(host, port); + serverOutput = serverSocket.getOutputStream(); + serverInput + = new BufferedInputStream(serverSocket.getInputStream()); + } + + // main_proc���\�b�h + // �l�b�g���[�N�Ƃ̂��Ƃ������X���b�h���X�^�[�g�����܂� + public void main_proc() + throws IOException + { + try { + // �X���b�h�p�N���XStreamConnector�̃I�u�W�F�N�g�𐶐����܂� + StreamConnector stdin_to_socket = + new StreamConnector(System.in, serverOutput); + StreamConnector socket_to_stdout = + new StreamConnector(serverInput, System.out); + // �X���b�h�𐶐����܂� + Thread input_thread = new Thread(stdin_to_socket); + Thread output_thread = new Thread(socket_to_stdout); + // �X���b�h���N�����܂� + input_thread.start(); + output_thread.start(); + } + catch(Exception e){ + System.err.print(e); + System.exit(1); + } + } + + // main���\�b�h + // TCP �R�l�N�V�������J���ď������J�n���܂� + public static void main(String[] arg){ + try { + T1 t = null; + t = new T1(); + t.openConnection(arg[0], Integer.parseInt(arg[1])); + t.main_proc(); + }catch(Exception e){ + e.printStackTrace(); + System.exit(1); + } + } +} + +// StreamConnector�N���X +// �X�g���[�����󂯎��C���҂��������ăf�[�^���󂯓n���܂� +// StreamConnector�N���X�̓X���b�h���\�����邽�߂̃N���X�ł� + +class StreamConnector implements Runnable { + InputStream src = null; + OutputStream dist = null; + // �R���X�g���N�^���o�̓X�g���[�����󂯎��܂� + public StreamConnector(InputStream in, OutputStream out){ + src = in; + dist = out; + } + // �����̖{�� + // �X�g���[���̓ǂݏ����𖳌��ɌJ��Ԃ��܂� + public void run(){ + byte[] buff = new byte[1024]; + while (true) { + try { + int n = src.read(buff); + if (n > 0) + dist.write(buff, 0, n); + } + catch(Exception e){ + e.printStackTrace(); + System.err.print(e); + System.exit(1); + } + } + } +} \ No newline at end of file diff --git a/TestThread.java b/TestThread.java new file mode 100644 index 0000000..f6235cf --- /dev/null +++ b/TestThread.java @@ -0,0 +1,35 @@ +// File: TestThread.java +import java.io.* ; + +//----------------------------------------------------------- +public class TestThread{ + public static void main(String[] args){ + System.out.print("TestThread"); + + myThread mt1 = new myThread("1 ", 1300); + myThread mt2 = new myThread("2 ", 2900); + + mt1.start(); + mt2.start(); + } +}; +//----------------------------------------------------------- +class myThread extends Thread { + String s; + int t; + + myThread(String s, int t) { + this.s = s; + this.t = t; + } + public void run() { + while(true) { + try { + System.out.print(this.s); + sleep(t); // t�~���b����x�~ + } catch(InterruptedException e) { + break; + } + } // end while + } // end of run +} // end of class myThread diff --git a/WRNet.java b/WRNet.java new file mode 100644 index 0000000..e5d72c4 --- /dev/null +++ b/WRNet.java @@ -0,0 +1,63 @@ +// WRNet.java +// �l�b�g���[�N��̃T�[�o�ɐڑ���,�f�[�^�𑗂�܂� +// ���̌�T�[�o����f�[�^���󂯎��,���̂܂܉�ʂɏo�͂��܂� +// �g���� java WRNet DNS�� �|�[�g�ԍ� +// �� java WRNet localhost 8000 + +//���C�u�����̗��p +import java.io.*; +import java.net.* ; + +// Wrnet�N���X +public class WRNet { + // �v���O�����̖{��main + public static void main(String[] args){ + byte[] buff = new byte[1024];//�z��̒�` + Socket wrsocket = null ;// �T�[�o�ڑ��p�\�P�b�g + InputStream instr = null;// �f�[�^�ǂݎ��p�I�u�W�F�N�g + OutputStream outstr = null;// �f�[�^�o�͗p�I�u�W�F�N�g + boolean cont = true ; + // �w��̃|�[�g�ɑ΂���,�\�P�b�g���쐬���܂� + // ���o�͂̃X�g���[�������,�f�[�^�ǂݏo�����������܂� + try{ + wrsocket + = new Socket(args[0],Integer.parseInt(args[1])) ; + instr = wrsocket.getInputStream() ; + outstr = wrsocket.getOutputStream() ; + } catch (Exception e) { + System.err.println("Network error.") ; + System.exit(1) ; + } + // + while (cont) { + try { + int n = System.in.read(buff); + if(buff[0] == '.') cont = false ; + else outstr.write(buff,0,n) ; + } + // �ȉ��͗�O�����ł� + catch(Exception e){ + // ��O���̓v���O�������I�����܂� + System.exit(1); + } + } + // �f�[�^�̏I���܂�,�ȉ��̃��[�v���J��Ԃ��܂� + cont = true ; + while (cont) { + try { + int n = instr.read(buff); + System.out.write(buff, 0, n) ; + } + catch(Exception e){ + // �ǂݏo���I�����Ƀ��[�v���I�� + cont = false ; + } + } + try { + instr.close() ; + } catch(Exception e) { + System.err.println("Close failed.") ; + System.exit(1) ; + } + } +} diff --git a/WriteFile.java b/WriteFile.java new file mode 100644 index 0000000..72bf2b5 --- /dev/null +++ b/WriteFile.java @@ -0,0 +1,41 @@ +// WriteFile.java + +// �L�[�{�[�h����̓��͂��󂯎��,���̂܂܉�ʂɏo�͂���D +// �܂�,�t�@�C���ɓ��͂����Ɋi�[����D +// �g����java Writefile �t�@�C���� +// �v���O�������I������ɂ́C�s�̐擪�Ƀs���I�h.����͂���D + +import java.io.*; +public class WriteFile { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + boolean cont = true ; + FileOutputStream outfile = null; // �t�@�C���o�͗p�I�u�W�F�N�g + try{ + outfile = new FileOutputStream(args[0]) ; + } + catch(FileNotFoundException e){ // �t�@�C�������̎��s + System.err.println("File not found") ; + System.exit(1) ; + } + // �s���Ńs���I�h�����͂����܂ŌJ��Ԃ��D + while (cont) { + try { + int n = System.in.read(buff); + System.out.write(buff, 0, n) ; + if(buff[0] == '.') cont = false ; + else outfile.write(buff,0,n) ; + } + catch(Exception e){ + System.exit(1); // �v���O�����̏I�� + } + } + try{ + outfile.close() ; + } + catch(IOException e){ + System.err.println("Error in closing a file") ; + System.exit(1) ; + } + } +} \ No newline at end of file