click button not working in server GUI program

SK33

New member
Joined
Sep 26, 2015
Messages
4
Programming Experience
Beginner
I have created a small server using socket programming in c# which will receive a file from a client and save it in a directory. I have made two buttons for the GUI of server. one is 'connect' which will start the server on clicking and the other is 'disconnect' which will stop the server. I have made a text box which is supposed to show a line "server started" when the connect button is pressed. But this is not working when I click on connect button. Moreover, after clicking the 'connect' button, 'disconnect' button isn't working 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 connect = new Button();
    connect.Parent = this;
    connect.Text = "Connect";
    connect.Location = new Point(295, 20);
    connect.Size = new Size(6 * Font.Height, 2 * Font.Height);
    connect.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 working**

    //Infinite loop to connect to new clients      
    while (true)
    {
        // Accept 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()); }
}
 
Last edited by a moderator:
newText.Text = "Server started"; //**This line is not working**
It is "working", but you are blocking the UI thread. Get rid of the While loop and for example use the asynchronous BeginAcceptTcpClient to move that processing to a different thread.
 
It is "working", but you are blocking the UI thread. Get rid of the While loop and for example use the asynchronous BeginAcceptTcpClient to move that processing to a different thread.
Can you please give me the correct code?
 
Back
Top Bottom