C# 如何创建线程化客户端套接字
我用一个C# 如何创建线程化客户端套接字,c#,multithreading,sockets,C#,Multithreading,Sockets,我用一个完成了一个简单的ASPX页面。此页面将通过套接字与Windows窗体应用程序建立连接 应该如何工作 每次单击我的时,我都必须将一些信息传递给我的Windows窗体read,然后我将打印它 TCP服务器代码 public partial class PaginaTeste : System.Web.UI.Page { private TcpListener tcpListener; private Thread listenThread; protected vo
完成了一个简单的ASPX页面。此页面将通过套接字与Windows窗体应用程序建立连接
应该如何工作
每次单击我的
时,我都必须将一些信息传递给我的Windows窗体read,然后我将打印它
TCP服务器代码
public partial class PaginaTeste : System.Web.UI.Page
{
private TcpListener tcpListener;
private Thread listenThread;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnBotao_OnClick(object sender, EventArgs e)
{
this.tcpListener = new TcpListener(IPAddress.Any, 8849);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
string serverResponse = "571764;10.";
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
clientStream.Write(sendBytes, 0, sendBytes.Length);
clientStream.Flush();
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
}
tcpClient.Close();
}
客户端代码(Windows窗体)
问题
public partial class Form1 : Form
{
TcpClient clientsocket2;
Thread thread;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_click(object sender, EventArgs e)
{
this.clientsocket2 = new TcpClient();
clientsocket2.Connect("server_ip", 8849);
this.thread = new Thread(new ThreadStart(this.imprimir));
this.thread.Start();
}
protected void imprimir()
{
NetworkStream serverStream = clientsocket2.GetStream();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientsocket2.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
string r = "Printing Test";
int retorno = -1;
retorno = IniciaPorta("COM7");
if (retorno == 1)
{
ConfiguraModeloImpressora(7);
BematechTX(r);
AcionaGuilhotina(1);
FechaPorta();
}
clientSocket.Close();
}
如果我在
中单击一次并初始化客户端应用程序
就可以了。在第二次单击
时,我传递给客户端应用程序的参数返回空
我在客户端应用程序中读取空
string returndata=System.Text.Encoding.ASCII.GetString(流内)代码>为什么在按下页面中的按钮时创建tcpListener?
您需要创建TcpClient并向Winform应用程序发送comand
此代码发送comand并从服务器获得回复
string data = "Some_Comand";
byte[] b = Encoding.ASCII.GetBytes(data);
byte[] buffer = new byte[1024];
var client = new TcpClient();
var serverEndPoint = new IPEndPoint(IPAddress.Parse("ip"), 8010);
client.Connect(serverEndPoint);
var clientStream = client.GetStream();
clientStream.Write(b, 0, b.Length);
Socket s = client.Client;
byte[] receiveData = new byte[200];
s.Receive(receiveData);
Debug.WriteLine(Encoding.ASCII.GetString(receiveData));
所有这些运行一个TcpListener
(服务器)作为页面的一部分(代表请求的一部分)对我来说是一个真正的危险信号。。。你想在这里做什么?在我的服务器的特定端口中打开通信。我有一台打印机,我做了一个Windows应用程序,通过DLL访问这台打印机。在我的Web应用程序中,我将发送数据,以便在此打印机中打印。因此,我的Web应用程序将数据发送到Windows窗体,并将Windows窗体发送到打印机。我曾经尝试过与ActiveX进行通信,但是我们的web应用程序将只在FireFox中运行。为什么winform不使用REST API/web服务之类的东西呢?你有没有一些例子来告诉我如何做到这一点?只需搜索“web服务ASP.NET”那里有很多IDisposable
对象,它们应该在using
语句中