如何将从服务器收到的结果存储到文本文件中 [英] How can I store the results I received from the server to a textfile

查看:118
本文介绍了如何将从服务器收到的结果存储到文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Can anybody help me with this here.  I am working on a network application where I have to create a socket connection to the IP address and port that they already given me and then send XML message to the socket and finally include the ReadMe.txt file where I will save what I have received from the server
<pre>Send the following XML message to the socket, Please note there is no security, no certificates, just a plan TCP/IP connection. The
request message is formatted below. Please include a README.txt file where you saved what you have received from the server.
Without this file, your assignment will not be evaluated.
<request>
<EventType>Authentication</EventType>
<event>
<UserPin>12345</UserPin>
<DeviceId>12345</DeviceId>
<DeviceSer>ABCDE</DeviceSer>
<DeviceVer>ABCDE</DeviceVer>
<TransType>Users</TransType>
</event>
</request>







我的尝试:






What I have tried:

private static Socket socket;

public static void main(String args[])
{
    try
    {
        socket = new Socket( "196.37.22.179", 9011);

        //Send the message to the server
        //PrintStream outstrm = null;
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);

        String str;
        str = "<request>";
        str += "<EventType>Authentication</EventType>";
        str += "<event>";
        str += "<UserPin>12345</UserPin>";
        str += "<DeviceId>12345</DeviceId>";
        str += "<DeviceSer>ABCDE</DeviceSer>";
        str += "<DeviceVer>ABCDE</DeviceVer>";
        str += "<TransType>Users</TransType>";
        str += "</event></request>";
        bw.write(str);
        bw.flush();
        System.out.println("Message sent to the server......! ");

        //Get the return message from the server
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);        
    }
    catch (Exception exception)
    {
        exception.printStackTrace();
    }
    finally
    {
        //Closing the socket
        try
        {
            socket.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

推荐答案

引用:

我想读取README.txt文件,将服务器的结果存储在文件中

I want to read from README.txt file to store results from server in a file




string text = File.ReadAllText(path);

string[] lines = File.ReadAllLines(path);



将数据添加到文件同样简单:


To add data to the file is just as simple:

File.AppendAllText(path, data);


这篇关于如何将从服务器收到的结果存储到文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆