Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 09:59

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C++] Problem accept Socket?

Discussion on [C++] Problem accept Socket? within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2014
Posts: 28
Received Thanks: 0
[C++] Problem accept Socket?

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
reload! is offline  
Old 05/24/2014, 09:29   #2
 
elite*gold: 1391
Join Date: Apr 2014
Posts: 77
Received Thanks: 14
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.
Actidnoide is offline  
Old 05/24/2014, 12:36   #3
 
elite*gold: 0
Join Date: Apr 2014
Posts: 28
Received Thanks: 0
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 :|
reload! is offline  
Old 05/24/2014, 13:02   #4
 
elite*gold: 1391
Join Date: Apr 2014
Posts: 77
Received Thanks: 14
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 .
Actidnoide is offline  
Old 05/24/2014, 14:57   #5
 
elite*gold: 0
Join Date: Apr 2014
Posts: 28
Received Thanks: 0
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
reload! is offline  
Reply


Similar Threads Similar Threads
socket_read() [function.socket-read]: unable to read from socket [0] - Problem
03/10/2012 - Web Development - 0 Replies
Thema gelöst ; )
Socket Problem
01/20/2012 - CO2 Private Server - 2 Replies
Fixed lol, well this can be closed.
[iBot] Problem auto accept ?
01/01/2012 - Silkroad Online - 5 Replies
Hey guys i need your help :D can i auto accept party with the ibot only for my chars ? Thx =)
the auto accept problem
04/06/2010 - Silkroad Online - 8 Replies
hello elitepvpers i'm farming my char right now, my friend does the botting job. i'm just standing. in the nights, I must have auto accept the ress and party.. or some way of accepting this ress\party while i'm afk. i must know a way to getting this ress\ party when i'm afk. Please guys...help me to find the solution. very importent to me. thanks...
(CSRO)auto accept ress problem!
03/08/2010 - Silkroad Online - 2 Replies
hey all i need a tool who can accept ress i tryed autoenter but not working. so any 1 can help me? i know that adrenaline loader haved a auto accept ress but that loader is outdated.



All times are GMT +1. The time now is 09:59.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.