2012年3月29日 星期四

3/29 java




























































DaytimeServer.java



import java.net.*;
import java.io.*;


// Chapter 6, Listing 2
public class DaytimeServer {
  public static final int SERVICE_PORT = 2013;


  public static void main(String args[]) {
    try {
      // Bind to the service port, to grant clients access to
      // the TCP daytime service
      ServerSocket server = new ServerSocket (SERVICE_PORT);


      System.out.println ("Daytime service started");


      // Loop indefinitely, accepting clients
      for (;;) {
        // Get the next TCP client
        Socket nextClient = server.accept();


        // Display connection details
        System.out.println ("Received request from " +
        nextClient.getInetAddress() + ":" + nextClient.getPort() );


        // Don't read, just write the message
        OutputStream out = nextClient.getOutputStream();
        PrintStream pout = new PrintStream (out);


        // Write the current date out to the user
        pout.print( new java.util.Date() );


        // Flush unsent bytes
        out.flush();


        // Close the connection
        nextClient.close();
      }
    } catch (BindException be) {
      System.err.println ("Service already running on port " + SERVICE_PORT );
    } catch (IOException ioe) {
      System.err.println ("I/O error - " + ioe);
    }
  }
}



DaytimeClient.java



import java.net.*;
import java.io.*;


// Chapter 6, Listing 1
public class DaytimeClient {
  public static final int SERVICE_PORT = 2013;


  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程式碼



Option Explicit


Private Sub cmdSend_Click()
  Winsock1.SendData txtSend.Text
End Sub


Private Sub Command1_Click()
Label2.Caption = Winsock1.State
Winsock1.Bind 7777, Winsock1.LocalIP
End Sub


Private Sub Command2_Click()
Label3.Caption = Winsock1.State
End Sub


Private Sub Command3_Click()
Winsock1.Listen  '監聽
Label4.Caption = Winsock1.State


End Sub


Private Sub Command4_Click()
Label5.Caption = Winsock1.State
End Sub


Private Sub Form_Load()
  'Winsock1.LocalPort = 7777
  'Winsock1.Listen  監聽
End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData, vbString
  txtReceived.Text = strData
  If strData = "close" Then
    Winsock1.Close
    cmdSend.Enabled = False
    Winsock1.Listen
  End If
End Sub


Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  Winsock1.Close
  Winsock1.Accept requestID  '改成接受
  cmdSend.Enabled = True
End Sub


clint端



Private Sub bind_Click()


Label1.Caption = Winsock1.State
End Sub


Private Sub bind2_Click()
Label2.Caption = Winsock1.State
End Sub


Private Sub cmdConnect_Click()
  Winsock1.LocalPort = 0   '以便自動產生Local Port
  Winsock1.Connect "192.168.15.51"  '設定改成您 Server 電腦的IP 號碼
  Label3.Caption = Winsock1.State
End Sub


Private Sub cmdExit_Click()
  Winsock1.SendData "close"
  DoEvents
  Winsock1.Close
  Winsock1.LocalPort = 0
End Sub


Private Sub cmdSend_Click()
  Winsock1.SendData txtOutput.Text
  DoEvents
End Sub


Private Sub Form_Load()
  Winsock1.RemotePort = 7777  '設定與Server端做Listen的Port相同
End Sub


Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State <> sckClosed Then
   cmdExit_Click
End If
End Sub


Private Sub Winsock1_Connect()
If Winsock1.State = sckConnected Then
   lstInput.AddItem "Connected! LocalPort =" & Winsock1.LocalPort _
                    & " RemptePort = " & Winsock1.RemotePort
End If


End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim mydata As String
  Winsock1.GetData mydata, vbString
  lstInput.AddItem mydata
End Sub


Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  lstInput.AddItem Description
End Sub





2012年3月15日 星期四

TCP與UDP差異

程式設計工藝大師

TCP與UDP差異

傳輸控制協議(Transmission Control Protocol, TCP)是一種連接導向的、可靠的、基於位元組流的運輸層(Transport layer)通信協議


用戶數據報協議(User Datagram Protocol, UDP)是一個簡單的面向數據報的傳輸層協議 ,UDP只提供數據的不可靠傳遞,它一旦把應用程序發給網路層的數據發送出去,就不保留數據備份(所以UDP有時候也被認為是不可靠的數據報協議)












圖片來源 http://blog.yam.com/ibma92157/article/27530306

當SERVER




Option Explicit

Private Sub cmdSend_Click()
  Winsock1.SendData txtSend.Text
End Sub

Private Sub Form_Load()
  Winsock1.LocalPort = 7777
  Winsock1.Listen  '監聽
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData, vbString
  txtReceived.Text = strData
  If strData = "close" Then
    Winsock1.Close
    cmdSend.Enabled = False
    Winsock1.Listen
  End If
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  MsgBox "有人敲門"
  Winsock1.Close
  Winsock1.Accept requestID  '改成接受
  cmdSend.Enabled = True
End Sub


當CLEINT


Option Explicit

Private Sub cmdSend_Click()
  Winsock1.SendData txtSend.Text
End Sub

Private Sub Form_Load()
  Winsock1.LocalPort = 7777
  Winsock1.Listen  '監聽
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData, vbString
  txtReceived.Text = strData
  If strData = "close" Then
    Winsock1.Close
    cmdSend.Enabled = False
    Winsock1.Listen
  End If
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  MsgBox "有人敲門"
  Winsock1.Close
  Winsock1.Accept requestID  '改成接受
  cmdSend.Enabled = True
End Sub







2012年3月1日 星期四

重點整理

物件  object
類別  class
屬性  property
名詞  caption
事件  event
行為  behavior
方法  method



















import javax.swing.*;
import java.awt.event.*;  // 要處理事件必須 import 此套件

public class Exe extends JFrame implements ActionListener {

/**
* @param args
*/
//int act = 0;     // 用來記錄按鈕被次數的變數
int SizeW = 800;
int SizeH = 800;

 public static void main(String[] args) {
 Exe test = new Exe();
 }

 // 用建構方法來建立元件、將元件加入視窗、顯示視窗
 public Exe() {
   setTitle("9730053");    // 設定視窗標題
   JButton mybutton = new JButton("變大");
   JButton mybutton1 = new JButton("22");
   JCheckBox mycheckbox = new JCheckBox("多選");
   JRadioButton myradiobutton = new JRadioButton("單選");
 
   mybutton.setSize(100,100);
   mybutton1.setSize(200,200);
   mycheckbox.setSize(500,500);
   myradiobutton.setSize(800,800);
 
   // 通知按鈕物件:本物件要當傾聽者
   mybutton.addActionListener(this);
 
   getContentPane().add(mybutton);
   getContentPane().add(mybutton1);
   getContentPane().add(mycheckbox);
   getContentPane().add(myradiobutton);
 
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setSize(SizeW,SizeH);
   setVisible(true);
 }

 public void actionPerformed(ActionEvent e) {
 SizeW = SizeW * 2;
 SizeH = SizeH * 2;
 setSize(SizeW,SizeH);
 }

}