#include <WinSock2.h>
#include <WinSock.h>
#include <process.h>
#include <time.h>
#include <iostream>
using namespace std;


class PoPSocket
{
public:
	void Socket()
	{
	const int iReqWinsockVer = 2;   // Minimum winsock version required
	WSAData wsaData;
	if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
	{
		// Check if major version is at least iReqWinsockVer
		if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
		{
			SOCKET hSocket;
			hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			if (hSocket==INVALID_SOCKET)
			{
				// error handling code
				MessageBox(0,"Invail Socket Handler",0,MB_ICONERROR);
				closesocket(hSocket);
			}
			else
			{
				struct sockaddr
				{
					u_short    sa_family;
					char       sa_data[14];
				};
				struct sockaddr_in
				{
					short   sin_family;
					u_short sin_port;
					struct  in_addr sin_addr;
					char    sin_zero[8];
				};
				// Convert a u_short from host to TCP/IP network byte order.
				u_short htons(u_short hostshort);

			}
		}
		else
		{
			// Required version not available
			MessageBox(0,"Required version of WinSoc not available",0,MB_ICONERROR);
		}
			// Cleanup winsock
		if (WSACleanup()!=0)
		{
			// cleanup failed
			MessageBox(0,"Winsoc cleanup failed",0,MB_ICONERROR);
		}
	}
	else
	{
		MessageBox(0,"Socket startup failed!",0,MB_ICONERROR);
	}
	}
};

so i can call it in the main.cpp as

PoPSocket soc;
soc.Socket();

but when am trying to finish the socket its keeping say

Error

Error	1	error C2373: 'htons' : redefinition; different type modifiers	
	2	IntelliSense: linkage specification is incompatible with previous "htons" (declared at line 1760 of "C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\WinSock2.h")

can anyone please help me?!

This is not valid syntax:

u_short htons(u_short hostshort);

I think you want simply:

u_short htons(hostshort);

This is not valid syntax:

u_short htons(u_short hostshort);

I think you want simply:

u_short htons(hostshort);

now it's giving me new errors

Error 1 error C2065: 'hostshort' : undeclared identifier
2 IntelliSense: identifier "hostshort" is undefined

You should listen to the compiler :) 'hostshort' is indeed not declared anywhere in the code you've shown us.

You should listen to the compiler :) 'hostshort' is indeed not declared anywhere in the code you've shown us.

check:http://www.madwizard.org/programming/tutorials/netcpp/4

he using

// Convert a u_short from host to TCP/IP network byte order.
u_short htons(u_short hostshort);

// Convert a u_long from host to TCP/IP network byte order.
u_long htonl(u_long hostlong);

// Convert a u_long from TCP/IP network order to host byte order.
u_short ntohs(u_short netshort);

// Convert a u_long from TCP/IP network order to host byte order.
u_long ntohl(u_long netlong);

and its working !!! idk whats wrong

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.