[C++] Problem accept Socket?

05/23/2014 22:22 reload!#1
Hi guys, i've a problem with socket when the sock will accept the connection :
Code:
//server.cpp
#ifndef Server_H
#define Server_H

#include <winsock.h>

#pragma comment (lib, "wsock32.lib")
namespace Server
{
	class Server_s
	{
	public:
		WSAData wsa;
		WORD word;
		SOCKET Connect,Listen;
	    sockaddr_in sock;
		int Start_Server();
		int port;
		char* ip;
		Server_s()
		{
			ip = "127.0.0.1";
			port=80;
			word = MAKEWORD(2,1);
			Connect=socket(2,1,0);
		    Listen=Connect;
			sock.sin_addr.s_addr=inet_addr(ip),sock.sin_family=2,sock.sin_port=htons(port);
		}
	};
}
#endif
Server.cpp :
Code:
#include "Server.h"
#include <iostream>
namespace Server
{
	int Server_s::Start_Server()
	{
		WSAStartup(word,&wsa);
		bind(Listen,(sockaddr*)&sock,sizeof(sock));
		int liste_start=listen(Listen,1);
		int size =sizeof(sock);
		while(liste_start==0){
		Connect=accept(Listen,(sockaddr*)&sock,&size);
		std::cout<<"Start() - > Recv Connection : "<<ip<<" "<<port<<std::endl;
		std::string session_in = "Welcome!";
		send(Connect,session_in.c_str(),session_in.size(),0);}
		return 0;
	}
}
Main :
Code:
#include "Server.h"
#include <iostream>
#define PORT 4003
#define s_c std::cout
#define s_e std::endl
int main()
{
	Server::Server_s *s_Start;
	s_Start=new Server::Server_s;
	system("title Server");
	s_c<<"Welcome to Server"<<s_e;
	
	s_Start->Start_Server();

	WSACleanup();
	getchar();
}
When i start the software and i try to request connection from 127.0.0.1:80, the Server not accept the connection << Can you help me? Thanks
05/24/2014 09:29 Actidnoide#2
Urgh, your code is quite hard to understand.

Code:
bind(Listen,(sockaddr*)&sock,sizeof(sock));
You should definitely check the return value of bind() so you can determine wether the port has been bound or not.
05/24/2014 12:36 reload!#3
I think that problem is listen because if i write a condition : while(listen_start==0),server not start but if bind(...);
listen(Listen,1)
if(Connect = accept(Liste...) {cout<<"Recv"<<endl{
But the connection result alway accept :|
05/24/2014 13:02 Actidnoide#4
Code:
while(listen_start==0)
This condition is useless because listen_start will never change (in your case)

Quote:
Code:
if(Connect = accept(Liste...) {cout<<"Ricevuta"<<endl{
But the connection result alway accept :|
Actually no, accept() will never return zero. You should check for "accept(...) != INVALID_SOCKET" take a look at the [Only registered and activated users can see links. Click Here To Register...].
05/24/2014 14:57 reload!#5
But this problem of accept() there is only if i start it with class,while i put this code in the main it's work :
Code:
//main
#include <iostream>
#include <Winsock2.h>
#pragma comment(lib,"wsock32.lib)
int main()
{
WSAData wsa;
WORD word = MAKEWORD(2,1);
WSAStartup(word,&wsa);
SOCKET Connect,Listen;
Connect=socket(2,1,0),Listen=Connect;
sockaddr_in sock;
sock.sin_addr.s_addr=inet_addr("127.0.0.1"),sock.sin_port=htons(80),sock.sin_family=2;
int size = sizeof(sock);
bind(Listen,(sockaddr*)&sock,sizeof(sok));
listen(Listen,1);
if(Connect=accept(Listen,(sockaddr*)&sock,&size)){
std::cout<<"Recv"<<std::endl;}
WSACleanup();
getchar();
}
Edit: Thanks all guys :) i declase the variables in the Server.cpp and no in the Server.h ;)