我有一个 .Net 框架项目(c#)。我在项目中有一个表单(如图所示)
。根据为我准备的文档,我应该使用 websocket 或 socket 并按照 Json 格式在底部文本框中写入消息(我不必使用 newtonsoft 进行转换)以便能够发送消息到一个 IP 地址。
但是我对 tcpclient/tcplistener 是否也能正常工作感到困惑。(下面显示的代码是我尝试实现的代码)
Public partial class Form1 : Form
{
TcpClient clientsocket=new tcpclient();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
NetworkStream serverStream=clientSocket.GetStream();
byte[] outStream=System.Text.Encoding.ASCII.GetBytes(textBox2.Text+"$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
//byte[] inStream = new byte[4096];
//int bytesread = serverStream.Read(inStream, 0, inStream.Length);
//string returndata = System.Text.Encoding.ASCII.GetString(inStream, 0, bytesread);
//msg(returndata);
}
private void Form1_Load(object sender, EventArgs e)
{
msg("Client Started");
// server.Connect(IPEP);
clientSocket.Connect(IPAddress.Parse("000.0.0.1"), 8080);
label1.Text = "Connected Value is {0} "+ clientSocket.Connected;
Console.WriteLine("Connected Value is {0}", clientSocket.Connected);
}
public void msg(string mesg)
{
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
}
}
我试过在输入时运行程序
{
"msg_id":"ROBOT_BODY_CTRL_CMD",
“body_part”:2,
“ Action ”:3
}
然而什么也没有发生。由于我正在发送消息,我是作为客户端还是服务器?
最佳答案
关于c# - 从 C# 应用程序向服务器发送 Json 格式消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51171318/