VB2010客服端程式

2021-08-19 20:09:41 字數 3195 閱讀 9179

imports system.net

imports system.net.sockets

imports system.text

imports system.threading

public class frmclient

'客戶端socket

dim clisocket as socket

'連線狀態

dim isconnected as boolean = false

'接收資訊執行緒

dim mythread as thread

private sub frmclient_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load

textbox2.text = "192.168.3.50"

end sub

sub recivemsg()

dim bytes() as byte = new byte(102400) {}

dim data as string = string.empty

dim tokens() as string

while true

dim bytesrec as integer = clisocket.receive(bytes)

data = encoding.utf8.getstring(bytes, 0, bytesrec)

tokens = data.trim.split("|")

select case tokens(0) '分析接收到的資料,可自己定義更多一些

case "exit"

isconnected = false

begininvoke(new eventhandler(addressof addinfo), tokens(1))   'invoke保證執行緒安全

clisocket.shutdown(socketshutdown.both)

clisocket.close()

exit sub

case "0xaa"

begininvoke(new eventhandler(addressof addinfo), tokens(1))   'invoke保證執行緒安全

end select

thread.sleep(10)

end while

end sub

'傳送資訊

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

'dim msg as byte() = encoding.utf8.getbytes(textbox1.text)

'dim bytessent as integer = clisocket.send(msg)

dim i as integer

dim sj() as byte

dim strsj as string

strsj = textbox1.text

redim sj(len(strsj) - 1)

for i = 1 to len(strsj)

sj(i - 1) = val("&h" & mid(strsj, i, 1))

next

dim msg as byte() = encoding.utf8.getbytes("0xaa|" + textbox1.text)

clisocket.send(msg)

'listbox1.items.add(textbox1.text)

'textbox1.text = ""

end sub

'非ui執行緒呼叫窗體控制項,保證執行緒安全。與聊天實現無關。

sub addinfo(byval sender as system.object, byval e as system.eventargs)

listbox1.items.add(sender.tostring)

end sub

'關閉視窗時發關退出資訊並清理資源

private sub form1_formclosing(byval sender as object, byval e as system.windows.forms.formclosingeventargs) handles me.formclosing

if isconnected then

dim msg as byte() = encoding.utf8.getbytes("exit|客戶端退出: " + me.handle.tostring)

dim bytessent as integer = clisocket.send(msg)

mythread.abort()

end if

end sub

private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click

button2.enabled = false

dim remoteep as new ipendpoint(net.ipaddress.parse(textbox2.text), 2052)

clisocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp)

tryclisocket.connect(remoteep)

listbox1.items.add("連線服務端成功!")

isconnected = true

mythread = new thread(addressof recivemsg)

mythread.start()

catch ex as exception

listbox1.items.add(ex.message)

end try

end sub

private sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click

close()

end sub

end class

VB2010動態載入檔案的怪問題

test.dll中的類plugin實現了iplugin form中呼叫時出錯,但如果改為第二段 則一切正常,而實際上 for 中 file變數的值和下面的pluginpath test.dll 是一樣的 你載入的不是同乙個檔案,那個出錯的時候選擇的檔案中是不是沒有這個型別?我想是有兩個為 myplu...

VB6公升級到VB2010之一 變數公升級

因為visual basic 2010已經對integer 已公升級為4位元組 和long 已公升級成8位元組 變數進行了公升級,並增加了short 2位元組 型別。所以vb6公升級到vb2010時需作如下修改 vb6 integer vb2010 short vb6 long vb2010 int...

VB6轉換C 2010函式替換

vb6轉換c 2010函式替換 vb6 c 2010 1 instr strkey,strkey.indexof instr start,string1,string2 compare 返回指定一字串在另一字串中最先出現的位置。在字串string1中,從start開始找string2,省略start...