click button not 加工 in server GUI program

SK33

新成员
已加入
2015年9月26日
留言内容
4
编程经验
Beginner
我使用c#中的套接字编程创建了一个小型服务器,该服务器将从客户端接收文件并将其保存在目录中。我为服务器的GUI创建了两个按钮。一个是'连接'这将在单击时启动服务器,另一个是'断开' 这将停止服务器。我做了一个文本框,应该显示一行"server started" when the 连接 button is pressed. But this is not 加工 when I click on 连接 button. Moreover, after clicking the '连接' button, '断开' button isn't 加工 anymore. My sample code is:

using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

class sampleserver : Form
{

    private TextBox newText;
    private TextBox conStatus;
    private ListBox results;
    public TcpClient tcpClient;
    public TcpListener tcpListener;
    public sampleserver()
{

    Text = " TCP Server";
    Size = new Size(400, 380);

    newText = new TextBox();
    newText.Parent = this;
    newText.Size = new Size(200, 2 * Font.Height);
    newText.Location = new Point(10, 55);
    results = new ListBox();
    results.Parent = this;
    results.Location = new Point(10, 85);
    results.Size = new Size(360, 18 * Font.Height);
    Label label2 = new Label();
    label2.Parent = this;
    label2.Text = "Connection Status:";
    label2.AutoSize = true;
    label2.Location = new Point(10, 330);
    conStatus = new TextBox();
    conStatus.Parent = this;
    conStatus.Text = "Disconnected";
    conStatus.Size = new Size(200, 2 * Font.Height);
    conStatus.Location = new Point(110, 325);

    Button 连接 = new Button();
    连接.Parent = this;
    连接.Text = "Connect";
    连接.Location = new Point(295, 20);
    连接.Size = new Size(6 * Font.Height, 2 * Font.Height);
    连接.Click += new EventHandler(ButtonConnectOnClick);
    Button discon = new Button();
    discon.Parent = this;
    discon.Text = "Disconnect";
    discon.Location = new Point(295, 52);
    discon.Size = new Size(6 * Font.Height, 2 * Font.Height);
    discon.Click += new EventHandler(ButtonDisconOnClick);

}


void ButtonDisconOnClick(object obj, EventArgs ea)
{
    Close();
}
void ButtonConnectOnClick(object obj, EventArgs ea)
{
    TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
    tcpListener.Start();
    newText.Text ="Server started"; //**This line is not 加工**

    //Infinite loop to 连接 to new clients      
    while (true)
    {
        // 接受 a TcpClient      
        TcpClient tcpClient = tcpListener.AcceptTcpClient();
        string address = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();
        Console.WriteLine(address);

        Console.WriteLine("Connected to client");
        byte[] data = new byte[1024];
        NetworkStream ns = tcpClient.GetStream();
        int recv = ns.Read(data, 0, data.Length);
        string id = Encoding.ASCII.GetString(data, 0, recv); //receives a client id
        StreamReader reader = new StreamReader(tcpClient.GetStream());



        // The first message from the client is the file size      
        string cmdFileSize = reader.ReadLine();


        // The first message from the client is the filename      
        string cmdFileName = reader.ReadLine();

        int length = Convert.ToInt32(cmdFileSize);
        byte[] buffer = new byte[length];
        int received = 0;
        int read = 0;
        int size = 1024;
        int remaining = 0;

        // Read bytes from the client using the length sent from the client      
        while (received < length)
        {
            remaining = length - received;
            if (remaining < size)
            {
                size = remaining;
            }

            read = tcpClient.GetStream().Read(buffer, received, size);
            received += read;
        }

        string root = Environment.CurrentDirectory;
        string folder = Path.Combine(root, id);
        if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);



        string fullFilePath = Path.Combine(folder, Path.GetFileName(cmdFileName));
        using (FileStream fStream = new FileStream(fullFilePath, FileMode.Create))
        {
            fStream.Write(buffer, 0, buffer.Length);
            fStream.Flush();
            fStream.Close();

        }

        Console.WriteLine("File received and saved in " + fullFilePath);
    }

}
public static void Main()
{
    Application.Run(new sampleserver()); }
}
 
由主持人最后编辑:
最佳 底部