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

I can't see how you're calling these functions and what values the variables have. Post more code that demonstrates how you use these functions and I'll have a look at it.

void setPort(String ^port){
		connectSock();
		P.port=To_string(port);
		args.param="";
		_beginthread(sendHTTP,0,&args);
		args.param="newmap=1";
		_beginthread(sendHTTP,0,&args);

	}

since nobody seems to be able to help me can someone give me an example of what im trying to do?
(beej doesnt help)
or point me somewhere they can help me?

You have threads, and a single unguarded global variable.

You have two functions with very similar names,
- sockConnect
- connectSock

Your 'state' global variable has two flags which seem to mean the same thing.
- bool ic;
- bool inuse;

Can you do what you want (once) as a single thread?

Because at the moment, it's a confused mess of half-cooked ideas all milling around as a bunch of race conditions.

i need to send traffic at random times. the packets are gathered as the user interacts

if i create a new socket when i want to send i get an error saying the socket is in use, and im using threads to send data so if i set so_reuse it wont help.
if i use one socket it gets disconnected for some reason, and when the program closes i get a fin flood.
so how do i do this????

so i rewrote it without the threads, but i still get the rst issue i first got

i send 2 packets and on the 3rd it replies with rst... any thoughts on why?

there is no more code- than what ive posted

void connectSock(){

		psock.sockVer = MAKEWORD(1, 1);	
		psock.ic=false;
		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.sock = socket(psock.srvrData->ai_family, psock.srvrData->ai_socktype, psock.srvrData->ai_protocol);
		if (psock.sock == INVALID_SOCKET) {
		closeSock(psock.sock);
		P.Event="Socket fail... ";
		std::stringstream outo;
			outo << WSAGetLastError();
			P.Event+=outo.str();
		return ;
		} 
		if(connect(psock.sock, psock.srvrData->ai_addr, psock.srvrData->ai_addrlen)<0){
			P.Event="Connect Fail";
			return ;
		}
		psock.ic=true;
	}

	void sendHTTP(string tpost){     
	
		if(!psock.ic)return;
		try{		
			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";
			buffer2+=tpost;
			buffer2+="\n";
	
			if(send(psock.sock, buffer2.c_str(), buffer2.length(), 0)<buffer2.length()){
				P.Event="Send error ";
				string s=(string)strerror(errno);
				P.Event+=(s);
				std::stringstream outo;
			outo << WSAGetLastError();
			P.Event+=outo.str();
				sendHTTP( tpost);
				return;
			}

		}catch(Exception ^ex){
		}
		return;
	}

	void setPort(String ^port){
		connectSock();
		P.port=To_string(port);
		sendHTTP("");
		sendHTTP("newmap=1");

	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.