All pastes #474552 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-06 22:01 UTC
rendered paste body

#define PORT 3490 // the port client will be connecting to 

c8 buffer[1000];
bool bufferreadout;
SOCKET s; //Socket handle



//CONNECTTOHOST – Connects to a remote host

bool ConnectToHost(int PortNo, char* IPAddress, bool dns)

{
	//Start up Winsock…
	WSADATA wsadata;
	WSAStartup(0x0202, &wsadata);

	//Did we get the right Winsock version?
	if (wsadata.wVersion != 0x0202)
	{
		WSACleanup(); //Clean up Winsock
		return false;
	}

	//Fill out the information needed to initialize a socket…
	SOCKADDR_IN target; //Socket address information

	target.sin_family = AF_INET; // address family Internet
	target.sin_port = htons (PortNo); //Port to connect on

	if(dns)
	{
		struct hostent *h;
		if(h = gethostbyname(IPAddress))
			target.sin_addr.s_addr = inet_addr (inet_ntoa(*((struct in_addr *)h->h_addr ))); //Target IP
	}
	else
	{
		target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
	}

	s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket

	if (s == INVALID_SOCKET)
	{
		return false; //Couldn’t create the socket
	}

	//Try connecting...
	if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
	{
		return false; //Couldn’t connect
	}
	else
		return true; //Success
}



//CLOSECONNECTION – shuts down the socket and closes any connection on it

void CloseConnection ()

{

	//Close the socket if it exists

	if (s)

		closesocket(s);



	WSACleanup(); //Clean up Winsock

}

int sendmsg(char* message)
{
	stringc msg = message;
	return send(s,msg.c_str(),msg.size(), 0);
}

void readloop()
{
	memset(buffer, 0, sizeof(buffer)); //Clear the buffer
	c8 rec = 0;

	while(rec == 0)
	{
		rec = recv (s, buffer, sizeof(buffer), 0);
		std::cout << buffer << std::endl;
	}

	bufferreadout = 1;

}

void read()
{
	memset(buffer, 0, sizeof(buffer)); //Clear the buffer
	if(recv (s, buffer, sizeof(buffer), 0) > 0){bufferreadout = 1;}//std::cout << buffer << std::endl;}//printf(" 1 ");}else printf(" 0 ");

}