C== chat application

hi. i have been doing a c++ client server chat application with exceptions . but when i run the server code the system breaks. how can i fix it? here is the code.


#include <winsock2.h>
#include <ws2tcpip.h>
#include "iostream"
#include <exception>

using namespace std;
//void main(int argc, char* argv[]){
class server{
public:
SOCKADDR_STORAGE from;

int retval,fromlen,socket_type;
static char servstr[NI_MAXSERV], hoststr[NI_MAXHOST];

SOCKET serverSocket, acceptSocket;
static const int port = 55555;
WSADATA wsaData;
int wsaerr;



void request(){


WORD wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0){
throw 1;
//cout << "The Winsock dll not found!" << endl;

}
else{
cout << "The Winsock dll found!" << endl;
cout << "The status: " << wsaData.szSystemStatus << endl;

WSACleanup();

}
}

void Socketrerval(){
int retval,fromlen,socket_type;
serverSocket = INVALID_SOCKET;
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket== INVALID_SOCKET){
throw 2 ;cout<<"Error at socket(): "<< WSAGetLastError() << endl;
WSACleanup();

}
else {
cout << "socket() is OK!" << endl;
}
}
void sockaddr(){
char servstr[NI_MAXSERV], hoststr[NI_MAXHOST];
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(port);
if (bind(serverSocket, (SOCKADDR*)&service, sizeof(service))== SOCKET_ERROR){
throw 3;
//cout << "bind() failed: " << WSAGetLastError() << endl;
closesocket (serverSocket);
WSACleanup();

}
else{
cout << "bind() is OK!" << endl;
}
}
void listenServer(){
if (listen(serverSocket, 1) == SOCKET_ERROR)
throw 4;
//cout << "listen(): Error listening on socket " << WSAGetLastError()<< endl;
else{
cout << "listen() is OK, I'm waiting for connections..." << endl;
}
}
void sockettype(){
fromlen = sizeof(socket_type);
retval = getsockopt(serverSocket, SOL_SOCKET, SO_TYPE, (char *)&socket_type, &fromlen);
fromlen = sizeof(from);
acceptSocket = accept(serverSocket, (SOCKADDR *)&from, &fromlen);
acceptSocket = accept(serverSocket, NULL,NULL);
if (acceptSocket == INVALID_SOCKET){
throw 5;
//cout << "accept failed: " << WSAGetLastError() << endl;
WSACleanup();

}
}
void socketretval(){
retval = getnameinfo((SOCKADDR *)&from,
fromlen,
hoststr,
NI_MAXHOST,
servstr,
NI_MAXSERV,
NI_NUMERICHOST | NI_NUMERICSERV);
if (retval != 0){
throw 5;
// cout << "getnameinfo failed: " << retval << endl;
WSACleanup();

}
cout << "Accepted connection from host " << hoststr << " and port " << servstr << endl;

cout << "Accepted connection" << endl;
}
void clientconnect(){

while(1){
char receiveBuffer[200] = "";
int byteCount = recv(acceptSocket, receiveBuffer, 200, 0);
if (byteCount < 0){
break;

printf("Client: error %ld.\n", WSAGetLastError());

//return 0;
}

else {
printf("Received data : %s \n", receiveBuffer);

}
}
}
};
void main(){
server e;
//


try{
e.request();
e.Socketrerval();
e.sockaddr();
e.listenServer();
e.sockettype();
}




catch(char* exception){

cout << "The Winsock dll not found!" << endl;


//cout<<"Error at socket(): "<< WSAGetLastError() << endl;
//cout << "bind() failed: " << WSAGetLastError() << endl;
//cout << "listen(): Error listening on socket " << WSAGetLastError()<< endl;
}



//e. Socketrerval();
//try{
// }
//catch(int * exception){
//throw 1; cout<< "Error at socket(): " << WSAGetLastError() << endl;
//throw 2;cout << "Error at socket(): " << WSAGetLastError() << endl;
//}

//e.sockaddr();
//e.listenServer();
//e.sockettype();
//e.socketretval();
//e.clientconnect();




//e.Socket();
/*
try{}
catch(string *error){
throw 2;
cout << "Error at socket(): " << WSAGetLastError() << endl;

*/

system("pause");
WSACleanup();

}
Last edited on
the system breaks

What does this even mean?
Why do you include iostream with the " ". It'd be

#include <iostream>

Also yeah, what do you mean "the system breaks"?
Last edited on
it gives an error message as "Unhandled exception at 0x779e4b32 in Server.exe: Microsoft C++ exception: int at memory location 0x0110f5fc.."
The quickest way to find the problem would be to use a debugger to find out exactly where it's crashing, and what the state of the memory is at that point.

It would be nice to be able to step through the application, but that's not much help if the server crashes as a result of interaction with the client.
is this server code ok?


#include <winsock2.h>
#include <ws2tcpip.h>
#include "iostream"
#include <exception>
using namespace std;

//class Server
//{
//public:

static SOCKADDR_STORAGE from;
static int retval,fromlen,socket_type;
static char servstr[NI_MAXSERV], hoststr[NI_MAXHOST];

static SOCKET serverSocket, acceptSocket;
static const int port = 55555;
static WSADATA wsaData;
static int wsaerr;

void test1()
{
//WSADATA wsaData;
//int wsaerr;
WORD wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0){
throw 1;
//cout << "The Winsock dll not found!" << endl;

}
else{
cout << "The Winsock dll found!" << endl;
cout << "The status: " << wsaData.szSystemStatus << endl;
}

}


void test2()
{
//SOCKET serverSocket, acceptSocket;
//serverSocket = INVALID_SOCKET;
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket== INVALID_SOCKET){
throw 2;//cout << "Error at socket(): " << WSAGetLastError() << endl;
WSACleanup();

}
else {
cout << "socket() is OK!" << endl;
}

}


void test3()
{
//SOCKET serverSocket, acceptSocket;
//int port = 55555;
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(port);
if (bind(serverSocket, (SOCKADDR*)&service, sizeof(service))== SOCKET_ERROR){
throw 3;
//cout << "bind() failed: " << WSAGetLastError() << endl;
closesocket(serverSocket);
WSACleanup();

}
else{
cout << "bind() is OK!" << endl;
}


}




void test4()
{
//SOCKET serverSocket, acceptSocket;
//int retval,fromlen,socket_type;
//SOCKADDR_STORAGE from;
//char servstr[NI_MAXSERV], hoststr[NI_MAXHOST];

if (listen(serverSocket, 1) == SOCKET_ERROR)
throw 4;
//cout << "listen(): Error listening on socket " << WSAGetLastError()<< endl;
else
cout << "listen() is OK, I'm waiting for connections..." << endl;


fromlen = sizeof(socket_type);
retval = getsockopt(serverSocket, SOL_SOCKET, SO_TYPE, (char *)&socket_type, &fromlen);
fromlen = sizeof(from);
acceptSocket = accept(serverSocket, (SOCKADDR *)&from, &fromlen);
//acceptSocket = accept(serverSocket, NULL,NULL);
if (acceptSocket == INVALID_SOCKET){
throw 5;//cout << "accept failed: " << WSAGetLastError() << endl;
WSACleanup();

}
retval = getnameinfo((SOCKADDR *)&from,
fromlen,
hoststr,
NI_MAXHOST,
servstr,
NI_MAXSERV,
NI_NUMERICHOST | NI_NUMERICSERV);
if (retval != 0){
throw 5;
//cout << "getnameinfo failed: " << retval << endl;
WSACleanup();

}
cout << "Accepted connection from host " << hoststr << " and port " << servstr << endl;

cout << "Accepted connection" << endl;


while(1){
char receiveBuffer[200] = "";
int byteCount = recv(acceptSocket, receiveBuffer, 200, 0);
if (byteCount < 0){
break;
throw 6;

// printf("Client: error %ld.\n", WSAGetLastError());

//return 0;
}

else {
printf("Received data : %s \n", receiveBuffer);

}
}


}


void main(int argc, char* argv[]){

/*
SOCKADDR_STORAGE from;
int retval,fromlen,socket_type;
char servstr[NI_MAXSERV], hoststr[NI_MAXHOST];

SOCKET serverSocket, acceptSocket;
int port = 55555;
WSADATA wsaData;
int wsaerr;
*/

try
{
test1();
test2();
test3();
test4();

}
catch(int *exception)
{
cout << "The Winsock dll not found!" << endl;
cout << "Error at socket(): " << WSAGetLastError() << endl;
cout << "bind() failed: " << WSAGetLastError() << endl;
cout << "listen(): Error listening on socket " << WSAGetLastError()<< endl;
cout << "getnameinfo failed: " << retval << endl;
cout << "accept failed: " << WSAGetLastError() << endl;
printf("Client: error %ld.\n", WSAGetLastError());
//cout << "exception occured" << endl;
}








system("pause");
WSACleanup();
}


Topic archived. No new replies allowed.