Archived

This topic is now archived and is closed to further replies.

Why doesn't this code work?

This topic is 5815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  
#include <iostream.h> 

#include "Socket.h" 

#include <windows.h> 

CIOSocket ClientSocket;
bool testreturn;
bool newstuff=true;
char ip[]="192.168.1.7";
#define MAXCLIENTS 100
DWORD threadid;
struct postion{
char postionofship[100];
};
postion info[MAXCLIENTS];
char message[100];
long WINAPI  ReciveFunction(void *vArg)
{
	do{

char messagel[100];
ClientSocket.Receive(messagel,100,NULL,false);
cout<<"Recived Message: ";
cout<<messagel<<endl<<endl;
cout<<"Enter Message"<<endl;
//strcpy(info[i].postionofship,messagel);


	}while(1==1);
return 1;
}
	

void SendFunction()
{	



	testreturn=ClientSocket.Send((void*)message,100,NULL);
	errortest(testreturn);
	


}

void main()
{
	

	testreturn=ClientSocket.Create();
	errortest(testreturn);
	testreturn=ClientSocket.Connect(ip,632);
	errortest(testreturn);
	cout<<"Connected"<<endl;
	CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)ReciveFunction,0,0,&threadid);
	do{
	
	
	cin.getline(message,100,'\n');
	cout<<endl;
	SendFunction();
	}while(1==1);
	int a;
	cin>>a;
}
///Socket.h

#include<winsock2.h>

int call=0;
void errortest(bool testerror)
{
	call++;
	if(testerror==false)
	{
		cout<<"Call: "<<call<<" returned an error"<<endl;
	}
}

class CIOSocket{
private:
	
	struct sockaddr_in addr; 
	WSADATA WSAData;
	WSAPROTOCOL_INFO pinfo;
	public:
		SOCKET Socket;
		bool					Accept( CIOSocket& CSocket, struct sockaddr* addr = NULL, int*addrlen = NULL );//

		void					Attach( SOCKET hSocket );//

		bool					Bind( int port = 0, char * lpszSocketAddress = NULL );//

		bool					Create( int AddressFamily = AF_INET, int Type = SOCK_STREAM, int Protocol = IPPROTO_IP );//

		bool					Connect( char * lpszHostAddress, int nHostPort,int AddressFamily = AF_INET );//

		bool					Close( void );//

		SOCKET				Detach( void );//

		SOCKET				GetSocketHandle( void );//

		bool					Listen( int nBackLog = 5 );//

		bool					Send( const void* lpBuf, int nBufLen, int nFlags = NULL );//

		int					Receive( void* lpBuf, int nBufLen, int nFlags = NULL, bool bAcceptPartial = false );//

		CIOSocket();
		~CIOSocket();
};
SOCKET CIOSocket::Detach(void)
{
	Socket=0;
	return Socket;
}
void CIOSocket::Attach(SOCKET hSocket)
{
	Socket=hSocket;
}
SOCKET CIOSocket::GetSocketHandle(void)
{
	return Socket;
}

CIOSocket::CIOSocket()
{
	WSAStartup( 0x0002, &WSAData );
}
CIOSocket::~CIOSocket()
{
Close();
WSACleanup();
}
bool CIOSocket::Close()
{
	if(!closesocket(Socket))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CIOSocket::Accept(CIOSocket &CSocket,sockaddr*addr,int *addrlen)
{
	if((CSocket.Socket=accept(Socket,addr,addrlen))!=INVALID_SOCKET)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CIOSocket::Send(const void *lpBuf,int nBufLen,int nFlags)
{
	if((send(Socket,(const char *)lpBuf,nBufLen,nFlags))!=SOCKET_ERROR)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int CIOSocket::Receive(void* lpBuf, int nBufLen, int nFlags, bool bAcceptPartial)
{
	int amount=0;
	if(bAcceptPartial==true)
	{
		amount=recv(Socket,(char *)lpBuf,nBufLen,nFlags);
	}
	else
	{
		do
		{
			int amount2=recv(Socket,(char *)lpBuf,nBufLen,nFlags);
			if(amount2==SOCKET_ERROR)
			{
			
			}
			else
			{
			amount+=amount2;
			}
			}while(amount<nBufLen);
	}
	return amount;
}
bool CIOSocket::Create( int AddressFamily, int Type, int Protocol )
{    
	addr.sin_family=AddressFamily; 
	//Socket=socket(AddressFamily,Type,Protocol);

	
	Socket=WSASocket(AddressFamily,Type,Protocol,NULL,0,WSA_FLAG_OVERLAPPED); 
	if(Socket==INVALID_SOCKET)
	{
		return false;
	}
	else
	{
		return true;
	}
}
bool CIOSocket::Bind(int port,char * lpszSocketAddress)
{
	sockaddr_in bindadd;
	bindadd.sin_family=addr.sin_family;
	if(lpszSocketAddress==NULL)
	{
		bindadd.sin_addr.s_addr=::htonl( INADDR_ANY );
	}
	else
	{
		bindadd.sin_addr.s_addr=inet_addr(lpszSocketAddress);         // change here 

	}
	bindadd.sin_port=htons(port); 
	if(!bind(Socket,(struct sockaddr *)&bindadd,sizeof(bindadd)))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CIOSocket::Connect(char *lpszHostAddress,int nHostPort,int AddressFamily)
{
	addr.sin_family=AddressFamily; 
	addr.sin_addr.s_addr=inet_addr(lpszHostAddress); 
	addr.sin_port=htons(nHostPort); 
	if(!connect(Socket,(struct sockaddr *)&addr,sizeof(addr)))
	{
		return true;
	} 
	else
	{
		return false;
	}
}
bool CIOSocket::Listen(int nBackLog)
{
	if(!listen(Socket,nBackLog))
	{
		return true;
	}
	else
	{
		return false;
	}
}
  
Why doesn't this code work!?!? Worked fine when the server was on the same computer as one of the clients, when I moved it over to a different computer, it stoped working!!! It still connects to the server, but nothing ever happens no the clients when you start typing. What should I do? Jesse [tags] Edited by - Magmai Kai Holmlor on January 10, 2002 8:47:28 PM

Share this post


Link to post
Share on other sites