2 Replies - 1031 Views - Last Post: 26 January 2012 - 11:04 AM Rate Topic: -----

#1 coldzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 09

multithreaded Client Server Unhandled exception at 0x566ad51c

Posted 25 January 2012 - 05:02 PM

i made a multithreaded client server , where the server stays up and running if a client connected to the server the server reads data from a given file line by line and sends it to client when i tested it without using the threading thing for just one client it worked perfectly but when i added the multi-client ability using threading and compiled the code the compiler doesn't find any error but when i run the server side and the client side and let the client connect to the server , the server shows that there is a client connected to it but when it reaches the thread part it crashes and shows this error :-

Unhandled exception at 0x566ad51c (msvcr100d.dll) in task1.exe: 0xC0000005: Access violation reading location 0xfdfdfdfd.

and i checked about it but still don't have any idea how to solve this issue !!??

any help please

The Server Part


#include"ClientServer_SockHeader.h"
#include"ThreadFunction.h"
#include "Server.h"
using namespace std;


Server::Server(int port_num , char *IpAddre , char * f_name)
{
	Ip=IpAddre;
	port=port_num;
	filez=f_name;
	cout<<"server started"<<endl;
	//server_socket(port , filez , Ip);
		///----Socket Structure----///
		
		addrlen = sizeof(addr); /* lenght of the address */

		
}

void Server::server_socket()
{
		///----Start the Winsock-DLL----///
		
		WSAData wsaData;
		WORD DLLVERSION;
		DLLVERSION = MAKEWORD(2,1);


	///----Start the DLL with WSAStartup----///
		answer= WSAStartup(DLLVERSION, &wsaData);



}

void Server::server_listen()
{
	///----Server Connection Part----///
		sConnect = socket(AF_INET , SOCK_STREAM,NULL);
		 /* AF_INET = Address type of internet family sockets for IPv4
		  * SOCK_STREAM = TCP " connection oriented "==> needs connection establishment and connection termination
		  * NULL is so because i dont want to define a protocol for the connection */

		addr.sin_addr.s_addr = inet_addr(Ip);
		/* in this part i assigned an IP for the server */
		addr.sin_family = AF_INET ; 
		/* in this part i set the family type of the server address */
		addr.sin_port = htons(port);
		/* in this part i assigned a port where the data is going to go through */


	///----Server Listening Part----///
		sListen = socket(AF_INET,SOCK_STREAM,NULL);

		bind(sListen,(SOCKADDR*)&addr,sizeof(addr));

		listen(sListen,SOMAXCONN);
		/* SOMAXCONN ==> lets server listen without any limit */
}
		
void Server::server_send()
{
	
	ThreadFunc=new ThreadFunction();
	ThreadFunc->answer=answer;
	ThreadFunc->filez=filez;
	///---- What server should do if connection was found----///
		for(;;)/>
		{
		
	
			cout<<"waiting for an incomming connection"<<endl;
		
			// if connection established then ....... 
				if(sConnect = accept(sListen,(SOCKADDR*)&addr,&addrlen))
					{
						cout<<"A connection was found"<<endl;
						
						/// the Thread section :(/>
						
						/*----------------------- Here is the big deal -----------------------*/
						 
						_beginthread(ThreadFunc->ThreadFunction2,0, (void *)&sConnect);

						/*----------------------- F with the best D like the rest -----------------------*/

						
				
									
					}

		
		}
}

Server::~Server()
{
	cout<<"Server been shutdown"<<endl;
}






The Client Part


#include"ClientServer_SockHeader.h"
#include "Client.h"

using namespace std;

Client::Client(int port_num , char *IpAddre , char * f_name)
{
	Ip=IpAddre;
	port=port_num;
	filez=f_name;
	cout<<"client started"<<endl;
	//server_socket(port , filez , Ip);
		///----Socket Structure----///
		
		addrlen = sizeof(addr); /* lenght of the address */
}

void Client::client_socket()
{
	

	///----Start the Winsock-DLL----///
		WSAData wsaData;
		WORD DLLVERSION;
		DLLVERSION = MAKEWORD(2,1);

	///----Start the DLL with WSAStartup----///
		answer= WSAStartup(DLLVERSION, &wsaData);

}

void Client::client_connect()
{
	///----Client Connection Part----///
		sConnect = socket(AF_INET , SOCK_STREAM,NULL);
		 // AF_INET = Address type of internet family sockets for IPv4
		 ///SOCK_STREAM = TCP " connection oriented "==> needs connection 
		 ///establishment and connection termination
		 // NULL is so because i dont want to define a protocol for the connection 

		addr.sin_addr.s_addr = inet_addr(Ip);
		   //in this part i assigned an IP for 
		   //establishing connection with the serever 

		addr.sin_family = AF_INET ; 
		/* in this part i set the family type of the Client address */
		
		addr.sin_port = htons(port);
		/* in this part i assigned a port where 
		   the data is going to go through */
}

void Client::client_rsv()
{
	loop=true;
	
						while(loop)
							{
								connect(sConnect,(SOCKADDR*) &addr,sizeof(addr));//connection establishment with the server part
									ofstream myfile; //creats a new file to store the outputs inside it
									myfile.open (filez);// name the new file
									do
									{
										
											char message [512]={""};
										/* creats each time an empty array of char 
										to fill it with the recieved lines from server */
											 answer = recv(sConnect,message,sizeof(message),0);
											 /* here the Client recieves a message from the Server */
											 if ( answer > 0 )
												{
													cout<<message<<endl; // the message gets shown to the client here 
													myfile << message<<endl;//writes data to file while the file is opened
												 }
											 else if ( answer==0 )
												 {
													cout<<"Connection closed\n"<<endl;
												  }
											 else
												{
													cout<<"connection lost"<<endl;
													myfile.close();
											     }
										
									} while(answer>0 && myfile.is_open());
									//while there is still data and the file is opened 
									//the operation continues

									closesocket(sConnect); // close the socket if data transmission stops
									WSACleanup();// terminates use of the Winsock 2 DLL (Ws2_32.dll)
									/* if the number of bytes 
									greater than 0 then there is still data to be recieved */
								
									loop=false;
							}
								
						
				
}


Client::~Client(void)
{
	cout<<"connection terminated"<<endl;
}







The Thread Part


#include"ClientServer_SockHeader.h"
#include"ThreadFunction.h"
using namespace std;

ThreadFunction::ThreadFunction()
{
	cout<<"thread started"<<endl;
}

void ThreadFunction::ThreadFunction2(void *Arg)
{
	
    ThreadFunction *Threada = (ThreadFunction *)Arg;
	
	Threada->ThreadFunction_main(Arg);
	
	delete Threada;
   
} 

void ThreadFunction::ThreadFunction_main(void *Arg)
{
	SOCKET sConnect=(*(int *)Arg);
/// to get data from the file targeted and send them to client
	ifstream fname; // file name 
	string file_name=filez; // file name from INI
	string line;//read lines

    fname.open(file_name); /// open the file part

			if(fname.fail()) /// if file didn't exist
				{
					line="file doesn't exist at all";
					cout<<line<<endl;
				}
			else
			{
				while (fname.is_open())
					{
					while ( fname.good() )
					{
						getline (fname,line); // gets data from file line by line
											
						for(int i=0;i<(int)(line.length());i++) /// if the alphabet was uppercase then leave it else turn it into uppercase
							{
								if(! isupper(line[i]))
									{
										line[i]=toupper(line[i]);
															
									}
													
							}
						cout<<"data transmission"<<endl;
						answer = send( sConnect,line.c_str(),strlen(line.c_str()),NULL );
						// sends the data to the connected client
											
						line=""; // clears the line after sending it to get the new line
					}
					fname.close();// closes the file after reading all the lines in it
					answer = shutdown(sConnect, SD_SEND); 
					// closes the connection of the client after
					// the file lines end
											
					if (answer == SOCKET_ERROR) 
						// if an error happend it'll close the connection between 
						//client and server without closing the server & clean all the data
						{
							cout<<"shutdown failed with error: \n"<<WSAGetLastError()<<endl;
							//WSAGetLastError function returns the error status for 
							//the last Windows Sockets operation that failed.

								closesocket(sConnect); // close the socket if data transmission stops
								WSACleanup();// terminates use of the Winsock 2 DLL (Ws2_32.dll)
						}
					}
			}
	

	_endthreadex(0);
}

ThreadFunction::~ThreadFunction()
{
	cout<<"thread destroyed"<<endl;
}





Is This A Good Question/Topic? 0
  • +

Replies To: multithreaded Client Server Unhandled exception at 0x566ad51c

#2 coldzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 09

Re: multithreaded Client Server Unhandled exception at 0x566ad51c

Posted 25 January 2012 - 05:09 PM

Server Side Header


#include"ThreadFunction.h"
#pragma once
class Server
{
public:
	
	
	ThreadFunction *ThreadFunc;
	long answer;
	char *filez;
	Server(int,char *,char *);
	void server_socket();
	void server_listen();
	void server_send();
	~Server(void);

private:
	int addrlen;char *Ip;int port;
	SOCKADDR_IN addr; /* server address */
	SOCKET sListen; /* listen to clients :D/> */
		SOCKET sConnect; /* establish connection with client */
		
};






Client Side Header


#pragma once
class Client
{
public:
	long answer;
	bool loop;

	Client(int,char *,char *);
	void client_socket();
	void client_connect();
	void client_rsv();
	~Client(void);

private:
	int addrlen;
	char *Ip;
	int port;
	char *filez;
	SOCKADDR_IN addr; /* address */
	SOCKET sConnect; /* establish connection with client */
};






Thread Function Header


#pragma once
class ThreadFunction
{
public:
	long answer;
	char * filez;
	ThreadFunction();
     static void ThreadFunction2(void *);
	 void ThreadFunction_main(void *);
	~ThreadFunction();
	//void *ClientC;
	
};



Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1743
  • View blog
  • Posts: 3,360
  • Joined: 30-May 10

Re: multithreaded Client Server Unhandled exception at 0x566ad51c

Posted 26 January 2012 - 11:04 AM

13 ThreadFunction *Threada = (ThreadFunction *)Arg;
14
15 Threada->ThreadFunction_main(Arg);
...
21 void ThreadFunction::ThreadFunction_main(void *Arg)
22 {
23 SOCKET sConnect=(*(int *)Arg);
So what is Arg really pointing at?
You cast it to one thing, then cast it to another.
No wonder it blows up at some point.


006 Client::Client(int port_num , char *IpAddre , char * f_name)
007 {
008 Ip=IpAddre;
009 port=port_num;
010 filez=f_name;
You need to make really sure that these pointers have a decent lifetime. Especially when the memory might be local to another function in another thread.
Make a true copy of the string within the constructor.
Having pointers to outside data is a really bad idea.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1