2016年4月28日 星期四

電子磅秤連線 JXW-1530K

----
廠商 :JXW-1530雙量程計重桌秤
----
廠商還真不錯,打去說:客戶有使用JXW-1530電子磅秤;我想要這台的規格書! 就mail 給我了!
真是好棒棒的硬體廠商!

系統結合磅秤機的視窗圖


---
這可適用:水果店 . 熟食 .....等凡秤重的商品,均可使用!!


美甲,美髮,醫美 服務人員業績統計表 各分日/月/年度

官網:進銷存,租書,批發,軟體設計::池龍工作室::POS系統
---
針對美甲及醫美和美髮 複雜的業績計算
採用動態列式計算並自動依抽成換算和依產品.服務項等自動分配計算

複雜度在:
1. 產品.服務項等均是動態
2. 服務人員 - 單項可能2人以上,須拆分




2016年4月24日 星期日

ASP.NET MVC 相關文章

Web Forms/Visual Basic/JavaScript/SQL/MVC
---
有空再來看看

SQL計算月底日期

--使用dateadd設定為現在日期的1個月後
select dateadd(month,1,getdate()) "1個月後是"
go

--以字串char(7)來取得年與月,2010/01
--並在尾端加上'/1',使字串成為2010/01/1
select convert(char(7),dateadd(month,1,getdate()),111)+'/1' "下個月1日的日期"
go

--將字串轉換為日期格式
select convert(datetime,(convert(char(7),dateadd(month,1,getdate()),111)+'/1')) "日期格式"

--下個月初減一日,就是這個月底
select DATEADD(day, -1, convert(datetime,(convert(char(7),dateadd(month,1,getdate()),111)+'/1'))) "月底日期" 

2016年4月23日 星期六

Crystal reports 依群組來編細目裡項目的編號

引用來源:[Crystal reports]依群組來編細目裡項目的編號..?
--
請在 報表新增一個 累加值欄位
會出現 一個 視窗 [建立累加值欄位]
裡面請選取設定
摘要欄位 : 請從左方選一個顯示在細項裡的欄位
摘要類型 : 計數
驗算 : 每個資料錄
重設 : 群組變更時
將此 累加值欄位放到細項區塊

2016年4月20日 星期三

美甲,醫美 管理系統

--------------
美甲及醫美的管理系統,均須採客製開發!  每家的需求項均有所不同

針對共同性質的 : 服務員 及  帳務  . 業績  等等特別處理
--------------
各大  醫美 . 美甲  等, 若有需求  歡迎至 進銷存,租書,批發,軟體設計::池龍工作室::POS系統  有相關聯絡方式!!

底下目前僅少部分系統截圖:




2016年4月18日 星期一

javascript show hidden div when select option is selected

參考來源:javascript show hidden div when select option is selected
--

<select id="test" name="form_select" onchange="showDiv(this)">
   <option value="0">No</option>
   <option value ="1">Yes</option>
</select>

<div id="hidden_div" style="display: none;">Hello hidden content</div>

function showDiv(elem){
   if(elem.value == 1)
      document.getElementById('hidden_div').style.display = "block";
}

2016年4月13日 星期三

android studio 版本AVD問題及設定值

1.測試 android-studio 版本 143.2739321 確定AVD 有問題
(android-studio-bundle-143.2739321-windows)

2.使用 android-studio-bundle-141.2456560-windows 版本,請勿更新版本 , 不然AVD 還是會掛掉!!

-------
以上1-2版本 , 折磨了反覆移除又安裝後的結果 ! 莫名其妙的AVD 不是沒有安裝 (android studio no emulator install) 不然就是 AVD 跑太久卡死在程序內...come line ...
-------

還好有抵留舊版本!!

----
另3個設定值:
1.顯示行號  2.不區分大小寫 3.自動引入元件 (如圖設定)




2016年4月11日 星期一

POS 零售業系統


----
應客戶需求,全部採自由設定控制  按鈕大小.顏色.字體.包含控制開關
----
目前能套入各行業別,列表如下:

01.傳統鞋襪批發
02.音響設備長期性租借契約拆帳
03.便利商店
04.蔬果冷凍批發
05.藥局
06.食品副供站
07.音響設備依場次出庫入庫控管
08.書局
09.飲料店
10.五金店
11.水電行材料批發
12.美甲
13.醫美

---
專業的客製行為開發,細心傾聽客戶的每項需求並打造出完全符合作業
一分錢一分貨,歡迎試用

2016年4月8日 星期五

POS系統

官網
---
金湧POS.ERP系統(網路版) 來電詢價

■金湧 ERP 系統,目前支援各行業別(多店/網路版)■
01.傳統鞋襪批發
02.音響設備長期性租借契約拆帳
03.便利商店
04.蔬果冷凍批發
05.藥局
06.食品副供站
07.音響設備依場次出庫入庫控管
08.書局
09.飲料店
10.五金店
11.水電行材料批發

★多元化特色★
1.支援條碼機列印
2.支援商品條碼本製作
3.支援匯出匯入 MS-Office Excel 2003.2007.2010 均可以,OpenOffice Calc
4.人性化操作,簡化重覆性操作
5.多門市系統
6.支援任何品牌盤點機資料匯入

★註冊享有權益★
1.享1年的保固期,期間內可調/增功能.報表(非主體功能須斟酌另報價)
2.保固或維護合約期間內遠端維護.更新一律免費

2016年4月7日 星期四

android studio avd not working

android studio avd not working

真是困擾且問題一堆!!

解決方法:
(1)移除
(2)刪除 android  SDK  -->C:\Users\####\AppData\Local\Android\sdk
(3)刪除 android 設定檔-->C:\Users\####\.AndroidStudio[版本代號]
----------
再重新安裝後 , 即可以正常運作 AVD 無法啟動的問題

2016年4月6日 星期三

VB.NET中使用FTP下載檔案的兩種方法

.net沒有專門處理FTP的類,我們可以通過調用系統自帶的FTP.EXE 或者是調用win32 API中的wininet.dll來完成基本操作。希望以下的代碼能為大家抛磚引玉。

方法一: 使用Ftp.exe ,通過process類來調用它。

Imports System.Diagnostics

...

Public Sub GetFileByCallFtp()

'定義ProcessStartInfo,Process的啟動資訊。

Dim psi As New ProcessStartInfo

'ftp.exe的路徑最好放到設定檔裡。

psi.FileName = "C:\WINNT\system32\ftp.exe"

psi.RedirectStandardInput = False

psi.RedirectStandardOutput = True

'該值指示不使用作業系統Shell程式啟動進程。

psi.UseShellExecute = False

'命令集檔案名.注意,路徑中不能有空格.

Dim fileName As String = "C\ftp.txt"

'-s:FileName表示,從檔中讀取控制命令

psi.Arguments = "-s:" + fileName

Dim proc As Process

proc = Process.Start(psi)

'等待進程完成任務

proc.WaitForExit()

'在主控台輸出結果

Console.WriteLine(proc.StandardOutput)

Console.ReadLine()

End Sub

==============================================================================

ftp.txt 裡的內容

方法二,使用win32 api —— wininet.dll

首先是,api聲明:

因為此測試程式,是VB.NET ConsoleApplication所以,api聲明寫在Module裡,

方法是靜態的。所以沒加Shared關鍵字, 這一點請大家注意。

_
Public Function InternetOpen(ByVal sAgent As String, ByVal LAccessType As Integer, ByVal sProxyName As String, _
ByVal SProxyBypass As String, ByVal lFlags As Integer) As Integer
End Function

_
Public Function InternetConnect(ByVal hInternetSession As Integer, ByVal sServerName As String, _
ByVal nServerPort As Integer, ByVal sUsername As String, _
ByVal sPassword As String, ByVal lService As Integer, _
ByVal lFlags As Integer, ByVal lCoNtext As Integer) As Integer
End Function

_
Public Function FtpGetFile(ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, _
ByVal dwCoNtext As Integer) As Boolean
End Function

_
Public Function InternetCloseHandle(ByVal hInet As Integer) As Integer
End Function

調用:

Public Sub GetFileByCallWininetDLL()
Try
Dim intinet As Integer = InternetOpen(Nothing, 0, Nothing, Nothing, 0)
If intinet > 0 Then

'參數:intinet的session值,ftp位址,埠,使用者名,密碼,lService, lFlags,lCoNtext

Dim intinetconn As Integer = InternetConnect(intinet, "192.168.110.152", 0, "tokiwa", "tokiwa", 1, 0, 0)

If intinetconn > 0 Then

'下載某個檔到指定檔

Dim ret As Boolean = FtpGetFile(intinetconn, "pagerror.gif", "C:\itest.gif", 0, 0, 1, 0)

If ret Then
Console.WriteLine("ok!")
Console.ReadLine()
End If
InternetCloseHandle(intinetconn)
InternetCloseHandle(intinet)
Else
Console.WriteLine("can't connect!")
Console.ReadLine()
End If

Else
Console.WriteLine("ftp wrong!")
Console.ReadLine()
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.ReadLine()
End Try

End Sub

VB.net實現的一個簡單的P2P示例

這是用VB.net實現的一個簡單的P2P示例.利用了UDP打洞技術.分伺服器端跟用戶端.伺服器端負責登陸記錄使用者的IP和埠及轉發打洞消息.(相關技術在CSDN搜一下.有很多的.).原理到處都有,這裡就沒有貼出來.這裡貼出了VB.net的代碼.供初學者交流.也歡迎高手點評...

  伺服器端在啟動成功後.輸入help可以查看到伺服器相關命令.

  用戶端在登陸成功後.輸入help可以查看用戶端相關命令.(登陸時使用者名隨便.)

  以下是伺服器端:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Collections

Module myUDPServer

#Region "全域變數"

Dim ServerSocket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim ipep As IPEndPoint = New IPEndPoint(IPAddress.Any, 11000)

Dim htUserList As New Hashtable '用來保存線上使用者和使用者的"IP和埠"

Dim userName(0) As String
Dim userIPEP(0) As IPEndPoint
Dim userTime(0) As Integer

Dim timerDelegate As New TimerCallback(AddressOf onLineTimeOut)

#End Region

#Region "參數"

'以下是用戶端到伺服器端的消息開頭
Const LOGININ As String = "10" '請求登陸的消息|||消息形式:10+自己的使用者名
Const LOGINOUT As String = "11" '請求登出的消息|||消息形式:11+自己的使用者名
Const GETULIST As String = "12" '請求獲得線上使用者清單|||消息形式:12
Const P2PCONN As String = "13" '請求P2P連接的消息|||消息形式:13+自己的使用者名+|+對方的使用者名
Const HOLDLINE As String = "14" '保持連接.|||消息開式:14+自己的使用者名

'以下是伺服器到用戶端的消息開頭
Const HVUSER As String = "20" '使用者名已存在
Const GETUSER As String = "21" '線上使用者清單|||消息格式:21+使用者名+EP
Const MAKHOLD As String = "22" '打洞命令|||消息格式:22+IP
Const LOGINOK As String = "23" '登陸成功
Const SERVCLS As String = "24" '伺服器關閉
Const MSGEND As String = "25" '消息結束

'以下是伺服器端的命名
Const EXITPRO As String = "EXIT" '退出命令
Const SHOWULIST As String = "SHOWUSER" '顯示線上使用者
Const HELP As String = "HELP" '顯示説明

#End Region

#Region "方法"

'主函數,程式入口
Sub Main()

'獲得伺服器的IP位址
Dim addressList As System.Net.IPAddress() = Dns.GetHostByName(Dns.GetHostName()).AddressList
Dim ServerIP As IPAddress = addressList(0)

ServerSocket.Bind(ipep)
Console.WriteLine("伺服器正在啟動....")
Console.WriteLine("伺服器IP:" & ServerIP.ToString & " 正在監聽" & ipep.Port.ToString & "埠")
Dim listenTH As New Thread(AddressOf listen)
listenTH.Start() '啟用監聽的執行緒
Console.WriteLine("伺服器啟動成功.....")

Dim timer As New Timer(timerDelegate, Nothing, 0, 5000)

Dim SVInput As String
While True
Console.Write("Server>")
SVInput = Console.ReadLine().ToUpper
Select Case SVInput
Case EXITPRO
listenTH.Abort()
ServerSocket.Close()
Exit Sub
Case SHOWULIST
showUser()
Case HELP
Console.Write("*********************************" & Chr(10) & Chr(13) & "exit:輸出當前程式" & Chr(10) & Chr(13) & "showuser:顯示當前線上使用者例表" & Chr(10) & Chr(13) & "help:顯示説明" & Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13))
Case Else
Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "笨瓜,你輸入的不是有效的命令." & Chr(10) & Chr(13) & "*********************************")
End Select
End While


End Sub

'列印線上使用者
Sub showUser()
Dim hava As Boolean = False
If userName.Length <> 0 Then
Dim i As Integer
For i = 1 To userName.Length - 1
If userName(i) <> "" Then
hava = True
Exit For
End If
Next
If hava = False Then
Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "當前沒有使用者線上" & Chr(10) & Chr(13) & "*********************************")
Exit Sub
End If
Console.WriteLine("*********************************")
For i = 1 To userName.Length - 1
If userName(i) <> "" Then
Console.WriteLine("使用者名:" & userName(i) & " 位址:" & userIPEP(i).ToString)
End If
Next
Console.WriteLine("*********************************")
Else
Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "當前沒有使用者線上" & Chr(10) & Chr(13) & "*********************************")
End If
End Sub

'伺服器監聽函數
Sub listen()

While True

Try
Dim recv As Integer = 0
Dim data As [Byte]() = New Byte(1024) {}
Dim sender As New IPEndPoint(IPAddress.Any, 0)
Dim tempRemoteEP As EndPoint = CType(sender, EndPoint)
recv = ServerSocket.ReceiveFrom(data, tempRemoteEP)

'Console.WriteLine(Encoding.Unicode.GetString(data))

Dim msgHead As String = Encoding.Unicode.GetString(data, 0, 4)
Select Case msgHead
Case LOGININ
Dim LoginThing As String = userLogin(data, tempRemoteEP, recv)
If LoginThing = HVUSER Then
sendMsg(HVUSER, tempRemoteEP)
ElseIf LoginThing = LOGINOK Then
sendMsg(LOGINOK, tempRemoteEP)

End If

Case LOGINOUT
userloginout(data, recv)

Case GETULIST
Dim userinfo As String = getUserList()
sendMsg(userinfo, tempRemoteEP)

Case P2PCONN
questP2PConn(data, recv)

Case HOLDLINE
holdOnLine(data, recv)
End Select

Catch e As Exception
'Console.WriteLine(e.ToString)
End Try
End While

End Sub

'轉發P2P連接請求
Private Sub questP2PConn(ByVal data() As Byte, ByVal recv As Integer)

Dim recvStr As String = Encoding.Unicode.GetString(data, 4, recv - 4)
Dim split() As String = recvStr.Split("|")

Dim fromEP As IPEndPoint
Dim toEP As IPEndPoint
Dim i As Integer
For i = 1 To userName.Length - 1
If userName(i) = split(0) Then


fromEP = userIPEP(i)
End If
If userName(i) = split(1) Then
toEP = userIPEP(i)
End If
Next
Dim holdbytes() As Byte = Encoding.Unicode.GetBytes(MAKHOLD & fromEP.ToString)
ServerSocket.SendTo(holdbytes, toEP)
End Sub

'函數.返回所有線上使用者.其格式:使用者名+|+使用者IPEP+|
Private Function getUserList() As String
Dim userInfo As String = GETUSER
Dim i As Integer
For i = 1 To userName.Length - 1
If userName(i) <> "" Then
userInfo += userName(i) & "|" & userIPEP(i).ToString & "|"
End If
Next
Return userInfo
End Function

'使用者登陸,直接返回登陸是否成功的值
Private Function userLogin(ByVal data As Byte(), ByVal userEP As IPEndPoint, ByVal recvCount As Integer) As String

Dim Uname As String = Encoding.Unicode.GetString(data, 4, recvCount - 4)

Dim Uinfobytes() As Byte

Dim i As Integer
Dim j As Integer

For i = 1 To userName.Length - 1
If Uname = userName(i) Then
Return HVUSER
End If
Next

For i = 1 To userName.Length - 1
If userName(i) = "" Then
userName(i) = Uname
userIPEP(i) = userEP
userTime(i) = 60
Console.Write(Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13) & Uname.Trim & "上線了." & "使用者位址:" & userEP.ToString & Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13))
Console.Write("Server>")

Uinfobytes = Encoding.Unicode.GetBytes(LOGININ & userName(i) & "|" & userIPEP(i).ToString)

For j = 1 To userName.Length - 1
If userName(j) <> "" And userName(j) <> Uname Then
ServerSocket.SendTo(Uinfobytes, userIPEP(j))
End If
Next
Return LOGINOK
End If
Next

Dim userCount As Integer = userName.Length

ReDim Preserve userName(userCount)
ReDim Preserve userIPEP(userCount)
ReDim Preserve userTime(userCount)

userName(userName.Length - 1) = Uname
userIPEP(userIPEP.Length - 1) = userEP
userTime(userTime.Length - 1) = 60

Console.Write(Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13) & Uname.Trim & "上線了." & "使用者位址:" & userEP.ToString & Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13))
Console.Write("Server>")

Uinfobytes = Encoding.Unicode.GetBytes(LOGININ & userName(userName.Length - 1) & "|" & userIPEP(userName.Length - 1).ToString)

For j = 1 To userName.Length - 1
If userName(j) <> "" And userName(j) <> Uname Then
ServerSocket.SendTo(Uinfobytes, userIPEP(j))
End If
Next
Return LOGINOK

End Function

'使用者登出
Private Sub userloginout(ByVal data As Byte(), ByVal recvCount As Integer)

Dim i As Integer
Dim Uname As String = Encoding.Unicode.GetString(data, 4, recvCount - 4)

For i = 1 To userName.Length - 1

If Uname = userName(i) Then

Dim loginOutMsg As String = LOGINOUT & userName(i)


userName(i) = ""
userIPEP(i) = Nothing
userTime(i) = 0

Dim j As Integer
For j = 1 To userName.Length - 1
If userName(j) <> "" Then

sendMsg(loginOutMsg, userIPEP(j))

End If
Next

Console.WriteLine(Chr(10) & Chr(13) & "*********************************")
Console.WriteLine("使用者" & Uname & "下線了.")
Console.WriteLine("*********************************")
Console.Write("Server>")

Exit For

End If

Next

End Sub

'保持使用者線上的過程
Private Sub holdOnLine(ByVal data As Byte(), ByVal recvCount As Integer)

Dim Uname As String = Encoding.Unicode.GetString(data, 4, recvCount - 4)

Dim i As Integer

For i = 1 To userName.Length - 1

If Uname = userName(i) Then

userTime(i) = 60
Exit For

End If

Next

End Sub

'使用者超時退出
Private Sub onLineTimeOut(ByVal state As [Object])

Dim i As Integer

For i = 1 To userName.Length - 1

If userTime(i) > 0 Then

userTime(i) -= 5

If userTime(i) <= 0 Then

Dim loginoutmsg As String = LOGINOUT & userName(i)

Console.WriteLine(Chr(10) & Chr(13) & "*********************************")
Console.WriteLine("使用者" & userName(i) & "下線了.")
Console.WriteLine("*********************************")
Console.Write("Server>")

userName(i) = ""
userIPEP(i) = Nothing

Dim ULoginOutbytes() As Byte = Encoding.Unicode.GetBytes(loginoutmsg)

Dim j As Integer
For j = 1 To userName.Length - 1

If userName(j) <> "" Then
If userIPEP(j) Is Nothing Then
Else
ServerSocket.SendTo(ULoginOutbytes, userIPEP(j))
End If
End If

Next

End If

End If

Next

End Sub

'發送消息的函數
Sub sendMsg(ByVal msg As String, ByVal remoteEP As IPEndPoint)
Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(msg)
Try

ServerSocket.SendTo(sendBytes, remoteEP)

Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub

#End Region

End Module

以下是用戶端:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading


Module Module1

#Region "參數"

'以下是用戶端到伺服器端的消息開頭
Const LOGININ As String = "10" '請求登陸的消息|||消息形式:10+自己的使用者名
Const LOGINOUT As String = "11" '請求登出的消息|||消息形式:11+自己的使用者名
Const GETULIST As String = "12" '請求獲得線上使用者清單|||消息形式:12+自己的使用者名


Const P2PCONN As String = "13" '請求P2P連接的消息|||消息形式:13+自己的使用者名+對方的使用者名
Const HOLDLINE As String = "14" '保持連接.|||消息開式:14+自己的使用者名


'以下是伺服器到用戶端的消息開頭
Const HVUSER As String = "20" '使用者名已存在
Const GETUSER As String = "21" '線上使用者清單|||消息格式:21+使用者名+EP
Const MAKHOLD As String = "22" '打洞命令|||消息格式:22+IP
Const LOGINOK As String = "23" '登陸成功
Const SERVCLS As String = "24" '伺服器關閉
Const MSGEND As String = "25" '消息結束


'以下是用戶端到用戶端的消息開頭
Const HOLDOK As String = "30" '打洞成功
Const CHATMSG As String = "31" '聊天消息
Const CHTMSGEND As String = "32" '聊天消息發送成功


'以下是用戶端的命名
Const EXITPRO As String = "EXIT" '退出命令
Const SHOWULIST As String = "SHOWUSER" '顯示線上使用者
Const HELP As String = "HELP" '顯示説明
Const SEND As String = "SEND" '發送消息


#End Region


#Region "全域全量"


Delegate Sub myMethodDelegate(ByRef myInData As Byte()) '登陸時用的事件


'Dim MaxTry As Integer = 5
Dim msgSendEnd As Boolean = False '消息是否發送成功,若發送成功,則會返回結束消息
Dim ThListen As New Thread(AddressOf listen) '監聽的執行緒
Dim ClientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) '用戶端套節字的定義


Dim username As String '當前使用者名
Dim ServerEP As IPEndPoint '伺服器的IPEP
Dim holdBytes As [Byte]() = Encoding.Unicode.GetBytes(HOLDLINE & username) '和伺服器保持連接連接時用到的byte陣列


Dim OLUserName() As String
Dim OLUserEP() As IPEndPoint
Dim getUrecCount As Integer
Dim testHold As Boolean = False
Dim testChat As Boolean = False


Private receiveDone As ManualResetEvent '在登陸時用來阻塞執行緒,等待收到資料
Private sendDone As ManualResetEvent '用來陰塞發送消息的執行緒.等待收到回送的確認訊息
Private getUDone As ManualResetEvent '用來阻塞請求好友名單的執行緒,等待接收好友名單
Private holdDone As ManualResetEvent '用來阻塞打洞時的執行緒
Private chatDone As ManualResetEvent '用來阻塞發送聊天消息時的執行緒


Dim timerDelegate As New TimerCallback(AddressOf holdonline) '為保持線上狀態弄得


#End Region


#Region "方法"


'主函數,程式入口
Sub Main()
Dim InputIP As String
Dim InputOK As Boolean = False



'判斷輸入的IP,並且保存伺服器的IPEP
While InputOK <> True
Console.Write("請輸入伺服器IP:")
InputIP = Console.ReadLine()
Try
ServerEP = New IPEndPoint(IPAddress.Parse(InputIP), 11000)
InputOK = True
Catch
Console.WriteLine("你輸入的伺服器IP不正確,請重新輸入.")
InputOK = False
End Try
End While


Dim bool As Boolean = False


'判斷使用者是否登陸成功
While bool <> True


Dim LoginOK As Boolean = Login()
If LoginOK = True Then
bool = True
Else
Console.Write("是否重試:輸入Y重試,輸入任意值退出程式:")
Dim tempYN As String = Console.ReadLine.ToUpper
If tempYN = "Y" Then
bool = False
Else
Exit Sub
End If
End If


End While


Console.WriteLine("使用者名:" & username)
holdBytes = Encoding.Unicode.GetBytes(HOLDLINE & username)
'登陸成功後.用一個timer,每隔50秒向伺服器發送消息,保持線上狀態跟在主機註冊的埠
Dim timer As New Timer(timerDelegate, Nothing, 10000, 50000)


'請求線上名單
Console.WriteLine("正在獲取線上名單,請稍後....")
Dim getUbool As Boolean = False
While getUbool <> True
getUbool = getU()
If getUbool = False Then
Console.Write("是否重試:輸入Y重試,輸入任意值退出程式:")
Dim tempYN As String = Console.ReadLine.ToUpper
If tempYN = "Y" Then
bool = False
Else
Exit Sub
End If
End If
End While


ThListen.Start()


'用來處理用戶端的一些命令
Dim SVInput As String
While True
Console.Write("Client>")
SVInput = Console.ReadLine().ToUpper
Select Case SVInput
Case EXITPRO
exitApp()
ThListen.Abort()
ClientSocket.Close()
Exit Sub
Case SHOWULIST
Console.WriteLine("*********************************")
showUserList()
Console.WriteLine("*********************************")
Case HELP
Console.Write("*********************************" & Chr(10) & Chr(13) & "exit:輸出當前程式" & Chr(10) & Chr(13) & "showuser:顯示當前線上使用者例表" & Chr(10) & Chr(13) & "send:發送消息.格式:send 使用者名 消息" & Chr(10) & Chr(13) & "help:顯示説明" & Chr(10) & Chr(13) & "*********************************" & Chr(10) & Chr(13))
Case Else
If SVInput.Substring(0, 4) = "SEND" Then
Dim split() As String = SVInput.Split(" ")
If split.Length = 3 Then
sendChatMsg(split(1), split(2))
Else
Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "你輸入的命令格式不正確.send命令格式為:send 使用者名 你的消息" & Chr(10) & Chr(13) & "*********************************")
End If
Else
Console.WriteLine("*********************************" & Chr(10) & Chr(13) & "笨瓜,你輸入的不是有效的命令." & Chr(10) & Chr(13) & "*********************************")
End If
End Select
End While


End Sub


'登陸函數
Private Function Login() As Boolean


receiveDone = New ManualResetEvent(False)
Dim userBytes As [Byte]()


Dim userOK As Boolean = False


Console.Write("請輸入你的使用者名:")


'判斷使用者名是否符合格式
While (userOK <> True)
username = Console.ReadLine.ToUpper
userBytes = Encoding.Unicode.GetBytes(LOGININ & username)


If userBytes.Length > 24 Or userBytes.Length < 10 Then
Console.WriteLine("使用者名不得小於6個位元組,且不得大於20個位元組.")
Console.Write("請重新輸入你的使用者名:")
Else
userOK = True
End If
End While


'向伺服器發送客戶消息
ClientSocket.SendTo(userBytes, ServerEP)


Dim data As [Byte]() = New Byte(1024) {}


Dim comStr As String = Encoding.Unicode.GetString(data, 0, 4)

'異面的接收伺服器回送的消息
Dim DGrecv As New myMethodDelegate(AddressOf recvLogin)
DGrecv.BeginInvoke(data, Nothing, Nothing)

'等待伺服器回送消息的時長為10秒,否則為伺服器超時
receiveDone.WaitOne(30000, True)

Dim recvStr As String = Encoding.Unicode.GetString(data, 0, 4)

If recvStr = comStr Then
Console.WriteLine("伺服器超時.登陸失敗!!")
Return False
End If

If Encoding.Unicode.GetString(data, 0, 4) = LOGINOK Then
Console.WriteLine("登陸成功!!")
Return True
ElseIf Encoding.Unicode.GetString(data, 0, 4) = HVUSER Then
Console.WriteLine("使用者名重複.登陸失敗!!")
Return False
Else
Console.WriteLine("伺服器未知錯誤,登陸失敗!!")
Return False
End If

End Function

'登出函數
Private Sub exitApp()

Dim loginOutStr As String = LOGINOUT & username
Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(loginOutStr)
ClientSocket.SendTo(sendBytes, ServerEP)

End Sub

'請求好友清單的函數
Private Function getU() As Boolean

getUDone = New ManualResetEvent(False)
Dim getUbytes As Byte() = Encoding.Unicode.GetBytes(GETULIST)
ClientSocket.SendTo(getUbytes, ServerEP)

Dim data As [Byte]() = New Byte(4056) {}
Dim comStr As String = Encoding.Unicode.GetString(data, 0, 4)

Dim GUrecv As New myMethodDelegate(AddressOf recvGetU)
GUrecv.BeginInvoke(data, Nothing, Nothing)

getUDone.WaitOne(30000, True)

Dim recvStr As String = Encoding.Unicode.GetString(data, 0, 4)

If recvStr = comStr Then
Console.WriteLine("伺服器超時.或取好友名單失敗!!")
Return False
End If

If Encoding.Unicode.GetString(data, 0, 4) = GETUSER Then
getUserList(data, getUrecCount)
Console.WriteLine("獲取線上名單成功!!")
showUserList()
Return True
Else
Console.WriteLine("伺服器未知錯誤,獲取線上名單失敗!!")
Return False
End If

End Function

'登陸時用來非同步接收伺服器發送的消息
Sub recvLogin(ByRef inData As Byte())

ClientSocket.Receive(inData)
receiveDone.Set()

End Sub

'請求好友名單時用來非同步接收伺服器發送的消息
Sub recvGetU(ByRef inData As Byte())

getUrecCount = ClientSocket.Receive(inData)
getUDone.Set()

End Sub

'處理收到的線上使用者資訊
Private Sub getUserList(ByVal userInfobytes() As Byte, ByVal reccount As Integer)

Dim ustr As String = Encoding.Unicode.GetString(userInfobytes, 4, reccount - 4)

Dim splitStr() As String = Nothing

splitStr = Ustr.Split("|")

Dim IPEPSplit() As String = Nothing

Dim i As Integer = 0

Dim k As Integer
For k = 0 To splitStr.Length - 2 Step 2
ReDim Preserve OLUserName(i)
ReDim Preserve OLUserEP(i)

OLUserName(i) = splitStr(k)
IPEPSplit = splitStr(k + 1).Split(":")
OLUserEP(i) = New IPEndPoint(IPAddress.Parse(IPEPSplit(0)), IPEPSplit(1))

IPEPSplit = Nothing
i += 1
Next

End Sub

'顯示線上使用者
Private Sub showUserList()
Dim i As Integer
For i = 0 To OLUserName.Length - 1
If OLUserName(i) <> "" Then
Console.WriteLine("使用者名:" & OLUserName(i) & " 使用者IP:" & OLUserEP(i).ToString)
End If
Next
End Sub

'客戶程式監聽的函數
Sub listen()

While True

Try
Dim recv As Integer = 0 '收到的位元組數
Dim data As [Byte]() = New Byte(1024) {} '緩衝區大小
Dim sender As New IPEndPoint(IPAddress.Any, 0)
Dim tempRemoteEP As EndPoint = CType(sender, EndPoint)
recv = ClientSocket.ReceiveFrom(data, tempRemoteEP)

Dim msgHead As String = Encoding.Unicode.GetString(data, 0, 4) '獲得消息頭的內容
Select Case msgHead
Case MSGEND
msgSendEnd = True
sendDone.Set()
Case LOGININ
addOnLine(data, recv)
Case LOGINOUT
removeOnLine(data, recv)
Case MSGEND
msgSendEnd = True
sendDone.Set()
Case MAKHOLD
Console.WriteLine(Chr(10) & Chr(13) & "收到打洞消息.")
makeHold(data, recv)
Console.Write("Client>")
Case CHATMSG
showChatMsg(data, recv)
Case HOLDOK
testHold = True
holdDone.Set()
Case CHTMSGEND
testChat = True
chatDone.Set()
End Select

Catch
End Try

End While
End Sub

'發送聊天消息
Private Sub sendChatMsg(ByVal remoteUser As String, ByVal chatMsgStr As String)

If remoteUser = username Then
Console.WriteLine("豬頭,你想幹什麼!!!")
Exit Sub
End If

Dim i As Integer

Dim remoteUEP As IPEndPoint
For i = 0 To OLUserName.Length - 1
If remoteUser = OLUserName(i) Then
remoteUEP = OLUserEP(i)
Exit For
End If
If i = OLUserName.Length - 1 Then
Console.WriteLine("找不到你想發送的使用者.")
Exit Sub
End If
Next

Dim msgbytes() As Byte = Encoding.Unicode.GetBytes(CHATMSG & username & "|" & chatMsgStr)
Dim holdbytes() As Byte = Encoding.Unicode.GetBytes(P2PCONN & username & "|" & remoteUser)

chatDone = New ManualResetEvent(False)
ClientSocket.SendTo(msgbytes, remoteUEP)
chatDone.WaitOne(10000, True)
If testChat = True Then
testChat = False
Exit Sub
End If

testHold = False
While testHold <> True
Console.WriteLine("打洞ing.....")
holdDone = New ManualResetEvent(False)
ClientSocket.SendTo(holdbytes, remoteUEP)
ClientSocket.SendTo(holdbytes, ServerEP)
holdDone.WaitOne(10000, True)
If testHold = True Then
Exit While
Else
Console.WriteLine("打洞超時,發送消息失敗.")
Console.Write("是否重試,按Y重試,按任意值結束髮送:")
Dim YorN As String = Console.ReadLine().ToUpper
If YorN = "Y" Then
testHold = False
Else
Exit Sub
End If
End If
End While

While testChat <> True
Console.WriteLine("打洞成功,正在準備發送.....")
chatDone = New ManualResetEvent(False)
ClientSocket.SendTo(msgbytes, remoteUEP)
chatDone.WaitOne(10000, True)
If testChat = True Then
Console.WriteLine("消息發送成功!!")
Exit While
Else
Console.WriteLine("發送超時,發送消息失敗.")
Console.Write("是否重試,按Y重試,按任意值結束髮送:")
Dim YorN As String = Console.ReadLine().ToUpper
If YorN = "Y" Then
testChat = False
Else
Exit Sub
End If
End If
End While
testHold = False
testChat = False
End Sub

'處理聊天消息
Private Sub showChatMsg(ByVal indata() As Byte, ByVal recvcount As Integer)
Dim msgStr As String = Encoding.Unicode.GetString(indata, 4, recvcount - 4)
Dim splitStr() As String = msgStr.Split("|")
Dim fromUname As String = splitStr(0)
Dim msg As String = splitStr(1)
Console.WriteLine(Chr(10) & Chr(13) & "收到來自" & fromUname & "的消息:" & msg)
Console.Write("Client>")
Dim i As Integer
For i = 0 To OLUserName.Length - 1
If OLUserName(i) = fromUname Then
Exit For
End If
Next
Dim tempbytes() As Byte = Encoding.Unicode.GetBytes(CHTMSGEND)
ClientSocket.SendTo(tempbytes, OLUserEP(i))
End Sub

'處理打洞函數
Private Sub makeHold(ByVal indata() As Byte, ByVal recvcount As Integer)
Dim makholdstr As String = Encoding.Unicode.GetString(indata, 4, recvcount)
Dim ipepstr() As String = makholdstr.Split(":")
Dim holdEP As IPEndPoint = New IPEndPoint(IPAddress.Parse(ipepstr(0)), ipepstr(1))

Dim holdbytes() As Byte = Encoding.Unicode.GetBytes(HOLDOK & username)
ClientSocket.SendTo(holdbytes, holdEP)
Console.WriteLine("回送打洞消息.")
End Sub

'處理使用者上線的函數
Private Sub addOnLine(ByVal inData() As Byte, ByVal recvCount As Integer)
Dim inStr As String = Encoding.Unicode.GetString(inData, 4, recvCount - 4)
Dim userinfo() As String = inStr.Split("|")
Dim strUserEP() As String = userinfo(1).Split(":")

Dim i As Integer
For i = 0 To OLUserName.Length - 1
If OLUserName(i) = "" Then
OLUserName(i) = userinfo(0)
OLUserEP(i) = New IPEndPoint(IPAddress.Parse(strUserEP(0)), strUserEP(1))
Console.WriteLine(Chr(10) & Chr(13) & "使用者" & OLUserName(i) & "上線了. 使用者位址:" & OLUserEP(i).ToString)
Console.Write("Client>")
Exit Sub
End If
Next

ReDim Preserve OLUserName(i + 1)
ReDim Preserve OLUserEP(i + 1)

OLUserName(i + 1) = userinfo(0)
OLUserEP(i + 1) = New IPEndPoint(IPAddress.Parse(strUserEP(0)), strUserEP(1))

Console.WriteLine(Chr(10) & Chr(13) & "使用者" & OLUserName(i + 1) & "上線了. 使用者位址:" & OLUserEP(i + 1).ToString)
Console.Write("Client>")

End Sub

'處理使用者下線的函數
Private Sub removeOnLine(ByVal inData() As Byte, ByVal recvCount As Integer)
Dim offUname As String = Encoding.Unicode.GetString(inData, 4, recvCount - 4)

Dim i As Integer
For i = 0 To OLUserName.Length - 1
If OLUserName(i) = offUname Then
OLUserName(i) = ""
OLUserEP(i) = Nothing
Console.WriteLine(Chr(10) & Chr(13) & "使用者" & offUname & "下線了.")
Console.Write("Client>")
Exit Sub
End If
Next
End Sub

'發送消息的函數
Public Function sendmsg(ByVal msg As String, ByVal sendToIPEP As IPEndPoint) As String

Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(msg)

'判斷發送的位元組數是否超過了伺服器緩衝區大小
If sendBytes.Length > 1024 Then
Return "W輸入的字數太多"
End If

'判斷消息是否發送成功
While msgSendEnd = False

sendDone = New ManualResetEvent(False)

Try

ClientSocket.SendTo(sendBytes, sendToIPEP)

sendDone.WaitOne(10000, True) '阻塞執行緒10秒

If msgSendEnd = False Then
Console.WriteLine("消息發送超時")
Else
Exit While
End If

Catch e As Exception

Console.WriteLine("發送消息失敗" & e.ToString)
Exit Function

End Try

Console.Write("是否重試?按Y重試,按任意鍵退出:")
Dim userInput As String = Console.ReadLine.ToUpper

If userInput = "Y" Then
Else
msgSendEnd = False
Exit Function
End If

End While

msgSendEnd = False

End Function

'用保持線上狀態的函數
Private Sub holdonline(ByVal state As [Object])
ClientSocket.SendTo(holdBytes, ServerEP)
End Sub

#End Region

End Module

vb.net的socket程式設計

(1) 用戶端:

‘發送

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytes(1024) As Byte '聲明位元組陣列
Dim sender1 As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
'初始化socket
Dim msg As Byte() = System.Text.Encoding.Unicode.GetBytes(TextBox1.Text)
'對發送的資料進行編碼
'***************************
'指定ip和埠
Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("192.168.22.131")
Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(0)
Dim ipe As New System.Net.IPEndPoint(ipAddress, 11000)
'**********************
sender1.Connect(ipe) '建立連接
Dim bytesSent As Integer = sender1.Send(msg) '發送資料
'關閉socket
sender1.Shutdown(Net.Sockets.SocketShutdown.Both)
sender1.Close()
End Sub

(2)伺服器端:

Dim listener As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
'初始socket

‘接收
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytes() As Byte
Dim handler As System.Net.Sockets.Socket = listener.Accept() '建立連接請求

Dim data As String = Nothing
bytes = New Byte(1024) {}
Dim bytesRec As Integer = handler.Receive(bytes) '接收資料
If bytesRec > 0 Then
data = System.Text.Encoding.Unicode.GetString(bytes, 0, bytesRec)
TextBox1.Text = data
Else
Exit Sub
End If
handler.Shutdown(Net.Sockets.SocketShutdown.Both)
handler.Close()
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'指定ip和埠
Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New System.Net.IPEndPoint(ipAddress, 11000)
listener.Bind(localEndPoint)
listener.Listen(10)
End Sub

'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

總結如下:

Imports System.Net
Imports System.Net.Sockets
Imports System.Net.Dns

1.本機ip:127.0.0.1;廣播IP:255.255.255.255。

應用UdpClient實現socket

廣播發送:

Dim udpClient As UdpClient = New UdpClient()
Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Parse("255.255.255.255"), 8888)

Dim DefaultIP As String = "127.0.0.1"
Dim ip As IPAddress

For Each ip In Dns.GetHostEntry(Dns.GetHostName()).AddressList
If ip.AddressFamily = AddressFamily.InterNetwork Then
DefaultIP = ip.ToString()
Exit For
End If
Next

Dim computerInfo As String = ":USER:" & txtUser & ":" & Dns.GetHostName() & ":" & DefaultIP & ":" & txtGroup
Dim buff As Byte() = Encoding.Default.GetBytes(computerInfo)
computerInfo = Nothing

udpClient.Send(buff, buff.Length, ep)
System.Threading.Thread.Sleep(2000)
buff = Nothing

廣播接收:

Dim server As UdpClient = New UdpClient(8888)
Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)

Dim buff As Byte() = server.Receive(ep)
Dim user As String = Encoding.Default.GetString(buff, 0, buff.Length)
Dim cmd As String = user.Substring(0, 6)
Dim user1 As String = user.Substring(6)

Dim s As String() = user1.Split(":")

2.Socket發送:

Dim IpHostName As String = Me.ListViewUser.Items(i).SubItems(2).Text
'初始化接受通訊端:定址方案,以字元流方式和Tcp通信(IP位址不能用system.net.socket.gethostentry(dns.gethostname)形式,這樣CE給CE和PC發信息都發不通)
socketSent = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
ipSent = New IPEndPoint(System.Net.IPAddress.Parse(IpHostName), 8887)

'與伺服器進行連接
socketSent.Connect(ipSent)

'將要發送的消息轉化為位元組流,然後發送
socketSent.Send(Encoding.Default.GetBytes(SendMsg.Trim()))

socketSent.Close()

發送過程:聲明socket物件——獲取要連接的設備IP和埠(IPendPoint物件)——連接connect——發送Send

3.socket接收

'初始化接受通訊端:定址方案,以字元流方式和Tcp通信
socketReceive = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)

'獲取本機IP位址並設置接受資訊的埠
Dim DefaultIP As String = "127.0.0.1"
Dim ip As IPAddress

For Each ip In Dns.GetHostEntry(Dns.GetHostName()).AddressList
If ip.AddressFamily = AddressFamily.InterNetwork Then
DefaultIP = ip.ToString()
Exit For
End If
Next

ipReceive = New IPEndPoint(IPAddress.Parse(DefaultIP), 8887)
'將本機IP位址和接受埠綁定到接受通訊端
socketReceive.Bind(ipReceive)
'監聽埠,並設置監聽佇列最大長度
socketReceive.Listen(1024)

接收過程:聲明socket物件——獲取本機設備IP和埠(IPendPoint物件)——綁定Bind——監聽listen


MSDN Winsock TCP/IP

參考引用來源
-----
Server端:

    class ServerReceive
    {
        static void Main(string[] args)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 8000);

            Socket s = new Socket(endPoint.Address.AddressFamily,
                SocketType.Dgram,
                ProtocolType.Udp);

            // Creates an IPEndPoint to capture the identity of the sending host.
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint senderRemote = (EndPoint)sender;

            // Binding is required with ReceiveFrom calls.
            s.Bind(endPoint);

            byte[] msg = new Byte[20]; // Buffer
            Console.WriteLine("Waiting to receive datagrams from client...");

            while(Console.KeyAvailable == false)
            {
                // This call blocks.
                s.Receive(msg);
                //s.ReceiveFrom(msg, ref senderRemote);
                Console.WriteLine("\n接收到的字串: " + Encoding.ASCII.GetString(msg));
            }
            s.Close();
        }
    }

Client端:

    class ClientSend
    {
        static void Main(string[] args)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 8000);
            Socket s = new Socket(endPoint.Address.AddressFamily,
                SocketType.Dgram,
                ProtocolType.Udp);

            byte[] msg = Encoding.ASCII.GetBytes("This is a test");
            Console.WriteLine("Sending data.");
            // This call blocks.
            s.SendTo(msg, endPoint);
            s.Close();
        }
    }