Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this class inherit from Thread and this is bind function
C#
int ITCPServer::Bind()
{
    if(Socket>0)
        return 4;
   Socket = socket(PF_INET,SOCK_STREAM,0);  // change value of socket
   //printf("%i",Socket);
//mm = 9;
   server.sin_addr.s_addr = INADDR_ANY;
   server.sin_family = PF_INET;
   server.sin_port  = htons(LTSC_Port);
   if(bind(Socket,(struct sockaddr*)&server,sizeof(server))!=0)
   {
       printf("%s\n","failed");

       return -1;
   }
  listen(Socket,6000);
    printf("%s\n","DSDSD");
    return Socket;
}

and this is listening function calling from Thread function
C#
int ITCPServer::Startlisten()
{
//  Bind();
    //ITCPClient c;
    int s = -1;
    char * bbuf = (char*)malloc(5);
    bzero(&bbuf,sizeof(bbuf));
    bzero(&client,sizeof(client));
    printf("%i \n";,Socket); //Socket always take default value  dosen't change from bind  
    while(true)
    {
    if( (s = accept(Socket,(struct sockaddr*)&client,(unsigned int*)sizeof(client)))>-1)
    {

   // = new ITCPClient();
       printf("so %i  \n",s);
   recv(s,bbuf,sizeof(bbuf),0);
   read(s,bbuf,sizeof(bbuf));
   printf("%s",bbuf);
     // c.Set_Client(s);
     //c.Write("hi");
      printf("%s",(char*)client.sin_addr.s_addr);
   }
    else
    {
   //   sleep(0);
    }
    }
    return 0;
}

and this is main function
C#
IThread *  run;
int main ()
{
   run = new ITCPServer();
   ((ITCPServer *)&run)->Bind();

   run->start();
   run->join();
   //pid_t p = fork();



}


this problem suck me and dosen't accept conection due to wrong value of socket
Posted
Comments
[no name] 5-Aug-14 13:14pm    
What is the return code for listen()?
Mahmoud_Gamal 5-Aug-14 15:35pm    
this is reserved function
i talk about Socket dosent change in cooment
[no name] 5-Aug-14 17:01pm    
The Socket variable is just a handle - like a file descriptor. You set it's value wjem you call socket(). bind() and listen() won't change the handle value.

You will get a new socket each time accept() returns - but again, your Socket variable won't change.

You are not checking the call to listen() for an error. How do you know the socket is really listening and able to accept connections?
Mahmoud_Gamal 6-Aug-14 2:53am    
i try to print Socket variable after Socket = socket(PF_INET,SOCK_STREAM,0);
it take a specific value
but when i use it before accept
accept(Socket,(struct sockaddr*)&client,(unsigned int*)sizeof(client)))
Socket always take default value here of constructor
why this occur i call first Bind() function in main function then call start listen

1 solution

Change
C++
((ITCPServer *)&run)->Bind();

to
C++
(dynamic_cast<ITCPServer*>(run))->Bind();


Reasons:

1. Don't use C-style typecasts in C++ programs, they may not do what you expect them to, especially when dealing with classes that have virtual methods! dynamic_cast and the other specialized forms of casting available in C++ will not only be more verbose on your intentions on performing the casting, and will do automatic sanity checks at both compile-time and run-time!

2. You applied the type cast to the address of run, which already is a pointer. So the pointer run was interpreted as a class instance, which makes no sense at all! Note that if you had used dynamic_cast to start with, the compiler would have pointed out your error...

[edit] fixed cast type from itcpserver to ITCPServer* [/edit]
 
Share this answer
 
v3
Comments
Mahmoud_Gamal 6-Aug-14 5:32am    
(dynamic_cast<itcpserver*>(run))->Bind(); doesn't run program couldn't be run
Stefan_Lang 6-Aug-14 6:07am    
Sorry, of course I meant dynamic_cast<ITCPServer*>, not dynamic_cast<itcpserver> - I fixed the solution.

(apparently the type was misinterpreted as a HTML tag by the editor :-( )
Mahmoud_Gamal 6-Aug-14 6:46am    
I know i write it like this but this is not problem when i change to dynamic_cast
the server dosen't listen when i telnet on it
((ITCPServer *)&run)->Bind();
// (dynamic_cast<itcpserver*>(run))->Bind();
if i reverse comment the server dosen't fire listen thread
i really heat that
Stefan_Lang 6-Aug-14 7:04am    
The former is entirely wrong. It interprets a pointer and the memory block following the memory it is stored in as an object.

The latter should work, provided the remainder of your program does. Of course, there may still be errors elsewhere.
Mahmoud_Gamal 6-Aug-14 7:11am    
OK really THANKS FOR YOUR TIME :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900