我的 TCP 客户端应该:
- 接受主机、端口和文本字符串。
- 连接到主机/端口并在您单击按钮时发送字符串(+ 两行提要 \r\n\r\n )。
- 然后读取结果并将结果放回文本框中。
我得到 port 和 string 结果的问题,但我没有得到任何关于主机的信息。
这是我到目前为止所得到的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;
namespace TCP_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string port = port1.Text;
int myParsedInt = Int32.Parse(port);
System.Net.IPAddress[] adresslist = Dns.GetHostAddresses(host1.Text);
Socket connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectSocket.Connect(adresslist[0], myParsedInt);
System.IO.StreamReader connectionRead = new System.IO.StreamReader(new NetworkStream(connectSocket));
connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(sendText.Text + "\r\n"));
while (connectionRead.Peek() >= 0)
{
this.textOutput.AppendText(connectionRead.ReadLine() + "\r\n");
}
connectSocket.Close();
}
private void textOutput_TextChanged(object sender, EventArgs e)
{
}
private void host1_TextChanged(object sender, EventArgs e)
{
}
private void port1_TextChanged(object sender, EventArgs e)
{
}
private void sendText_TextChanged(object sender, EventArgs e)
{
}
}
}