C#模拟http服务器响应post请求但是没有收到数据
我启动本地服务器时可以正常接收到post过来的数据,上传到阿里云启动外网服务器之后就收不到了
处理接收的代码,业务逻辑已经注释,在接收到数据时立即调用Messagebox.Show()来显示接收到的字符串(别纠结用winform做的……)
private void handleTheRequest(Socket clientSocket)
{
if (clientSocket.Available <= 0)
return;
byte[] buffer = new byte[4096];
int receivedBCount = clientSocket.Receive(buffer); // Receive the request
string strReceived = charEncoder.GetString(buffer, 0, receivedBCount);
MessageBox.Show(strReceived);
}
启动服务器的代码
public bool start(IPAddress ipAddress, int port, int maxNOfCon, string contentPath)
{
if (running) return false; // If it is already running, exit.
try
{
if (serverSocket != null)
serverSocket.Close();
// A tcp/ip socket (ipv4)
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
serverSocket.Listen(maxNOfCon);
serverSocket.ReceiveTimeout = timeout;
serverSocket.SendTimeout = timeout;
running = true;
this.contentPath = contentPath;
}
catch (Exception _e)
{
form.DoLog(_e.ToString());
return false;
}
form.DoLog("Server running at port: " + port);
// Our thread that will listen connection requests
// and create new threads to handle them.
Thread requestListenerT = new Thread(() =>
{
while (running)
{
Socket clientSocket;
try
{
clientSocket = serverSocket.Accept();
// Create new thread to handle the request and continue to listen the socket.
Thread requestHandler = new Thread(() =>
{
clientSocket.ReceiveTimeout = timeout;
clientSocket.SendTimeout = timeout;
try
{
handleTheRequest(clientSocket);
}
catch (Exception _e)
{
try
{
form.DoLog(_e.ToString());
clientSocket.Close();
}
catch
{
}
}
});
requestHandler.Start();
}
catch (Exception _e)
{
form.DoLog("【】" + _e.ToString());
}
}
});
requestListenerT.Start();
return true;
}
附图,第一张是本地服务器接收到的数据内容,第二张是外网服务器接收到的内容
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论