多次发送http post请求以后会报错10061

//SocketClient socket = new SocketClient("192.168.1.100", 9999);
  //socket.Connect();
  //Send(socket, "POST /UploadScreenCapture.ashx HTTP/1.1\r\n");
  //Send(socket, "Content-Length: " + buffer.Length + "\r\n\r\n");
  //Send(socket,buffer);//buffer中的数据有1M大小

   
  //socket.Close();
  //byte[] recvBuff = Read(socket);
  //string s = Encoding.UTF8.GetString(recvBuff, 0, recvBuff.Length);
void Send(SocketClient client, string s)
  {
  Send(client,Encoding.UTF8.GetBytes(s));
  }

  void Send(SocketClient client, byte[] buffer)
  {
  int len = buffer.Length;
  int res;
  int pos = 0; //下一次发送需要开始的位置:
  while (pos < len)
  {
  res = client.Send(buffer, pos, len);
  if (res <= 0)
  break;
  pos += res;
  }
  }

  byte[] Read(Socket client)
  {
  List<byte> list = new List<byte>();
  byte[] buffer = new byte[1024];
  int count;
  while ((count=client.Recieve(buffer)) > 0)
  {
  list.AddRange(buffer.Take(count));
  }
  return list.ToArray();
  }

这是一段我的程序,用C#写的,不管具体语言了,都是Socket编程,思路都是一样的。
我向服务器发出post请求,把一个1M左右的流传到服务器上,运行第一次没问题,但是在同一个进程中运行第二次,就报错10061。
我确认服务器端没问题,因为服务器端是一个有名的web服务器,而且我用.net中比较健壮的内置类WebClient测试上传多次也没问题,就是自己写的Socket发送总是出错。

作者: cownew   发布时间: 2011-06-15

Send()失败?WSAGetLastError()的值是什么?

作者: VisualEleven   发布时间: 2011-06-15

connect失败,WSAGetLastError()的值是100061
我是通过C#调用VC编写的Com组件,SocketClient类是对Com组件的封装,Com的源码如下:


#include "stdafx.h"
#include "NetSockets.h"

#include <Winsock2.h>
#include <iostream>
#include <connmgr.h>

STDMETHODIMP CNetSockets::Initialize()
{
WSADATA info;
  return WSAStartup(MAKEWORD(2,2), &info);
}

STDMETHODIMP CNetSockets::Cleanup()
{
return WSACleanup();
}

STDMETHODIMP CNetSockets::SendBytes(SOCKET socket, unsigned char * buff, int len, int flags)
{
return send(socket, (char*)buff, len, flags);
}

STDMETHODIMP CNetSockets::Connect(wchar_t* hostname, int port, SOCKET* pSocket)
{
char* pchName = new char[wcslen(hostname) + 1];
wcstombs(pchName, hostname, wcslen(hostname));

hostent *he;

if((he = gethostbyaddr((char *) &pchName, 4, AF_INET)) == 0)
if ((he = gethostbyname(pchName)) == 0) 
{
delete pchName;
return WSAGetLastError();
}

delete pchName;

*pSocket = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr = *((in_addr *)he->h_addr);
memset(&(addr.sin_zero), 0, 8); 

if (connect(*pSocket, (sockaddr *) &addr, sizeof(sockaddr)))
{
return WSAGetLastError();
}

return 0;
}

STDMETHODIMP CNetSockets::ReadBytes(SOCKET socket, unsigned char * buff, int len)
{
return recv(socket, (char*)buff, len, 0);
}

STDMETHODIMP CNetSockets::Close(SOCKET socket)
{
return closesocket(socket);
}

STDMETHODIMP CNetSockets::SetRecieveTimeout(SOCKET socket, int timeout)
{
timeval tv;
tv.tv_sec = timeout;
   
return setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&tv), sizeof(timeval));
}

STDMETHODIMP CNetSockets::SetSendTimeout(SOCKET socket, int timeout)
{
timeval tv;
tv.tv_sec = timeout;
   
return setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&tv), sizeof(timeval));
}

// CNetSockets

操作系统是WinCE7.0,不是PC

作者: cownew   发布时间: 2011-06-15