Hey, I wrote some code to send http (see below) and i get a connection error 10056 which means its already connected. However on wireshark i see rst packets from the server, and it doesnt send a packet...
In my program i call connect sock then sendhttp. i only call closesock when the program is exiting.
i know theres something about how you cant reuse a socket after its been closed, is that whats going on? Should i call connectsock instead of sockconnect on socket close?
code:
struct SockInfo{
WORD sockVer;
WSADATA wsaData;
SOCKET mysock;
struct addrinfo hints, *srvrData, *p;
bool ic;
bool inuse;
int errors;
};
SockInfo psock;
void closeSock(){
try{
shutdown(psock.mysock, 2);
closesocket(psock.mysock);
WSACleanup();
psock.ic=false;
}catch(char *e){}
}
void sockConnect(){
int sts=0;
if(connect(psock.mysock, psock.srvrData->ai_addr, psock.srvrData->ai_addrlen)<0){
sts=WSAGetLastError();
if(sts==10056){
psock.ic=true;
return;
}
P.Event="Connect Fail";
std::stringstream outo;
outo << sts;
P.Event+=outo.str();
psock.ic= false;
return;
}
psock.ic=true;
}
void connectSock(){
psock.sockVer = MAKEWORD(1, 1);
WSAStartup(psock.sockVer, &psock.wsaData);
char url[512];
sprintf(url, P.httpUrl.c_str());
memset(&psock.hints, 0, sizeof psock.hints);
psock.hints.ai_family = AF_UNSPEC;
psock.hints.ai_socktype = SOCK_STREAM;
getaddrinfo(url, "80", &psock.hints, &psock.srvrData);
psock.mysock = socket(AF_INET, SOCK_STREAM, 0);
if (psock.mysock == INVALID_SOCKET) {
WSACleanup();
P.Event="Socket fail... ";
return ;
}
psock.errors=0;
psock.inuse=false;
psock.ic=false;
return ;
}
void quitchecking(){
P.quit=true;
closeSock();
}
static void sockRecv(void *param){
char rcv[10];
if(recv(psock.mysock, rcv, 10,0)<10)psock.ic=false;
}
static void sendHTTP(void *param){
threadPass *para=(threadPass *)param;
while(psock.inuse){Sleep(100);
P.Event="IN use";}
psock.inuse=true;
HANDLE rcvTd=(HANDLE)_beginthread(sockRecv, 0,NULL);
if(WaitForSingleObject(rcvTd, 300)==WAIT_TIMEOUT){
psock.ic=false;
}
if(!psock.ic)sockConnect();
if(!psock.ic){
//P.Event="Unable to connect to stats server";
return;
}
P.Event="Send";
try{
string tpost=para->param;//
if(tpost.length()!=0)tpost+="&";
if(P.post.length()!=0)tpost+=P.post;
if(tpost.length()!=0)tpost+="&";
tpost+="port="+P.port;
string buffer2="POST "+P.path+" HTTP/1.0\r\nHost: "+P.httpUrl+"\r\nUser-Agent: Mozilla/4.0\r\nContent-Length: ";
std::stringstream outo;
outo << tpost.length();
buffer2+= outo.str();
buffer2+="\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"+tpost;
char buffer[4096];
sprintf_s(buffer, buffer2.c_str());
if(send(psock.mysock, buffer, sizeof(buffer), 0)<sizeof(buffer)){
P.Event="Send error ";
string s=(string)strerror(errno);
P.Event+=(s);
_beginthread(sendHTTP, 0, param);
return;
}
//closeSock();
}catch(Exception ^ex){
}
psock.inuse=false;
}
sorry for the length. Ive been jabbing at this for like 2 weeks. I give up.
this is on windows