C#在单击窗体按钮时调用一个方法



我已经试了一整天了,但我没有看到任何有用的资源来解决这个问题。

单击按钮时,我想从Dashboard窗体调用MyTcpListener类中的Server()

提前谢谢。

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Csearch
{
class MyTcpListener
{
public static void Server()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also use server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("nHit enter to continue...");
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Csearch;
namespace Csearch
{

public partial class Dashboard : Form
{
public Dashboard()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
// Server Start
private void button1_Click(object sender, EventArgs e)
{

}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{ 
}
// Client Start
private void button1_Click_1(object sender, EventArgs e)
{
Console.WriteLine("This is the start of something very great");

}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{

}
}
}

由于您已经在MyTcpListener中将Server定义为静态方法,因此可以像下面这样直接调用它。

private void button1_Click(object sender, EventArgs e)
{
MyTcpListener.Server();
}

将类设为公共静态

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Csearch
{
public static class MyTcpListener
{
public static void Server()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also use server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("nHit enter to continue...");
Console.Read();
}
}
}

您的类和方法应该是publicstatic,如下所示。这样您就可以使用它,而无需创建类的实例并直接调用它。

public static class MyTcpListener
{
public static void Server()
{

您只需要将类(MyTcpListener(更改为public,然后调用您的方法,如下所示:

namespace Csearch
{
public class MyTcpListener
{
public static void Server()
{
//......
}
}
}
---------------------------------
namespace Csearch
{ 
public partial class Dashboard : Form
{
public Dashboard()
{
InitializeComponent();
}
// Server Start
private void button1_Click(object sender, EventArgs e)
{
MyTcpListener.Server();
}
}

最新更新