winsock class

class winsock error

Page 1 of 1

5 Replies - 1969 Views - Last Post: 26 January 2010 - 12:23 PM Rate Topic: -----

#1 herki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 10

winsock class

Posted 26 January 2010 - 06:41 AM

hi i am trying to make winsock class. But i get some errors form program 10014 and 10093.

here is main cpp code

 #include "class_server.h"
class_server::class_server()
{
	this->setIp();
	this->setPort();
	this->setMax_Clients();
	this->setDeafault_buflen();
}

void class_server::setIp()
{
	strcpy(ip, "127.0.0.1");
}

void class_server::setPort()
{
	strcpy(port, "27015");
}

void class_server::setMax_Clients()
{
	this->max_clients = 10;
}

void class_server::setDeafault_buflen()
{
	this->default_buflen = 512;
}

void class_server::setRecvbuflen()
{
	this->recvbuflen = this->default_buflen;
}

char *class_server::getIp()
{
	return this->ip;
}
int class_server::connect()
{
	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	
	if(iResult != 0)
	{
		printf("WSAStartup failed: %d\n", iResult);
		return 1;
	}

	struct addrinfo *result = NULL, *ptr = NULL, hints;

	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	iResult = getaddrinfo(NULL, this->port, &hints, &result);
	if(iResult != 0)
	{
		printf("getaddrinfo failed: %d\n", iResult);
		WSACleanup();
		return 1;
	}

	SOCKET ListenSocket = INVALID_SOCKET;

	ListenSocket = socket(result->ai_family,result->ai_socktype, result->ai_protocol); 

	if(ListenSocket == INVALID_SOCKET)
	{
		printf("Error at socket: %ld\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}

	iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
	if(iResult == SOCKET_ERROR)
	{
		printf("bind failed: %d\n", WSAGetLastError());
		freeaddrinfo(result);
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	if(listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
	{
		printf("Error at bind(): %ld\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	//SOCKET ClientSocket;

	this->ClientSocket = INVALID_SOCKET;

	this->ClientSocket = accept(ListenSocket, NULL, NULL);
	if(this->ClientSocket == INVALID_SOCKET)
	{
		printf("accept failed: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	return iResult;
}

int class_server::recive()
{
	iResult = recv(this->ClientSocket, recvbuf, recvbuflen, 0);
	if(iResult > 0)
		{
			printf("Bytes recived: %d\n", iResult);
			iSendResult =	send(this->ClientSocket, recvbuf, iResult, 0);
			if(iSendResult == SOCKET_ERROR)
			{
				printf("send failed: %d\n", WSAGetLastError());
				closesocket(this->ClientSocket);
				WSACleanup();
				return 1;
			}

			printf("Bytes sent: %d\n", iSendResult);
					printf("Bytes sent: %d\n", iSendResult);
		}
		else if(iResult == 0)
			printf("Connection closed...\n");
		else
		{
			printf("recv failed: %d\n", WSAGetLastError());
			closesocket(this->ClientSocket);
			WSACleanup();
			return 1;
		}
}

int *class_server::disconect()
{
	iResult = shutdown(ClientSocket, SD_SEND);
	if(iResult == SOCKET_ERROR)
	{
		printf("shutdown failed: %d\n", WSAGetLastError());
		closesocket(ClientSocket);
		WSACleanup();
		return (int*)1;
	}

	closesocket(ClientSocket);
	WSACleanup();
}




and here is class header

 //#include "class_server.cpp"

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#pragma comment(lib, "ws2_32")

class class_server
{
private:
	int iResult;
	int iSendResult;
	int iRecive;
	int recvbuflen;
	int count;
	int max_clients;
	int default_buflen;

	char recvbuf[512];

	char *ip;
	char *port;

	SOCKET ClientSocket;
	WSADATA wsaData;
	

public:
	//constructor
	class_server();
	//set
	void setIp();
	void setPort();
	void setMax_Clients();
	void setDeafault_buflen();
	void setRecvbuflen();
	
	//get
	char *getIp();
	int connect();
	int recive();
	int fSend();
	int *disconect();
};



i have working client side but can t get data sent to server and receive there.

This post has been edited by herki: 26 January 2010 - 07:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: winsock class

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: winsock class

Posted 26 January 2010 - 06:45 AM

Look! A list of Winsock errors and their causes!
Was This Post Helpful? 0
  • +
  • -

#3 herki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 10

Re: winsock class

Posted 26 January 2010 - 06:51 AM

View PostJackOfAllTrades, on 26 Jan, 2010 - 05:45 AM, said:




ok i checked those error but i don t know how to solve it. i did try, but noe luck. is there any oop version of winsck code examples. i like to read them and use it if i get good ideas there.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: winsock class

Posted 26 January 2010 - 06:58 AM

Before you go diving into Winsock, you should have the basics of C and C++ coding down. You do not.

this->ip = "127.0.0.1";


ip is a char pointer to which you've not assigned any memory, which means it could point anywhere in memory. You also can't assign a C-style string like this; ever hear of strcpy?

Baby steps.
Was This Post Helpful? 0
  • +
  • -

#5 herki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 10

Re: winsock class

Posted 26 January 2010 - 07:05 AM

yeah i am little rusty haven t written long time in c++. But i will learn from my mistakes. And i always learn by mistakes and help i ask because i may read books and to exercises but it s not for me. And code runs program will work but my main question is why my WSAStartup isn t working when i want to send or receive data.
Was This Post Helpful? 0
  • +
  • -

#6 herki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 10

Re: winsock class

Posted 26 January 2010 - 12:23 PM

ok i did get it work and made some improvements can you check my code and suggest me what to improve.

#include "class_server.h"
class_server::class_server()
{
	this->setClients();
	this->setIp();
	this->setPort();
	this->setMax_Clients();
	this->setDeafault_buflen();
	this->setRecvbuflen();
}

void class_server::setClients()
{
	clients = 0;
}

void class_server::setIp()
{
	strcpy_s(ip, "127.0.0.1");
}

void class_server::setPort()
{
	strcpy_s(port, "27015");
}

void class_server::setMax_Clients()
{
	this->max_clients = 10;
}

void class_server::setDeafault_buflen()
{
	this->default_buflen = 512;
}

void class_server::setRecvbuflen()
{
	this->recvbuflen = this->default_buflen;
}

char *class_server::getClientIp()
{
	return this->ClientIp;
}

int class_server::getClients()
{
	return clients;
}

char *class_server::getIp()
{
	return this->ip;
}
int class_server::connect()
{
	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

	if(iResult != 0)
	{
		printf("WSAStartup failed: %d\n", iResult);
		return 1;
	}
	struct addrinfo *result = NULL, *ptr = NULL, hints;

	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	iResult = getaddrinfo(NULL, port, &hints, &result);
	if(iResult != 0)
	{
		printf("getaddrinfo failed: %d\n", iResult);
		WSACleanup();
		return 1;
	}

	//SOCKET ListenSocket = INVALID_SOCKET;

	ListenSocket = socket(result->ai_family,result->ai_socktype, result->ai_protocol); 

	if(ListenSocket == INVALID_SOCKET)
	{
		printf("Error at socket: %ld\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}

	iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
	if(iResult == SOCKET_ERROR)
	{
		printf("bind failed: %d\n", WSAGetLastError());
		freeaddrinfo(result);
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	if(listen(ListenSocket, max_clients) == SOCKET_ERROR)
	{
		printf("Error at bind(): %ld\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	//SOCKET ClientSocket;

	
	clients++;
	ClientSocket[clients] = accept(ListenSocket, NULL, NULL);
	if(ClientSocket[clients] == INVALID_SOCKET)
	{
		printf("accept failed: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}
	return 1;
}

int class_server::recive()
{
	MyPacket packet;
	iResult = recv(ClientSocket[clients], (char*)&packet, recvbuflen, 0);
	if(iResult > 0)
	{
			printf("Bytes recived: %d\n", iResult);
			printf("text recived: %s\n", packet.mystring);
	}
	else if(iResult == 0)
		printf("Connection closed...\n");
	else
	{
		printf("recv failed: %d\n", WSAGetLastError());
		closesocket(ClientSocket[clients]);
		WSACleanup();
		return 1;
	}
	return 1;
}

int class_server::fSend()
{
	MyPacket packet; 
	strcpy_s(packet.mystring, "this is a test");
	iSendResult = send(ClientSocket[clients], (char*)&packet, sizeof(iResult), 0);
	if(iSendResult > 0)
	{
		printf("Bytes sent: %d\n", iSendResult);
	}
	else if(iResult == 0)
		printf("Connection closed...\n");
	else
	{
		printf("send failed: %d\n", WSAGetLastError());
		closesocket(ClientSocket[clients]);
		WSACleanup();
		return 1;
	}
	return 1;
}

int *class_server::disconect()
{
	iResult = shutdown(ClientSocket[clients], SD_SEND);
	if(iResult == SOCKET_ERROR)
	{
		printf("shutdown failed: %d\n", WSAGetLastError());
		closesocket(ClientSocket[clients]);
		WSACleanup();
		return (int*)1;
	}

	closesocket(ClientSocket[clients]);
	WSACleanup();
	return (int*)1;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1