I am making a winsock client/server char application as suggested by my programming lecturer (because I have finished all his work for now). When I attempt to create a socket it fails. Here is my code:
SimpleCharServer.cppSimpleChatServer.hCode:#include <iostream> #include <winsock2.h> #include <wininet.h> #include <sys/types.h> #include <string> #include "SimpleChatServer.h" using namespace std; const char ipAddress[16] = "127.0.0.1"; const int portNum = 4444; const int MAX_USERS = 10; const int BUFFER_SIZE = 50; const int REQ_WINSOCK_VER = 2; const int QUEUE_SIZE = 5; int main(void) { //Array of 'connectedUser' structures to hold a list of hosts connected //connectedUser currentUsers[MAX_USERS]; //Enough space for 10 users //Show welcome message showWelcomeMsg(); struct sockaddr_in clientIn; struct sockaddr_in serverIn; //Buffer to messages char txtBuffer[BUFFER_SIZE]; WSADATA wsaData; serverIn.sin_family = AF_INET; serverIn.sin_addr.s_addr = htonl(INADDR_ANY); serverIn.sin_port = htons(portNum); SOCKET hSocket; if(WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &wsaData)==0) { //Create a socket if(hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)==0) //Create socket success { cout << "Socket created" << endl; //TEST //Bind the socket to socket address bind(hSocket,(struct sockaddr*)&serverIn,sizeof(serverIn)); //Bind socket success cout << "Socket bound" << endl; //TEST listen(hSocket, QUEUE_SIZE); //Listening cout << "Listenining on socket" << endl; //TEST int addrLen; bool errOccur = false; while(!errOccur) //Infinate loop, see Glenn about it { //Wait for a connection, then accept it accept(hSocket,(struct sockaddr*)&clientIn, &addrLen); //Receive the data that was sent if(recv(hSocket,txtBuffer, sizeof(txtBuffer),0)==0) { //Print the message printMessage(txtBuffer); //Send data back to all clients send(hSocket, txtBuffer, strlen(txtBuffer),0); } } } else { cout << "Error: Unable to create socket" << endl; } } //Close the socket closesocket(hSocket); //Winsock cleanup routine WSACleanup(); cout << "Server shutting down" << endl; return 0; } void printMessage(char *txtBuffer) { cout << txtBuffer << endl; } void showWelcomeMsg() { cout << "===============================================================================" << endl; cout << "= =" << endl; cout << "= SIMPLE CHAT SERVER =" << endl; cout << "= =" << endl; cout << "= BY DANIEL MORGAN =" << endl; cout << "= =" << endl; cout << "===============================================================================" << endl << endl; }Any idea what the problem is?Code:#ifndef SIMPLE_CHAT_SERVER #define SIMPLE_CHAT_SERVER /*typedef struct { char hostAlias[25]; char ipAddress[16]; }connectedUser;*/ //int addUserToList(connectedUser* currentUsers, char *userHostAlias, char *userIpAddress, int userCount); void showWelcomeMsg(); //void connectServer(unsigned int portNum); //void printUserList(connectedUser* currentUsers, int userCount); //Take array of 'connectedUser' structures by reference void printMessage(char *txtBuffer); #endif
p.s. I am using Microsoft Visual C++ Express




Reply With Quote