为什么班上的会员价值总是采取不正当的价值我改变它我如何...... [英] Why Member Value In Class Always Take Defualt Value Althougth I Change It How Do I...

查看:81
本文介绍了为什么班上的会员价值总是采取不正当的价值我改变它我如何......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此类继承自Thread,这是绑定函数

this class inherit from Thread and this is bind function

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

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

IThread *  run;
int main ()
{
   run = new ITCPServer();
   ((ITCPServer *)&run)->Bind();

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



}





这个问题很糟糕,因为socket



this problem suck me and dosen't accept conection due to wrong value of socket

推荐答案

更改

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



to


to

(dynamic_cast<ITCPServer*>(run))->Bind();





理由:



1.不要在C ++中使用C风格的类型转换程序,它们可能无法达到您的期望,特别是在处理具有虚拟方法的类时! dynamic_cast 以及C ++中提供的其他特殊形式的强制转换不仅会更加详细地说明您在执行转换时的意图,而且会在编译时和编译时进行自动完整性检查。运行时!



2.您将类型转换应用于运行的地址,该地址已经是指针。所以指针 run 被解释为一个类实例,根本就没有意义!请注意,如果您使用 dynamic_cast 开始,编译器会指出您的错误...



固定演员类型从 itcpserver ITCPServer * [/ edit]



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]


这篇关于为什么班上的会员价值总是采取不正当的价值我改变它我如何......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆