您的位置:程序门 -> .net技术 -> vb.net



求助各位大虾!!帮帮忙


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求助各位大虾!!帮帮忙
发表于:2007-05-29 11:56:51 楼主
以下是一个vb.net编写的tcp协议窗口联机程序,在visual   studio2005上运行出现了警告,连不上,由于我只是个初学者,很多部明白,但由于时间很急,希望各位大虾帮帮忙,拜托了,真的很感谢!!
imports   system.net
imports   system.net.sockets
imports   system.threading
imports   system.text
public   class   form1
        inherits   system.windows.forms.form
        dim   mode   as   integer   =   0
        public   s_socket   as   system.net.sockets.socket
        public   c_socket   as   socket
        private   sub   form1_load(byval   sender   as   object,   byval   e   as   system.eventargs)   handles   mybase.load
                button1.enabled   =   true
                button2.enabled   =   false
                button3.enabled   =   false
                label2.text   =   "联机主机名称 "
        end   sub

        private   sub   radiobutton1_checkedchanged(byval   sender   as   object,   byval   e   as   system.eventargs)   handles   radiobutton1.click
                mode   =   0
                label2.text   =   "主机名称 "
                dim   hostname   as   string   =   dns.gethostname()
                dim   serverip   as   ipaddress   =   dns.gethostentry(hostname).addresslist(0)
                textbox2.text   =   serverip.tostring
        end   sub

        private   sub   radiobutton2_checkedchanged(byval   sender   as   object,   byval   e   as   system.eventargs)   handles   radiobutton2.click
                mode   =   1
                label2.text   =   "联机主机名称 "
        end   sub

        private   sub   button1_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button1.click
                button1.enabled   =   false
                button2.enabled   =   true
                if   mode   =   0   then
                        try
                                dim   hostname   as   string   =   dns.gethostname()
                                dim   serverip   as   ipaddress   =   dns.gethostentry(hostname).addresslist(0)
                                dim   port_no   as   string   =   "8000 "

                                dim   server1   as   new   tcplistener(serverip,   system.int32.parse(port_no))
                                console.writeline( "server   ip:   "   +   serverip.tostring()   +   "port: "   +   port_no)
                                server1.start()
                                textbox1.text   =   "等待联机中! "
                                dim   listen1   as   new   server_thread(server1)
                                listen1.main1   =   me
                                dim   serverthread1   as   new   thread(new   threadstart(addressof   listen1.startup))
                                serverthread1.start()
                        catch   e1   as   exception
                                msgbox(e1.stacktrace.tostring())
                        end   try
                else
                        button3.enabled   =   true
                        dim   ip   as   string   =   textbox2.text
                        dim   port   as   integer   =   8000
                        dim   host_address1   as   ipaddress   =   dns.gethostentry(ip).addresslist(0)
                        dim   host1   as   new   ipendpoint(host_address1,   port)
                        c_socket   =   new   socket(addressfamily.internetwork,   sockettype.stream,   protocoltype.tcp)
                        try
                                textbox1.text   =   "等待联机中! "
                                c_socket.connect(host1)
                                textbox1.text   =   "联机中! "
                                dim   listen2   as   new   client_thread
                                listen2.main1   =   me
                                dim   clientthread1   as   new   thread(new   threadstart(addressof   listen2.startup))
                                clientthread1.start()
                        catch   e1   as   exception
                                msgbox(e1.tostring())
                                exit   sub
                        end   try
                end   if
        end   sub

        private   sub   button2_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button2.click
                dim   str1   as   string
                try
                        if   mode   =   0   then
                                str1   =   textbox4.text
                                dim   databuf()   as   byte   =   encoding.unicode.getbytes(str1.tochararray())
                                s_socket.send(databuf,   databuf.length,   socketflags.none)
                        else
                                str1   =   textbox4.text
                                dim   databuf()   as   byte   =   encoding.unicode.getbytes(str1.tochararray())
                                c_socket.send(databuf,   databuf.length,   socketflags.none)
                        end   if
                catch   ex   as   exception
                        msgbox(ex)
                end   try
                textbox4.text   =   " "
        end   sub

        private   sub   button3_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button3.click
                try
                        if   mode   =   0   then
                        else
                                dim   str1   as   string
                                str1   =   "close "
                                dim   databuf()   as   byte   =   encoding.unicode.getbytes(str1.tochararray())
                                c_socket.send(databuf,   databuf.length,   socketflags.none)
                                c_socket.shutdown(socketshutdown.both)
                                c_socket.close()
                                button2.enabled   =   false
                                button3.enabled   =   false
                        end   if
                catch   ex   as   exception
                        msgbox(ex)
                end   try
                textbox1.text   =   "离线 "
        end   sub
end   class
public   class   server_thread
        friend   main1   as   form1
        private   p_tcp   as   system.net.sockets.tcplistener
        public   sub   new(byval   tcp1   as   tcplistener)
                me.p_tcp   =   tcp1
        end   sub
        public   sub   startup()
                try
                        main1.s_socket   =   p_tcp.acceptsocket()
                        main1.textbox1.text   =   "联机中! "
                        dim   client1   as   ipendpoint   =   ctype(main1.s_socket.remoteendpoint,   ipendpoint)
                        while   (true)
                                dim   databuf(1024)   as   byte
                                dim   x   as   integer   =   main1.s_socket.receive(databuf,   0,   main1.s_socket.available,   socketflags.none)
                                dim   str1   as   string   =   encoding.unicode.getstring(databuf,   0,   x)
                                if   str1.length   >   0   then
                                        str1   =   str1   &   chr(13)   &   chr(10)
                                        main1.textbox3.text   +=   str1
                                end   if
                                if   str1   =   "close "   then
                                        main1.textbox1.text   =   "离线 "
                                        exit   while
                                end   if
                        end   while
                        main1.s_socket.shutdown(socketshutdown.both)
                        main1.s_socket.close()
                catch   e   as   exception
                        msgbox(e.stacktrace.tostring())
                        if   main1.s_socket.connected   then
                                main1.s_socket.close()
                        end   if
                end   try
        end   sub
end   class
public   class   client_thread
        friend   main1   as   form1
        public   sub   startup()
                try
                        while   (true)
                                dim   databuf(1024)   as   byte
                                dim   x   as   integer   =   main1.c_socket.receive(databuf,   0,   main1.c_socket.available,   socketflags.none)
                                dim   str1   as   string   =   encoding.unicode.getstring(databuf,   0,   x)
                                if   str1.length   >   0   then
                                        str1   =   str1   &   chr(13)   &   chr(10)
                                        main1.textbox3.text   +=   str1
                                end   if
                        end   while
                        main1.c_socket.shutdown(socketshutdown.both)
                        main1.c_socket.close()
                catch   e   as   exception
                        msgbox(e.stacktrace.tostring())
                        if   main1.c_socket.connected   then
                                main1.c_socket.close()
                        end   if
                end   try
        end   sub
end   class


快速检索

最新资讯
热门点击