[英]Sending Json format message from a c# application to server
I have a .Net framework Project(c#).I have a form in the project(shown in the picture) 我有一个.Net框架Project(c#)。我在项目中有一个表单(如图所示)
.Following a documentation that was prepared for me, I am supposed to use either websocket or socket and write the message in the bottom textbox following a Json Format(I dont have to convert using newtonsoft) to be able to send the message to an ip address. 根据为我准备的文档,我应该使用websocket或socket并按照Json格式(我不必使用newtonsoft进行转换)将消息写在底部文本框中,以便能够将消息发送到ip地址。
However I am confused as to whether would tcpclient/tcplistener work as well.(The code shown below is the one that I have tried implementing) 但是我对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;
}
}
I have tried running the program, while typing { "msg_id":"ROBOT_BODY_CTRL_CMD", "body_part":2, "action":3 }
however nothing happens. 我尝试输入{ "msg_id":"ROBOT_BODY_CTRL_CMD", "body_part":2, "action":3 }
来运行程序,但是什么也没有发生。 Since I am sending a message, am I acting as a client or the server? 由于我正在发送消息,因此我是作为客户端还是作为服务器?
When you send a message to any ip address on specific socket you are client and when you receive message while listening to any port you are acting as server 当您将消息发送到特定套接字上的任何IP地址时,您就是客户端;当您在侦听任何端口时收到消息时,您将充当服务器
here is Microsoft Official Examples on Synchronous an Asynchronous Socket Programming 这是有关同步套接字编程的Microsoft官方示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.