我正在制作一个应用程序,它将使用IP地址和端口号从设备读取数据并将其保存到文本文件中。这是我的代码..
ipaddress = "192.168.1.230";
int port = int.Parse("2300");
textfileSaveLocation = "D://vikas//data.txt";
byte[] data = new byte[1024];
string stringData;
string input;
IPAddress ipadd = IPAddress.Parse(ipaddress);
IPEndPoint ipend = new IPEndPoint(ipadd,port);
Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
sock.NoDelay = false;
try
{
sock.Connect(ipend);
textBox4.AppendText("Connected to host" + "\r\n");
}
catch (Exception dfg)
{
textBox4.AppendText("Problem connecting to host" + "\r\n");
textBox4.AppendText(dfg.ToString ()+"\r\n");
return;
}
try
{
int recv = sock.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
textBox4.AppendText(stringData + "\r\n");
while (true)
{
Application.DoEvents();
input = "Client here";
sock.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = sock.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
textBox4.AppendText(stringData + "\r\n");
string df = "";
try
{
FileStream dr = new FileStream(textfileSaveLocation, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
StreamReader fg = new StreamReader(dr);
df = fg.ReadToEnd();
fg.Dispose();
dr.Dispose();
}
catch (Exception dfjdfs)
{
}
try
{
File.Delete(textfileSaveLocation);
}
catch (Exception jhu)
{
}
try
{
FileStream cd = new FileStream(textfileSaveLocation, FileMode.Create);
StreamWriter cdf = new StreamWriter(cd);
cdf.WriteLine (df);
cdf.WriteLine (stringData);
cdf.Dispose();
cd.Dispose();
}
catch (Exception hgy)
{
}
}
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
catch (Exception DFGFD)
{
}
}
在运行上面的代码时我只得到
Client here client here
在文本文件中的不同解析格式中,需要存储数据。
On Reviewing我知道我需要在代码中输入用户名和密码来从机器上读取数据,但我没有在哪里添加..
请帮助我找出在发布的代码中添加用户名和密码的位置,以便将数据读入文本文件。 提前谢谢。