https://sites.google.com/site/kernelnetworkprograming/javaprogramming
===================================================================================
網路程式設計 (20140409)
命令提示字元視窗:
1. Server 和 Client的通訊
2. Server 和 兩個 Client的通訊
Windows 環境 Apache Http Server 2.2.25 安裝教學(附載點)
Win32 Binary without crypto (no mod_ssl) (MSI Installer):
httpd-2.0.65-win32-x86-no_ssl.msi
[免費] 如何用 FileZilla Server v0.9.41 架設 FTP 站? (FTP伺服器)
http://www.flag.com.tw/news/FT732.zip
==================================================================================
下載並安裝 Java Platform (JDK) Java SE Downloads 並 參考網頁 "Hello World!" for Microsoft Windows 執行程式 ,使結果出現 Hello World!
Java程式設計 v.s JavaApplet,JavaScript網頁程式
環境變數 path 的設定 執行成功 java 和 javac
https://sites.google.com/site/kernelnetworkprograming/javaprogramming
===================================================================================
網路程式設計 (20140409)
命令提示字元視窗:
1. Server 和 Client的通訊
2. Server 和 兩個 Client的通訊
Win32 Binary without crypto (no mod_ssl) (MSI Installer): httpd-2.0.65-win32-x86-no_ssl.msi
[免費] 如何用 FileZilla Server v0.9.41 架設 FTP 站? (FTP伺服器)
http://www.flag.com.tw/news/FT732.zip
==================================================================================
===================================================================================
網路程式設計 (20140409)
命令提示字元視窗:
1. Server 和 Client的通訊
2. Server 和 兩個 Client的通訊
Windows 環境 Apache Http Server 2.2.25 安裝教學(附載點)
Win32 Binary without crypto (no mod_ssl) (MSI Installer): httpd-2.0.65-win32-x86-no_ssl.msi
[免費] 如何用 FileZilla Server v0.9.41 架設 FTP 站? (FTP伺服器)
http://www.flag.com.tw/news/FT732.zip
==================================================================================
下載並安裝 Java Platform (JDK) Java SE Downloads 並 參考網頁 "Hello World!" for Microsoft Windows 執行程式 ,使結果出現 Hello World!
Java程式設計 v.s JavaApplet,JavaScript網頁程式
環境變數 path 的設定 執行成功 java 和 javac
Java程式設計 v.s JavaApplet,JavaScript網頁程式
環境變數 path 的設定 執行成功 java 和 javac
Eclipse Downloads
作業1 列印字串
程式碼
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello world!");
System.out.println("許志宇");
}
}
結果畫面
==============================================================
=================================================================
作業2 列印9X9乘法表
程式碼
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello world!");
System.out.println("許志宇");
System.out.println("九九乘法表:");
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
結果畫面
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello world!");
System.out.println("許志宇");
System.out.println("九九乘法表:");
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
結果畫面
=================================================================
==============================================================
import java.io.*;
import java.net.*;
class FtpServer
{
public static void main(String args[])
{
//InetAddress.getByName(192.168.20.16);
try{
InetAddress myip1 = InetAddress.getByName("java.sun.com");
System.out.println("主機名稱: " + myip1);
/*
InetAddress[] myip1 = InetAddress.getAllByName("java.sun.com");
for(int i=0; i System.out.println(myip1[i].getHostAddress());
*/
//Socket socket=new Socket(InetAddress.getByName(iaddr),port);
InetAddress myHost = InetAddress.getLocalHost();
//System.out.println("主機名稱: " + myHost.getHostName());
//System.out.println("主機名稱: " + myHost.getHostAddress());
//System.out.println("主機名稱: " + myHost.getByName("M410-"));
String server = "127.0.0.1";
int port = 5566;
//Socket soc = new Socket(server,port);
Socket soc = new Socket();
//soc.bind(server) ;
//System.out.println(args[0]);
//System.out.println(args[1]);
//System.out.println(args[2]);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
}
==============================================================
//Ftp Client
import java.io.*;
import java.net.*;
public class Client13_4 {
int i;
static String iaddr;
static int port;
static String infilename;
public Client13_4() {
try{
Socket socket=new Socket(InetAddress.getByName(iaddr),port);
DataOutputStream outstream = new DataOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream(infilename);
while((i=fis.read()) !=-1)
outstream.writeInt(i);
outstream.writeInt(i);
System.out.println("Data sent to internet successfully!");
socket.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]) {
if (args.length < 3){
System.out.println("USAGE: java Client13_4 [iaddr] [port] [infilename]");
System.exit(1);
}
iaddr = args[0];
port=Integer.parseInt(args[1]);
infilename = args[2];
Client13_4 ClientStart=new Client13_4();
}
}
==============================================================
//FTP server
import java.net.*;
import java.io.*;
public class Server13_4 {
int messagein;
static int port;
static String outfilename;
public Server13_4() {
try{
ServerSocket SS = new ServerSocket(port);
System.out.println("Server is created and waiting Client to connect...");
Socket socket = SS.accept();
System.out.println("Client IP = " +
socket.getInetAddress().getHostAddress());
DataInputStream instream = new DataInputStream(socket.getInputStream());
FileOutputStream fos = new FileOutputStream(outfilename);
while(messagein != -1){
messagein = instream.readInt();
fos.write(messagein);
}
System.out.println("Data written to File successfully!");
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
if(args.length < 2){
System.out.println("Usage: java Server13_4 [port] [outfilename]");
System.exit(1);
}
port=Integer.parseInt(args[0]);
outfilename = args[1];
Server13_4 ServerStart=new Server13_4();
}
}
==============================================================
import java.awt.*;
import java.awt.event.*;
public class ThreeButtonPressDemo {
public static void main(String[] args){
ActionListener a = new MyActionListener();
Frame f = new Frame("Java Frame ");
f.setSize(200, 200);
Button b1 = new Button("確定");
Button b2 = new Button("取消");
Button b3 = new Button("結束");
b1.addActionListener(a);
b2.addActionListener(a);
b3.addActionListener(a);
b1.setSize(50, 50);
FlowLayout flow=new FlowLayout();
f.setLayout(flow);
f.add(b1, flow);
f.add(b2, flow);
f.add(b3, flow);
f.show();
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
else if (s.equals("Bonjour")) {
System.out.println("Good Morning");
}
else {
System.out.println(s + " clicked");
}
}
}
==============================================================
import java.awt.*;
import java.awt.event.*;
public class ButtonPressDemo {
public static void main(String[] args){
Button b;
ActionListener a = new MyActionListener();
Frame f = new Frame("Java Applet");
f.add(b = new Button("Bonjour"), BorderLayout.NORTH);
b.setActionCommand("Good Morning");
b.addActionListener(a);
f.add(b = new Button("Good Day"), BorderLayout.CENTER);
b.addActionListener(a);
f.add(b = new Button("Aurevoir"), BorderLayout.SOUTH);
b.setActionCommand("Exit");
b.addActionListener(a);
f.pack();
f.show();
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
else if (s.equals("Bonjour")) {
System.out.println("Good Morning");
}
else {
System.out.println(s + " clicked");
}
}
}
===============================================================
//Client
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener {
private JTextField JexT = new JTextField();
private JTextArea JtaR = new JTextArea();
DataOutputStream osTS;
DataInputStream iFrS;
public static void main(String[] args) {
new Client();
}
public Client() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter Random Number"), BorderLayout.WEST);
p.add(JexT, BorderLayout.CENTER);
JexT.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p, BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(JtaR), BorderLayout.CENTER);
JexT.addActionListener(this);
setSize(500,300);
setTitle("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
Socket connectToServer = new Socket("localhost", 7000);
iFrS = new DataInputStream(
connectToServer.getInputStream());
osTS =
new DataOutputStream(connectToServer.getOutputStream());
}
catch (IOException ex) {
JtaR.append(ex.toString() + '\n');
}
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JTextField) {
try {
double radius = Double.parseDouble(JexT.getText().trim());
osTS.writeDouble(radius);
osTS.flush();
double area = iFrS.readDouble();
JtaR.append("Number is " + radius + "\n");
JtaR.append("Random Number received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}
===============================================================
//Server
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
private JTextArea JtaR = new JTextArea();
public static void main(String[] args) {
new Server();
}
public Server() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(JtaR), BorderLayout.CENTER);
setSize(450, 300);
setTitle("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
ServerSocket Sersock= new ServerSocket(7000);
JtaR.append("Server started at " + new Date() + '\n');
Socket ConTe = Sersock.accept();
DataInputStream disFC = new DataInputStream(
ConTe.getInputStream());
DataOutputStream dosTC = new DataOutputStream(
ConTe.getOutputStream());
while (true) {
double Numb = disFC.readDouble();
double Aex = Numb*Numb*Math.random();
dosTC.writeDouble(Aex);
JtaR.append("Number received from client: " + Numb + '\n');
JtaR.append("Random number is : " + Aex + '\n');
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
===============================================================
import java.net.*;
{
public static void main(String args[])
{
//InetAddress.getByName(192.168.20.16);
try{
System.out.println("主機名稱: " + myip1);
/*
InetAddress[] myip1 = InetAddress.getAllByName("java.sun.com");
for(int i=0; i
*/
//System.out.println("主機名稱: " + myHost.getHostName());
//System.out.println("主機名稱: " + myHost.getHostAddress());
//System.out.println("主機名稱: " + myHost.getByName("M410-"));
String server = "127.0.0.1";
int port = 5566;
//Socket soc = new Socket(server,port);
Socket soc = new Socket();
//soc.bind(server) ;
//System.out.println(args[0]);
//System.out.println(args[1]);
//System.out.println(args[2]);
}
System.out.println(e.getMessage());
}
import java.net.*;
int i;
static String iaddr;
static int port;
static String infilename;
public Client13_4() {
try{
Socket socket=new Socket(InetAddress.getByName(iaddr),port);
DataOutputStream outstream = new DataOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream(infilename);
while((i=fis.read()) !=-1)
outstream.writeInt(i);
outstream.writeInt(i);
socket.close();
}
System.out.println(e.getMessage());
}
}
if (args.length < 3){
System.out.println("USAGE: java Client13_4 [iaddr] [port] [infilename]");
System.exit(1);
}
port=Integer.parseInt(args[1]);
infilename = args[2];
Client13_4 ClientStart=new Client13_4();
}
}
import java.io.*;
int messagein;
static int port;
static String outfilename;
try{
ServerSocket SS = new ServerSocket(port);
System.out.println("Server is created and waiting Client to connect...");
System.out.println("Client IP = " +
socket.getInetAddress().getHostAddress());
FileOutputStream fos = new FileOutputStream(outfilename);
messagein = instream.readInt();
fos.write(messagein);
}
System.out.println("Data written to File successfully!");
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
if(args.length < 2){
System.out.println("Usage: java Server13_4 [port] [outfilename]");
System.exit(1);
}
port=Integer.parseInt(args[0]);
outfilename = args[1];
Server13_4 ServerStart=new Server13_4();
}
}
import java.awt.event.*;
public class ThreeButtonPressDemo {
public static void main(String[] args){
ActionListener a = new MyActionListener();
Frame f = new Frame("Java Frame ");
f.setSize(200, 200);
Button b1 = new Button("確定");
Button b2 = new Button("取消");
Button b3 = new Button("結束");
b1.addActionListener(a);
b2.addActionListener(a);
b3.addActionListener(a);
FlowLayout flow=new FlowLayout();
f.setLayout(flow);
f.add(b1, flow);
f.add(b2, flow);
f.add(b3, flow);
f.show();
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
else if (s.equals("Bonjour")) {
System.out.println("Good Morning");
}
else {
System.out.println(s + " clicked");
}
}
}
import java.awt.event.*;
public class ButtonPressDemo {
public static void main(String[] args){
Button b;
ActionListener a = new MyActionListener();
Frame f = new Frame("Java Applet");
f.add(b = new Button("Bonjour"), BorderLayout.NORTH);
b.setActionCommand("Good Morning");
b.addActionListener(a);
f.add(b = new Button("Good Day"), BorderLayout.CENTER);
b.addActionListener(a);
f.add(b = new Button("Aurevoir"), BorderLayout.SOUTH);
b.setActionCommand("Exit");
b.addActionListener(a);
f.pack();
f.show();
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
else if (s.equals("Bonjour")) {
System.out.println("Good Morning");
}
else {
System.out.println(s + " clicked");
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
private JTextArea JtaR = new JTextArea();
DataOutputStream osTS;
DataInputStream iFrS;
new Client();
}
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter Random Number"), BorderLayout.WEST);
p.add(JexT, BorderLayout.CENTER);
JexT.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().add(p, BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(JtaR), BorderLayout.CENTER);
setSize(500,300);
setTitle("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
iFrS = new DataInputStream(
connectToServer.getInputStream());
osTS =
new DataOutputStream(connectToServer.getOutputStream());
}
catch (IOException ex) {
JtaR.append(ex.toString() + '\n');
}
}
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JTextField) {
try {
osTS.writeDouble(radius);
osTS.flush();
double area = iFrS.readDouble();
JtaR.append("Number is " + radius + "\n");
JtaR.append("Random Number received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
new Server();
}
getContentPane().add(new JScrollPane(JtaR), BorderLayout.CENTER);
setSize(450, 300);
setTitle("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JtaR.append("Server started at " + new Date() + '\n');
Socket ConTe = Sersock.accept();
DataInputStream disFC = new DataInputStream(
ConTe.getInputStream());
DataOutputStream dosTC = new DataOutputStream(
ConTe.getOutputStream());
double Aex = Numb*Numb*Math.random();
dosTC.writeDouble(Aex);
JtaR.append("Random number is : " + Aex + '\n');
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
vc#
Apache/PHP/MySQL
畢業考( 5/24)
第十二週 (2012年5月17日)
=====================================================================================
import java.awt.*;
import java.awt.event.*;
public class TestTicTacToe extends Frame implements WindowListener, ActionListener
{
// 啟動棋盤, 等待使用者點按
public void init( )
{
Panel pan1 = new Panel();
Panel pan2 = new Panel();
Button button = new Button("Click Me");//000000000
TextField text = new TextField(10);
setLayout( new BorderLayout( 3, 3 ) );
pan1.setLayout( new GridLayout( 3, 3 ) );
pan2.setLayout( new GridLayout( 1, 2 ) );
pan2.add(button);
pan2.add(text);
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 3; j++ )
{
BookKeepArray[ i ][ j ] = new Button( );
pan1.add( BookKeepArray[ i ][ j ] );
BookKeepArray[ i ][ j ].addActionListener( this );
BookKeepArray[ i ][ j ].setFont(new Font("Serif", Font.BOLD, 72));
}
addWindowListener( this );
add("Center",pan1);
add("South",pan2);
newBoard( );
}
public TestTicTacToe( )
{
init( );
}
// 重玩 ! 新建一個TicTacToe的案例, 清除按鈕上的標籤
public void newBoard( )
{
t = new TicTacToe( );
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 3; j++ )
{
BookKeepArray[ i ][ j ].setLabel( "" ); // 無標籤
BookKeepArray[ i ][ j ].setEnabled( true ); // 可點按下棋步
}
}
private int KeepGameInteresting = 0;
// 為電腦選一個棋步
// 若是電腦先下,利用KeepGameInteresting隨機選擇棋步,增加遊戲的變化
// 否則就要呼叫TryOneMove來選個好棋步
public void doComputerMove( boolean predictToWin )
{
BestMove ComputerMove;
if( predictToWin )
ComputerMove = t.TryOneMove( TicTacToe.COMPUTER );
else
{
ComputerMove = new BestMove( 0, KeepGameInteresting % 3, KeepGameInteresting / 3 );
KeepGameInteresting = ( KeepGameInteresting + 1 ) % 9;
}
System.out.println( " 電腦選的列的位置 = " + ComputerMove.row +
" 電腦選的行的位置 = " + ComputerMove.column );
BookKeepArray[ ComputerMove.row ][ ComputerMove.column ].setLabel( computerYouKnowMe );
BookKeepArray[ ComputerMove.row ][ ComputerMove.column ].setEnabled( false );
t.recordMoveInArray( TicTacToe.COMPUTER, ComputerMove.row, ComputerMove.column );
}
// 遊戲結束時(注意結束的條件), 開始另一盤, 圈叉對調
public boolean GameOverRest( boolean condition, String message, boolean ComputerMoves )
{
if( condition )
{
System.out.println( message );
System.out.println( "重新開始新的一盤遊戲!" );
newBoard( );
if( ComputerMoves )
{
System.out.println( "電腦先下囉!" );
computerYouKnowMe = "X";
HUMANOPPYouKnowMe = "O";
doComputerMove( false );
}
else
{
HUMANOPPYouKnowMe = "X";
computerYouKnowMe = "O";
System.out.println( "您先下吧!" );
}
}
return condition;
}
// WindowListener介面的實作
public void windowClosing( WindowEvent event )
{
System.exit( 0 );
}
public void windowClosed( WindowEvent event ) { }
public void windowDeiconified( WindowEvent event ) { }
public void windowIconified( WindowEvent event ) { }
public void windowActivated( WindowEvent event ) { }
public void windowDeactivated( WindowEvent event ) { }
public void windowOpened( WindowEvent event ) { }
// 處理按鈕點選(button click)事件,注意按錯時的處理方式
public void actionPerformed( ActionEvent evt )
{
if( evt.getSource( ) instanceof Button )
{ // 棋手每按一下棋盤,就要進行處理
( (Button)evt.getSource( ) ).setLabel( HUMANOPPYouKnowMe );
// 外觀改變
( (Button)evt.getSource( ) ).setEnabled( false ); // 已下棋步,不能再按
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 3; j++ )
if( evt.getSource( ) == BookKeepArray[ i ][ j ] )
// 在陣列資料結構上記載棋步
t.recordMoveInArray( TicTacToe.HUMANOPP, i, j );
// 在棋手下完後,檢查勝負,棋盤滿而無勝負就是平手(Draw)
if( GameOverRest( t.boardIsFull( ), "DRAW", true ) )
return;
// 換電腦下
doComputerMove( true );
// 檢查勝負
GameOverRest( t.ResultIsWin( TicTacToe.COMPUTER ),
"嘿!我贏了", true );
// 檢查是否平手(Draw)
GameOverRest( t.boardIsFull( ), "DRAW", false );
return; // 等棋手下棋
}
}
// 主程式, 建立新棋盤,開始玩!
public static void main( String [ ] args )
{
Frame f = new TestTicTacToe( );
f.pack( );
f.show( );
// 啟始之後,等棋手下棋
}
private Button [ ][ ] BookKeepArray = new Button[ 3 ][ 3 ];//開始時棋盤是空的
private TicTacToe t;
private String computerYouKnowMe = "O";
private String HUMANOPPYouKnowMe = "X";
}
======================================================================================
http://www.flag.com.tw/news/FT732.zip
javac -deprecation example.java
java 網路程式設計 旗標 顏春煌
Java 網路程式設計範例
try catch是專門在處理錯誤事件
java while (true)
MyClient.java
============================================================
import java.net.*;
import java.io.*;
public class MyClient {
public static final int SERVICE_PORT = 20;
public static void main(String args[]) {
// Check for hostname parameter
if (args.length != 1) {
System.out.println ("Syntax - DaytimeClient host");
return;
}
// Get the hostname of server
String hostname = args[0];
try {
// Get a socket to the daytime service
Socket daytime = new Socket (hostname, SERVICE_PORT);
System.out.println ("Connection established");
// Set the socket option just in case server stalls
daytime.setSoTimeout ( 2000 );
// Read from the server
BufferedReader reader = new BufferedReader (
new InputStreamReader(daytime.getInputStream()));
System.out.println ("Results : " + reader.readLine());
// Close the connection
daytime.close();
} catch (IOException ioe) {
System.err.println ("Error " + ioe);
}
}
}
============================================================
server
============================================================
import java.net.*;
import java.io.*;
public class MyServer {
public final static int myPort=20; // 使用port 20
public static void main(String args[]) {
//ServerSocket ss;
//Socket sc;
//PrintWriter op;
try {
ServerSocket ss = new ServerSocket(myPort);
while (true) {
Socket sc = ss.accept(); // 等待建立連線
PrintWriter op = new PrintWriter(sc.getOutputStream());
op.println("Hi! There!");
op.flush(); // 很重要, 不要漏掉
//sc.close();
//break;
}
}
catch (IOException e) {
System.err.println(e);
}
System.out.println("end");
/*
try {
ss = new ServerSocket(myPort);
try {
while (true) {
sc = ss.accept(); // 等待建立連線
op = new PrintWriter(sc.getOutputStream());
op.println("Hi! There!");
op.flush(); // 很重要, 不要漏掉
sc.close();
}
}
catch (IOException e) {
ss.close();
System.err.println(e);
}
}
catch (IOException e) {
System.err.println(e);
}
*/
}
}
Java 網路程式設計範例
第十一週 (2012年5月10日)
try catch是專門在處理錯誤事件
java while (true)
MyClient.java
============================================================
import java.net.*;
import java.io.*;
public class MyClient {
public static final int SERVICE_PORT = 20;
public static void main(String args[]) {
// Check for hostname parameter
if (args.length != 1) {
System.out.println ("Syntax - DaytimeClient host");
return;
}
// Get the hostname of server
String hostname = args[0];
try {
// Get a socket to the daytime service
Socket daytime = new Socket (hostname, SERVICE_PORT);
System.out.println ("Connection established");
// Set the socket option just in case server stalls
daytime.setSoTimeout ( 2000 );
// Read from the server
BufferedReader reader = new BufferedReader (
new InputStreamReader(daytime.getInputStream()));
System.out.println ("Results : " + reader.readLine());
// Close the connection
daytime.close();
} catch (IOException ioe) {
System.err.println ("Error " + ioe);
}
}
}
============================================================
server
============================================================
import java.net.*;
import java.io.*;
public class MyServer {
public final static int myPort=20; // 使用port 20
public static void main(String args[]) {
//ServerSocket ss;
//Socket sc;
//PrintWriter op;
try {
ServerSocket ss = new ServerSocket(myPort);
while (true) {
Socket sc = ss.accept(); // 等待建立連線
PrintWriter op = new PrintWriter(sc.getOutputStream());
op.println("Hi! There!");
op.flush(); // 很重要, 不要漏掉
//sc.close();
//break;
}
}
catch (IOException e) {
System.err.println(e);
}
System.out.println("end");
/*
try {
ss = new ServerSocket(myPort);
try {
while (true) {
sc = ss.accept(); // 等待建立連線
op = new PrintWriter(sc.getOutputStream());
op.println("Hi! There!");
op.flush(); // 很重要, 不要漏掉
sc.close();
}
}
catch (IOException e) {
ss.close();
System.err.println(e);
}
}
catch (IOException e) {
System.err.println(e);
}
*/
}
}
======================================================================================
第十週 (2012年4月26日)
Java 網路程式設計範例
期中考
4/19
兩人一組
兩人一組
1. 請點名單簽名
2. 期中考請放上LMS 的作業(期中考),檔名兩人學號
成績以上傳LMS(程式壓縮:包括2 個 Client,1個 Server,各有不同的IP)時間先後順序分為
100 2 組
95 2 組
90 2 組
85 2 組
80 2 組
75 2 組
70 2 組
65 2 組
60 2 組
沒有完成以50分計算
期中考
九宮格圈叉遊戲(利用 9 個按紐),利用網路連線(2 個 Client 及1個 Server),即可與對方進行圈叉遊戲,把資料用字串陣列方式送出
。
參考
圈叉
按紐
===================================
第七週 (2012年4月12日)
第五週 (2012年3月29日)
網路核心程式設計
kernelnetworkprograming
4 以加入Accept 說明VB Winsock 的每一個狀態下所代別的意義與VB ...
作業
Server initial, bind, listen
Client initial, bind,
參考
winsocket 的連線狀態
http://lms.ctl.cyut.edu.tw/9730611/doc/43964
http://blog.yam.com/ibma92157/article/27580746
助教鄧文柯
修課同學
陳政貿許峻榮
徐嘉佑
楊政峯
周誠哲
張立傑
吳懿倫
朱修宏
郭尚銘
楊華睿
何永裕
陳昭宇
邱柏瑞
羅巧欣
卓家詮
林郁維
何宗益
戴永發
紀韋丞
王文勇
吳秉松
Solidworks的VB二次開發
國立中央大學與資策會與合辦Java網際網路程式設計師就業養成班課程
第四週 (2012年3月22日)
作業
java-> vb
開啟Sever被192.168.10.163連線: 三向交握結果: 當clinet連線192.168.10.163: 三向交握結果: Tcp state diagram:
第四週 (2012年3月15日)
張運晟
Private Sub Form_Load()
Winsock1.LocalPort = 7777
'設定自己Winsock的通訊埠
Winsock1.RemotePort = 7777 '設定與Server端做Listen的Port相同
'設定對方Winsock的通訊埠
Winsock1.RemoteHost = "192.168.15.56"
'設定對方的IP 上面的IP
Winsock1.Bind
End Sub
===========================================
寫簡單的 socket 程式
但是要使用 netstat 這個指令來看看
windows 裡面 socket 程式的動作
參考
莊三輝
鍾國珍
Private Sub Form_Load()
Winsock1.LocalPort = 7777
'設定自己Winsock的通訊埠
Winsock1.RemotePort = 7777 '設定與Server端做Listen的Port相同
'設定對方Winsock的通訊埠
Winsock1.RemoteHost = "192.168.15.56"
'設定對方的IP 上面的IP
Winsock1.Bind
End Sub
===========================================
寫簡單的 socket 程式
但是要使用 netstat 這個指令來看看
windows 裡面 socket 程式的動作
參考
莊三輝
鍾國珍
vb 訊息
Java Developer Tutorials and Training
xp 路徑設定
第二週 (2012年3月1日)
Vb button
It is easy to put a button on a VB form.
java button
How to Use Buttons, Check Boxes, and Radio Buttons
java class button
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
---------------------------------------------------------------------
import javax.swing.*;
import java.awt.event.*; // 要處理事件必須 import 此套件
public class Exe extends JFrame implements ActionListener {
/**
* @param args
*/
//int act = 0; // 用來記錄按鈕被次數的變數
int SizeW = 510;
int SizeH = 1570;
public static void main(String[] args) {
Exe test = new Exe();
}
// 用建構方法來建立元件、將元件加入視窗、顯示視窗
public Exe() {
setTitle("Hsu Chih-Yu"); // 設定視窗標題
JButton mybutton = new JButton("宇");
JButton mybutton1 = new JButton("ww");
mybutton.setSize(150, 150);
mybutton1.setSize(250, 150);
// 通知按鈕物件:本物件要當傾聽者
mybutton.addActionListener(this);
getContentPane().add(mybutton);
getContentPane().add(mybutton1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(SizeW,SizeH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
SizeW = SizeW * 2;
SizeH = SizeH * 2;
setSize(SizeW,SizeH);
}
}
隨堂練習
放上一個 Check Boxes
第二週 (2012年3月1日)
Vb button
It is easy to put a button on a VB form.
java button
How to Use Buttons, Check Boxes, and Radio Buttons
java class button
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
---------------------------------------------------------------------
import javax.swing.*;
import java.awt.event.*; // 要處理事件必須 import 此套件
public class Exe extends JFrame implements ActionListener {
/**
* @param args
*/
//int act = 0; // 用來記錄按鈕被次數的變數
int SizeW = 510;
int SizeH = 1570;
public static void main(String[] args) {
Exe test = new Exe();
}
// 用建構方法來建立元件、將元件加入視窗、顯示視窗
public Exe() {
setTitle("Hsu Chih-Yu"); // 設定視窗標題
JButton mybutton = new JButton("宇");
JButton mybutton1 = new JButton("ww");
mybutton.setSize(150, 150);
mybutton1.setSize(250, 150);
// 通知按鈕物件:本物件要當傾聽者
mybutton.addActionListener(this);
getContentPane().add(mybutton);
getContentPane().add(mybutton1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(SizeW,SizeH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
SizeW = SizeW * 2;
SizeH = SizeH * 2;
setSize(SizeW,SizeH);
}
}
隨堂練習
放上一個 Check Boxes