2012年4月26日 星期四
2012年4月11日 星期三
2012/04/12
Dim s(2), a(2) As Integer
Dim st(2), totalstr As String
Private Sub Command1_Click(Index As Integer)
If Index = 0 Then
s(Index) = s(Index) + 1
a(Index) = s(Index) Mod 2
Select Case a(Index)
Case 0
Shape1(2).FillColor = QBColor(10)
Label2.Caption = a(Index)
st(Index) = 0
Case 1
Shape1(2).FillColor = QBColor(12)
Label2.Caption = a(Index)
st(Index) = 1
End Select
ElseIf Index = 1 Then
s(Index) = s(Index) + 1
a(Index) = s(Index) Mod 2
Select Case a(Index)
Case 0
Shape1(1).FillColor = QBColor(10)
Label2.Caption = a(Index)
st(Index) = 0
Case 1
Shape1(1).FillColor = QBColor(12)
Label2.Caption = a(Index)
st(Index) = 1
End Select
ElseIf Index = 2 Then
s(Index) = s(Index) + 1
a(Index) = s(Index) Mod 2
Select Case a(Index)
Case 0
Shape1(0).FillColor = QBColor(10)
Label2.Caption = a(Index)
st(Index) = 0
Case 1
Shape1(0).FillColor = QBColor(12)
Label2.Caption = a(Index)
st(Index) = 1
End Select
End If
totalstr = st(0) + st(1) + st(2)
Label1.Caption = totalstr
End Sub
2012年4月4日 星期三
2012/04/05
VB TCP 多人連線
Server端
IP : 192.168.15.46
Client端
IP : 192.168.15.47、192.168.15.48
Server端
截圖
winsock設定
程式碼
Option Explicit
Private Sub cmdSend_Click()
Dim i As Long
For i = 1 To 2
Winsock1(i).SendData txtSend.Text
Next
End Sub
Private Sub Form_Load()
Winsock1(0).LocalPort = 7777
Winsock1(0).Listen 'Winsock1(0)負責監聽 Clien1=Winsock1(1) Clien2=Winsock1(2)
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim i As Long
For i = 1 To 4
If Winsock1(i).State = sckClosed Then
Winsock1(i).Accept requestID
List1.AddItem "Local Port=" + Str(Winsock1(i).LocalPort) + " RemotePort = " + Str(Winsock1(i).RemotePort)
Exit For
End If
Next
cmdSend.Enabled = True
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As String
Winsock1(Index).GetData strData, vbString
txtReceived.Text = strData
If Index = 1 Then
Winsock1(2).SendData txtReceived.Text '接收到來至Clien1的資料送到Clien2
Else
Winsock1(1).SendData txtReceived.Text '接收到來至Clien2的資料送到Clien1
End If
If strData = "close" Then
Winsock1(1).Close
cmdSend.Enabled = False
Winsock1(0).Listen
End If
End Sub
Client端
截圖
IP與port設定
程式碼
Private Sub cmdConnect_Click()
Winsock1.LocalPort = 0 '以便自動產生Local Port
Winsock1.Connect "192.168.15.46" '設定改成您 Server 電腦的IP 號碼
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
Server端
IP : 192.168.15.46
Client端
IP : 192.168.15.47、192.168.15.48
Server端
截圖
winsock設定
程式碼
Option Explicit
Private Sub cmdSend_Click()
Dim i As Long
For i = 1 To 2
Winsock1(i).SendData txtSend.Text
Next
End Sub
Private Sub Form_Load()
Winsock1(0).LocalPort = 7777
Winsock1(0).Listen 'Winsock1(0)負責監聽 Clien1=Winsock1(1) Clien2=Winsock1(2)
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim i As Long
For i = 1 To 4
If Winsock1(i).State = sckClosed Then
Winsock1(i).Accept requestID
List1.AddItem "Local Port=" + Str(Winsock1(i).LocalPort) + " RemotePort = " + Str(Winsock1(i).RemotePort)
Exit For
End If
Next
cmdSend.Enabled = True
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As String
Winsock1(Index).GetData strData, vbString
txtReceived.Text = strData
If Index = 1 Then
Winsock1(2).SendData txtReceived.Text '接收到來至Clien1的資料送到Clien2
Else
Winsock1(1).SendData txtReceived.Text '接收到來至Clien2的資料送到Clien1
End If
If strData = "close" Then
Winsock1(1).Close
cmdSend.Enabled = False
Winsock1(0).Listen
End If
End Sub
Client端
截圖
IP與port設定
程式碼
Private Sub cmdConnect_Click()
Winsock1.LocalPort = 0 '以便自動產生Local Port
Winsock1.Connect "192.168.15.46" '設定改成您 Server 電腦的IP 號碼
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月28日 星期三
2012/03/29
JAVA TCP連線
Server端
畫面截圖
Client端
畫面截圖
連線 → cmdconnect
傳送 → cmdSend
停止 → cmdExit
Server端
畫面截圖
Client端
畫面截圖
VB JAVA連線時 VB畫面
VB TCP連線
sckClosed = 0 --缺省值,關閉。
SckOpen = 1 --打開。
SckListening = 2 --偵聽
sckConnectionPending = 3 --連結掛起
sckResolvingHost = 4 --識別主機。
sckHostResolved = 5 --已識別主機
sckConnecting = 6 --正在連結。
sckConnected = 7 --已連結。
sckClosing = 8 --同級人員正在關閉連結。
sckError = 9 --錯誤
SckOpen = 1 --打開。
SckListening = 2 --偵聽
sckConnectionPending = 3 --連結掛起
sckResolvingHost = 4 --識別主機。
sckHostResolved = 5 --已識別主機
sckConnecting = 6 --正在連結。
sckConnected = 7 --已連結。
sckClosing = 8 --同級人員正在關閉連結。
sckError = 9 --錯誤
TCP-Server
程式碼
Option Explicit
Private Sub cmdSend_Click()
Winsock1.SendData txtSend.Text
Label2.Caption = Winsock1.State
End Sub
Private Sub Command1_Click()
Winsock1.Bind 7777, Winsock1.LocalIP
Label2.Caption = Winsock1.State
End Sub
Private Sub Command2_Click()
Label2.Caption = Winsock1.State
End Sub
Private Sub Command3_Click()
Winsock1.Listen
Label2.Caption = Winsock1.State
End Sub
Private Sub Command4_Click()
Winsock1.Close
Label2.Caption = Winsock1.State
End Sub
Private Sub Command5_Click()
Label2.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
截圖
Bind → Command1
initial → Command2
Listen → Command3
Close → Command4
State → Command5
Close → Command4
State → Command5
TCP-Client
程式碼
Private Sub cmdConnect_Click()
Winsock1.LocalPort = 0 '以便自動產生Local Port
Winsock1.Connect "192.168.15.47" '設定改成您 Server 電腦的IP 號碼
Label1.Caption = Winsock1.State
End Sub
Private Sub cmdExit_Click()
Winsock1.SendData "close"
DoEvents
Winsock1.Close
Winsock1.LocalPort = 0
Label1.Caption = Winsock1.State
End Sub
Private Sub cmdSend_Click()
Winsock1.SendData txtOutput.Text
DoEvents
Label1.Caption = Winsock1.State
End Sub
Private Sub Command1_Click()
Winsock1.LocalPort = 0
Label1.Caption = Winsock1.State
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 Label2_Click()
nd Sub
Private Sub Label3_Click()
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
截圖
連線 → cmdconnect
傳送 → cmdSend
停止 → cmdExit
2012年3月21日 星期三
2012年3月15日 星期四
2012/03/15
TCP
優點 : 傳送可靠,程式可省略可靠機制。
缺點 : 但是速度比較慢。
UDP
優點 : 傳輸量大﹐迅速。
缺點 : 不可靠,程式或需自行提供可靠機制。
socket連線基本流程
TCP連線-VB
TCP server監聽
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
CLIENT端
Private Sub cmdConnect_Click()
Winsock1.LocalPort = 0 '以便自動產生Local Port
Winsock1.Connect "192.168.15.46" '設定改成您 Server 電腦的IP 號碼
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月2日 星期五
2012年2月29日 星期三
2012/03/01(四)
物件Object
類別Class
屬性property
事件event
行為behavior
方法method
VB
.hide隱藏
.show 顯示另一個表單
.printfrom1 印表機
Name不要改
Caption 改顯示名稱
---JAVA----
set title 改外框名稱
JButton mybutton = new JButton("SS"); 創造一個按鈕
mybutton.setSize(100, 100); 設定按鈕大小
上帝 依照 他的 形象 造人
---程式碼---
import javax.swing.*;
import java.awt.event.*; // 要處理事件必須 import 此套件
public class Exe extends JFrame
implements ActionListener {
/**
* @param args
*/
//int act = 0; // 用來記錄按鈕被次數的變數
int SizeW = 500;
int SizeH = 500;
public static void main(String[] args) {
Exe test = new Exe();
}
// 用建構方法來建立元件、將元件加入視窗、顯示視窗
public Exe() {
setTitle("JAVA程式-按鈕"); // 設定視窗標題
JButton mybutton = new JButton("SS");
mybutton.setSize(100, 100);
JButton mybutton1 = new JButton("SS2");
mybutton1.setSize(150, 150);
JCheckBox mycheakbox = new JCheckBox("ss3");
mycheakbox.setSize(200, 200);
JRadioButton myJRadioButton = new JRadioButton("SS4");
myJRadioButton.setSize(150, 150);
// 通知按鈕物件:本物件要當傾聽者
mybutton.addActionListener(this);
//把按鈕加入到表單內
getContentPane().add(mybutton);
getContentPane().add(mybutton1);
getContentPane().add(mycheakbox);
getContentPane().add(myJRadioButton);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(SizeW,SizeH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//SizeW = SizeW * 2;
//SizeH = SizeH * 2;
//setSize(SizeW,SizeH);
}
}
類別Class
屬性property
事件event
行為behavior
方法method
VB
.hide隱藏
.show 顯示另一個表單
.printfrom1 印表機
Name不要改
Caption 改顯示名稱
---JAVA----
set title 改外框名稱
JButton mybutton = new JButton("SS"); 創造一個按鈕
mybutton.setSize(100, 100); 設定按鈕大小
上帝 依照 他的 形象 造人
---程式碼---
import javax.swing.*;
import java.awt.event.*; // 要處理事件必須 import 此套件
public class Exe extends JFrame
implements ActionListener {
/**
* @param args
*/
//int act = 0; // 用來記錄按鈕被次數的變數
int SizeW = 500;
int SizeH = 500;
public static void main(String[] args) {
Exe test = new Exe();
}
// 用建構方法來建立元件、將元件加入視窗、顯示視窗
public Exe() {
setTitle("JAVA程式-按鈕"); // 設定視窗標題
JButton mybutton = new JButton("SS");
mybutton.setSize(100, 100);
JButton mybutton1 = new JButton("SS2");
mybutton1.setSize(150, 150);
JCheckBox mycheakbox = new JCheckBox("ss3");
mycheakbox.setSize(200, 200);
JRadioButton myJRadioButton = new JRadioButton("SS4");
myJRadioButton.setSize(150, 150);
// 通知按鈕物件:本物件要當傾聽者
mybutton.addActionListener(this);
//把按鈕加入到表單內
getContentPane().add(mybutton);
getContentPane().add(mybutton1);
getContentPane().add(mycheakbox);
getContentPane().add(myJRadioButton);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(SizeW,SizeH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//SizeW = SizeW * 2;
//SizeH = SizeH * 2;
//setSize(SizeW,SizeH);
}
}
訂閱:
文章 (Atom)