1

大家好,感谢您花时间帮助我解决我的问题。我刚开始编程,所以如果我问愚蠢的问题,请原谅我。

我在用 C# 编写的 Windows 窗体应用程序中有一个服务器,以及一个连接到服务器的客户端。客户端是一个用 Java 编写的 Android 应用程序。

我可以成功连接并向服务器发送数据(这是一个 XML 字符串)。我的问题是,无论我做什么,我都无法从服务器以“Bla bla bla the string was successful receivedThank you”的形式得到响应。发生这种情况是因为服务器没有发送数据,或者我的客户端没有收到它。尽管我已经搜索并尝试了不同的代码组合,但我无法弄清楚。

下面是服务器和客户端的代码:

客户:

public void functionSend (View view)
{
    RadioGroup type = (RadioGroup) findViewById(R.id.radioGroupType);
    TextView ip = (TextView) findViewById(R.id._ip);
    TextView port = (TextView) findViewById(R.id.port);
    TextView event = (TextView) findViewById(R.id.event);
    TextView amount = (TextView) findViewById(R.id.amount);
    TextView comment = (TextView) findViewById(R.id.comment);
    Spinner currency = (Spinner) findViewById(R.id._currencySpinner);
    _ip = ip.getText().toString();
    _event = event.getText().toString();
    _amount = amount.getText().toString();
    _comment = comment.getText().toString();
    if (_comment.equals(""))
    {
        _comment = "empty";
    }
    _currency = currency.getSelectedItem().toString();
    if (port.getText().toString().equals(""))
    {
        _port = 3344; port.setText("3344");
    }
    else
    {
        _port = Integer.parseInt(port.getText().toString());
    }
    rbID = type.getCheckedRadioButtonId();
    if (rbID == R.id.RadioButtonOutgo)
    {
        _type = "outgo";
    }
    else
    {
        _type = "income";
    }

    //------formam XML string--------
    method = "create event";
    key = "123456";
    //formam stringul xml care i-l vom transmite catre server
    clientXMLStream = "<?xml version='1.0' encoding='UTF-8'?>";
    clientXMLStream = clientXMLStream + "\n <Client>";
    clientXMLStream = clientXMLStream +  "\n <ClientKey>" + key + "</ClientKey>";
    clientXMLStream = clientXMLStream +  "\n <Method>" + method + "</Method>";
    clientXMLStream = clientXMLStream +  "\n <ForDay>" + day + "</ForDay>";
    clientXMLStream = clientXMLStream +  "\n <ForMonth>" + (month+1) + "</ForMonth>";
    clientXMLStream = clientXMLStream +  "\n <ForYear>" + year + "</ForYear>";
    clientXMLStream = clientXMLStream +  "\n <Event>" + _event + "</Event>";
    clientXMLStream = clientXMLStream +  "\n <Amount>" + _amount + "</Amount>";
    clientXMLStream = clientXMLStream +  "\n <Type>";
    clientXMLStream = clientXMLStream +  _type + "</Type>";
    clientXMLStream = clientXMLStream +  "\n <Currency>" + _currency + "</Currency>";
    clientXMLStream = clientXMLStream +  "\n <Comment>";
    clientXMLStream = clientXMLStream +  _comment + "</Comment>";
    clientXMLStream = clientXMLStream +  "\n </Client>";
    //--------------------------------
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    DataOutputStream dataOutputStream = null;
    DataInputStream dataInputStream = null;
    try
    {
        Socket socket = null;
        socket = new Socket(_ip, _port);
        //socket.setSoTimeout(2000);
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataInputStream = new DataInputStream(socket.getInputStream());

        dataOutputStream.writeBytes(clientXMLStream.toString());
        event.setText(dataInputStream.readUTF().toString());
        dataOutputStream.flush();
        socket.close();
    }
    catch (Exception e)
    {

    }
}

这是服务器代码:

    void startServer()
{
    Thread listenThread = new Thread(new ThreadStart(listenToMessages));
    listenThread.Start();
}

public void listenToMessages()
{
    tcplistener.Start();
    while (true)
    {
        TcpClient client = tcplistener.AcceptTcpClient();
        Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
        clientThread.Start(client);
    }
}

public void HandleClientComm(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();

    byte[] message = new byte[4096];
    int bytesRead;

    while (true)
    {
        bytesRead = 0;

        try
        {
            bytesRead = clientStream.Read(message, 0, 4096);
            NetworkStream _clientStream = tcpClient.GetStream();

            ASCIIEncoding _encoder = new ASCIIEncoding();
            byte[] buffer = _encoder.GetBytes("Some Contacts as a string!");
            byte[] lengthBuffer = BitConverter.GetBytes(buffer.Length);
            clientStream.Write(lengthBuffer, 0, lengthBuffer.Length);
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
            tcpClient.Close();

            //clientStream.Close();
        }
        catch
        {
            break;
        }

        if (bytesRead == 0)
        {
            break;
        }
        tcpClient.Close();
        ASCIIEncoding encoder = new ASCIIEncoding();
        processRemoteInputs(encoder.GetString(message, 0, bytesRead));
        //reply

    }
}

void stopServer()
{

}

void processRemoteInputs(String clientXmlString)
{
    XmlDocument remote = new XmlDocument();
    remote.LoadXml(clientXmlString);
    XmlNode method = remote.SelectSingleNode(".//Client/Method");
    if (method.InnerText == "create event")
    {
        XmlNode remoteDay = remote.SelectSingleNode(".//Client/ForDay");
        _neSelDay = Convert.ToInt16(remoteDay.InnerText);
        XmlNode remoteMonth = remote.SelectSingleNode(".//Client/ForMonth");
        _neSelMonth = Convert.ToInt16(remoteMonth.InnerText);
        XmlNode remoteYear = remote.SelectSingleNode(".//Client/ForYear");
        _neSelYear = Convert.ToInt16(remoteYear.InnerText);
        XmlNode remoteEvent = remote.SelectSingleNode(".//Client/Event");
        String _remoteEvent = remoteEvent.InnerText;
        XmlNode remoteAmount = remote.SelectSingleNode(".//Client/Amount");
        String _remoteAmount = remoteAmount.InnerText;
        XmlNode remoteType = remote.SelectSingleNode(".//Client/Type");
        String _remoteType = remoteType.InnerText;
        XmlNode remoteCurrency = remote.SelectSingleNode(".//Client/Currency");
        String _remoteCurrency = remoteCurrency.InnerText;
        XmlNode remoteComment = remote.SelectSingleNode(".//Client/Comment");
        String _remoteComment = remoteComment.InnerText;
        addNewEvent(_remoteEvent, _remoteAmount, _remoteType, _remoteCurrency, _remoteComment);
        setMonthLabel(); clearAllCurrentRows();
        loadXmlIntoDataGridView(); displayMonthDebit(); _monthDataGrid.Rows[Convert.ToInt16(_neSelDay) - 1].Selected = true;
        _monthDataGrid.CurrentCell = _monthDataGrid.Rows[Convert.ToInt16(_neSelDay) - 1].Cells[0]; displayDetailedInfoOnRowSelect();
        displayMonthChart();
    }
}
4

0 回答 0