Home > other >  C # write a send and receive messages with the HTTP response code can't run to validate the rea
C # write a send and receive messages with the HTTP response code can't run to validate the rea

Time:09-21

The first is the HTTP interface test tools online https://getman.cn/AJzpc

Then I wrote the following WINFORM code
 
using System;
using System.Collections.Generic;
Using System.Com ponentModel;
using System.Data;
Using System. Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Using System. Windows. Forms;

Using System.Net;
using System.Threading;
Using System.Net.Sockets;
Using System. Diagnostics;

The namespace myInitialProject
{
Public partial class Form2: Form
{
Thread serverThread;
Thread clientThread;
The Socket serverSocket.


Private byte [] result=new byte [1024].
//Socket clientSocket;
Public Form2 ()
{
InitializeComponent();
}


Public void ServerStart ()
{
ListBoxInfo. Items. Add (" Server Start... ");
String host="127.0.0.1";//Form1 passed IPAddress. Any to define
Int nPort=10000;
ServerSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
ServerSocket. Bind (new IPEndPoint (IPAddress. Parse (the host), nPort));
ServerSocket. Listen (10);
ServerThread=new Thread (ListenClientConnect);//to monitor client
ServerThread. Start ();
}

///& lt; summary>
///to monitor client connection
///& lt;/summary>
Public void ListenClientConnect ()
{
While (true)
{
The Socket clientSocket=serverSocket. The Accept ();
.//clientSocket. Send (Encoding UTF8 GetBytes (" connected server "));
Thread receiveThread=new Thread (ReceiveMessage);
//set to background threads, as the main thread to exit out
ReceiveThread. IsBackground=true;
ReceiveThread. Start (clientSocket);
}
}

///& lt; summary>

///receiving information///& lt;/summary>
///& lt; Param name="clientSocket & gt;"
Public void ReceiveMessage (object clientSocket)
{
The Socket myClientSocket=(Socket) clientSocket;
Byte [] data=https://bbs.csdn.net/topics/new byte [1024].
Int nReceiveNumber;



Byte [] tempData=https://bbs.csdn.net/topics/new byte [1024 * 2];//data from the browser
Int reciverNumber=myClientSocket. The Receive (tempData);
. String tempStr=Encoding UTF8. Get string (tempData, 0, reciverNumber);

String resdata=https://bbs.csdn.net/topics/DateTime.Now.ToString (" yyyy - MM - dd HH: MM: ss. The FFF ") + "server request time:" + 0;
Byte [] body.=Encoding UTF8. GetBytes (resdata);
Byte [] head.=Encoding UTF8. GetBytes (@ "HTTP/1.1 200 OK
The Content - Length: "+ body Length + @"
The content-type: text/plain
Date: "+ string Format (" {0: R}," DateTime. Now) + @ "
Server: Cftea Web Server + resdata ");
StringBuilder sb=new StringBuilder();
Sb. Append (" HTTP/1.1 200 OK \ r \ n ");
Sb. Append (" content-type: text/plain \ r \ n ");
Sb. Append (" \ r \ n ");
Sb. Append (" test data returned by the \ r \ n ");
Byte [] responseData=https://bbs.csdn.net/topics/Encoding.UTF8.GetBytes (sb. ToString ());
MyClientSocket. Send (responseData);
MyClientSocket. Close ();
/* the while (true)
{
Try
{

//receiving data
NReceiveNumber=myClientSocket. The Receive (result);
If (nReceiveNumber==0)
{
return;
}



. String strMessage=Encoding UTF8. Get string (result, 0, nReceiveNumber);
//print out the client sends back the message here
Trace. WriteLine (" receives the returned information: "+ myClientSocket. RemoteEndPoint. The ToString () +" "+ strMessage);
//sent back to the client
//string strWelcome="has been connected to the server";

//get the sequence of bytes of the string
//data=https://bbs.csdn.net/topics/Encoding.UTF8.GetBytes (strWelcome);
//myClientSocket. Send (data, the data Length, SocketFlags. None).
//short connection to send the closed SOCKET
//myClientSocket. Close ();
If (myClientSocket. Connected)
{
//determine whether the client has closed
//this method can not detect some type of connection problems,
Interrupt or//, for example, the network cable to the remote host closed unexpectedly, you must try to send or receive data to detect these types of errors,
If (myClientSocket. Poll (0, SelectMode. SelectRead))
{

{

//if (nRead==0)
{
MyClientSocket. Close ();
MessageBox. Show (" the client closed ");
break;
}
}
}
The else
{
MessageBox. Show (" the client not closed ");
}
}

}
The catch (System. Exception ex)
{
MessageBox. Show (" server listening the Error: "+ ex. Message);
//ban send and upload
//myClientSocket. Shutdown (SocketShutdown. Both);
//if (myClientSocket. Poll (1, SelectMode SelectRead))
//{
//byte [] judgeData=https://bbs.csdn.net/topics/new byte [1];
//int nRead=myClientSocket. The Receive (judgeData, SocketFlags. Peek);
//if (nRead!=0)
//myClientSocket. Close ();
//}

break;
}
} */
}

///& lt; summary>
///to monitor button
///& lt;/summary>
///& lt; Param name="sender" & gt;
///& lt; Param name="e" & gt;
Private void btnListen_Click (object sender, EventArgs e)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related