0

たった1つの単純なASPXページを作成しました<asp:Button>。このページは、Socketを介してWindowsフォームアプリケーションとの接続を行います。

どのように機能する必要があります

クリックするたびに、<asp:Button>読み取ったWindowsフォームに情報を渡す必要があります。その後、それを印刷します。

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();
        }

問題

自分の中で1回クリックして<asp:Button>、作品を初期化するとClient Applicationうまくいきます。<asp:Button>2回目に、リターンに渡すパラメーターをクリックすると、Client Application空になります。

クライアントアプリケーションで空を読んだ場所

string returndata = System.Text.Encoding.ASCII.GetString(inStream);

4

1 に答える 1

0

ページのボタンを押したときにtcpListenerを作成するのはなぜですか?TcpClientを作成し、コマンドを送信する必要がありますWinformアプリこのコードはコマンドを送信し、サーバーから応答を取得します

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));

それで全部です

于 2012-10-26T12:59:11.203 に答える