|
-
Jan 19th, 2007, 04:12 PM
#1
Thread Starter
Fanatic Member
Winsock client/server - server socket error
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.cpp
Code:
#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;
}
SimpleChatServer.h
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
Any idea what the problem is?
p.s. I am using Microsoft Visual C++ Express
Last edited by x-ice; Jan 19th, 2007 at 08:01 PM.
-
Jan 19th, 2007, 06:50 PM
#2
Re: Winsock client/server - server socket error
This line:
Code:
if (hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)==0)
{
// create successful ..
is wrong.
Code:
if ( (hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET)
{
// create succesful...
is correct. In the first case, you're saying that the socket is only successfuly created if socket() returns 0. Actually, socket() returns INVALID_SOCKET if it fails to create a socket, or a handle to the socket otherwise. So the code I've written says "as long as hSocket is not INVALID_SOCKET" then creating the socket succeeded.
Good luck!
PS -- for more information check the msdn page on the functions you're using. For example, here is the page on socket: http://msdn2.microsoft.com/en-us/library/ms740506.aspx
Last edited by sunburnt; Jan 19th, 2007 at 06:55 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jan 19th, 2007, 08:01 PM
#3
Thread Starter
Fanatic Member
Re: Winsock client/server - server socket error
That solves that error.
Thanks.
Last edited by x-ice; Jan 19th, 2007 at 08:06 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|