首页
论坛
新职位
搜索论坛
什么's new
新职位
新的个人资料帖子
最新活动
会员
目前的访客
新的个人资料帖子
搜索个人资料帖子
VB.NET社区
登录
登记
什么's new
搜索
搜索
仅搜索标题
经过:
新职位
搜索论坛
Menu
Log in
Register
安装应用
安装
首页
论坛
成分& Controls
网络/插座
单击按钮在服务器GUI程序中不起作用
您正在使用过期的浏览器。它可能无法正确显示此网站或其他网站。
您应该升级或使用
替代浏览器
.
回复主题
信息
<blockquote data-quote ="SK33" data-source="post: 5623" data-attributes="member: 9813"><p>我使用c#中的套接字编程创建了一个小型服务器,该服务器将从客户端接收文件并将其保存在目录中。我为服务器的GUI创建了两个按钮。一个是<strong>'connect'</strong>这将在单击时启动服务器,另一个是<strong>'disconnect'</strong>这将停止服务器。我做了一个文本框,应该显示一行"server started" when the <strong>connect</strong>按下按钮。但这在我单击连接按钮时不起作用。此外,点击'connect' button, <strong>'disconnect'</strong> button isn'不再工作了。我的示例代码是:</p><p></p><p>[XCODE = c#]正在使用系统;</p><p>使用System.Drawing;</p><p>using System.Net;</p><p>使用System.Net.Sockets;</p><p>using System.Text;</p><p>使用System.Windows.Forms;</p><p>using System.IO;</p><p>使用System.Collections.Generic;</p><p></p><p>class sampleserver:表格</p><p>{</p><p></p><p> 私人TextBox newText;</p><p> 私有TextBox conStatus;</p><p> 私有ListBox结果;</p><p> 公共TcpClient tcpClient;</p><p> 公共TcpListener tcpListener;</p><p> 公共sampleserver()</p><p>{</p><p></p><p> Text = " TCP Server";</p><p> 大小=新大小(400,380);</p><p></p><p> newText =新的TextBox();</p><p> newText.Parent = this;</p><p> newText.Size = new Size(200,2 * Font.Height);</p><p> newText.Location =新Point(10,55);</p><p> 结果=新的ListBox();</p><p> result.Parent = this;</p><p> results.Location =新Point(10,85);</p><p> results.Size = new Size(360,18 * Font.Height);</p><p> 标签label2 = new Label();</p><p> label2.Parent = this;</p><p> label2.Text = "连接状态:";</p><p> label2.AutoSize = true;</p><p> label2.Location =新Point(10,330);</p><p> conStatus =新的TextBox();</p><p> conStatus.Parent = this;</p><p> conStatus.Text ="Disconnected";</p><p> conStatus.Size = new Size(200,2 * Font.Height);</p><p> conStatus.Location =新点(110,325);</p><p></p><p> 按钮连接= new Button();</p><p> connect.Parent = this;</p><p> connect.Text ="Connect";</p><p> connect.Location =新Point(295,20);</p><p> connect.Size = new Size(6 * Font.Height,2 * Font.Height);</p><p> connect.Click + = new EventHandler(ButtonConnectOnClick);</p><p> 按钮discon = new Button();</p><p> discon.Parent = this;</p><p> discon.Text = "Disconnect";</p><p> discon.Location = new Point(295,52);</p><p> discon.Size = new Size(6 * Font.Height,2 * Font.Height);</p><p> discon.Click + = new EventHandler(ButtonDisconOnClick);</p><p></p><p>}</p><p></p><p></p><p>void ButtonDisconOnClick(object obj,EventArgs ea)</p><p>{</p><p> Close();</p><p>}</p><p>void ButtonConnectOnClick(object obj,EventArgs ea)</p><p>{</p><p> TcpListener tcpListener =新的TcpListener(IPAddress.Any,1234);</p><p> tcpListener.Start();</p><p> newText.Text ="Server started" ;; // **此行不起作用**</p><p></p><p> //无限循环连接到新客户端 </p><p> while (true)</p><p> {</p><p> //接受一个TcpClient </p><p> TcpClient tcpClient = tcpListener.AcceptTcpClient();</p><p> 字符串地址=((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();</p><p> Console.WriteLine(地址);</p><p></p><p> Console.WriteLine("已连接到客户端");</p><p> 字节[]数据=新字节[1024];</p><p> NetworkStream ns = tcpClient.GetStream();</p><p> int recv = ns.Read(data,0,data.Length);</p><p> 字符串ID = Encoding.ASCII.GetString(data,0,recv); //接收客户ID</p><p> StreamReader reader =新的StreamReader(tcpClient.GetStream());</p><p></p><p></p><p></p><p> //来自客户端的第一条消息是文件大小 </p><p> 字符串cmdFileSize = reader.ReadLine();</p><p></p><p></p><p> //来自客户端的第一条消息是文件名 </p><p> 字符串cmdFileName = reader.ReadLine();</p><p></p><p> int长度= Convert.ToInt32(cmdFileSize);</p><p> byte []缓冲区=新的byte [length];</p><p> 收到的int = 0;</p><p> int read = 0;</p><p> 整数大小= 1024;</p><p> 剩余的int = 0;</p><p></p><p> //使用客户端发送的长度从客户端读取字节 </p><p> 一会儿(收到< length)</p><p> {</p><p> 剩余=长度-已收到;</p><p> if (remaining < size)</p><p> {</p><p> 大小=剩余;</p><p> }</p><p></p><p> 读取= tcpClient.GetStream()。读取(缓冲区,接收,大小);</p><p> 收到+ =已读;</p><p> }</p><p></p><p> 字符串根= Environment.CurrentDirectory;</p><p> 字符串文件夹= Path.Combine(root,id);</p><p> 如果(!Directory.Exists(文件夹))Directory.CreateDirectory(文件夹);</p><p></p><p></p><p></p><p> 字符串fullFilePath = Path.Combine(文件夹,Path.GetFileName(cmdFileName));</p><p> 使用(FileStream fStream = new FileStream(fullFilePath,FileMode.Create))</p><p> {</p><p> fStream.Write(buffer,0,buffer.Length);</p><p> fStream.Flush();</p><p> fStream.Close();</p><p></p><p> }</p><p></p><p> Console.WriteLine("文件已接收并保存在" + fullFilePath);</p><p> }</p><p></p><p>}</p><p>公共静态无效Main()</p><p>{</p><p> Application.Run(新的sampleserver()); }</p><p>}[/XCODE]</p></blockquote><p></p>
Insert quotes…
确认
发表回复
首页
论坛
成分& Controls
网络/插座
单击按钮在服务器GUI程序中不起作用
本网站使用Cookie来帮助个性化内容,调整您的体验并在注册时保持登录状态。
继续使用本网站,即表示您同意我们使用cookie。
接受
了解更多…
最佳
底部