将指针传递给函数中的SOCKADDR_IN和SOCKET [英] Passing pointer to SOCKADDR_IN and SOCKET in a function

查看:142
本文介绍了将指针传递给函数中的SOCKADDR_IN和SOCKET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数createServerSocket()。多个线程可以访问此函数来创建它们的套接字。



我希望每个线程传递三个参数,一个* socketIdentifier,sockaddr_in *和特定端口号到createrServerSocket ()函数,以便每个线程都有一个唯一的套接字。



为此,我将* socketIdentifier,sockaddr_in *和特定的端口号传递给createrServerSocket()函数为指针使得socketIdentifier和socket创建必须在线程内可访问。



下面是我的代码片段:



I have a function createServerSocket(). This function can be accessed by multiple threads for creating their sockets.

I want each thread to pass three arguments, a *socketIdentifier, sockaddr_in* and specific port number to createrServerSocket() function, so that each thread has a unique socket.

For this, I am passing *socketIdentifier, sockaddr_in* and specific port number to createrServerSocket() function as pointers so that socketIdentifier and socket created must be accessable inside thread.

Below is my code snippet:

VOID createServerSocket(SOCKADDR_IN *socket, SOCKET *socketIdentifier, int PORT)
{

   //Socket Binding//
   WSADATA wsa; 

   //Initialise winsock//
   if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
      {
         exit(EXIT_FAILURE);
      }

   //Create a socket//
   if((*socketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
      {                 
         MessageBox(NULL,
                    "Socket not Created",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Socket Created//

   //Prepare the sockaddr_in structure//
   *socket.sin_family = AF_INET;
   *socket.sin_addr.s_addr = INADDR_ANY;
   *socket.sin_port = htons( PORT );

   //Bind//
   if( bind(AH_glb_socketIdentifier ,(struct sockaddr *)&serverSocket , sizeof(serverSocket)) == SOCKET_ERROR)
      {             
         MessageBox(NULL,
                    "Bind Failed",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Else Bind Done//
   MessageBox(NULL,
              "Bind Done",
              "SUCCESS :)",
              MB_ICONINFORMATION);

}



这是调用函数:




Here is the calling function:

DWORD WINAPI threadProc(LPVOID param)
{
    SOCKADDR_IN socket;
    SOCKET socketIdentifier;
    createServerSocket(*socket,*socketIdentifier,8888); //Create a socket with a socketIdentifier and bind()ed to PORT#8888.
        //Do something at this socket
        Return TRUE;
}



这些是我得到的错误:



错误C2064:术语不评估为带有3个参数的函数

错误C2228:'.sin_family''的左边必须有class / struct / union错误C2228:''.sin_addr''的左边必须有类/ struct / union

错误C2228:''。S_un''左边必须有class / struct / union

错误C2228:''。S_addr''左边必须有类/结构/联合错误C2228:''.sin_port''左边必须有class / struct / union

错误C2070:''''unknown-type'''':非法sizeof操作数

推荐答案

首先,你的代码有很多systax错误我不知道你有没有复制/粘贴该代码。它应该返回TRUE而不是返回TRUE(注意R)。



现在让我告诉你一些关于指针的信息。

SOCKADDR_IN socket ;

SOCKET socketIdentifier;


是正常的变量和

& socket

& socketIdentifier


是指针。



* socket

* socketIdentifier


是值。

现在第一个出错是你的参数是指针和你传递的值。

调用代码应该是,

First of all,your code has lot of systax error i dont known you have copy/paste that code or not.It should be return TRUE and not Return TRUE (note "R").

Now let me tell you some this about pointers.
SOCKADDR_IN socket;
SOCKET socketIdentifier;

are normal variables and
&socket
&socketIdentifier

are pointers.
and
*socket
*socketIdentifier

are values.
Now first mistak is that you parameters are pointers and your passing Values.
calling code should be like,
SOCKADDR_IN Passingsocket;//I have change your variable name from socket to passing socket because...
//SOCKET PASCAL FAR socket (
//                 __in int af,
//                 __in int type,
//                 __in int protocol);
//name socket already use by windows for function name..:-) 
SOCKET socketIdentifier;
createServerSocket(&Passingsocket,&socketIdentifier,8888); //Create a socket with a socketIdentifier and bind()ed to PORT#8888.
//Do something at this socket
return TRUE;





此调用工作正常,

现在你的函数体不应该包含任何名称为socket的变量

所以你的函数体可能是这样的,





This calling work fine,
Now your functions body should not containa any variable with name "socket"
So your function body may be like,

VOID createServerSocket(SOCKADDR_IN *Passingsocket, SOCKET *socketIdentifier, int PORT)
{
 
   //Socket Binding//
   WSADATA wsa; 
 
   //Initialise winsock//
   if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
      {
         exit(EXIT_FAILURE);
      }
 
   //Create a socket//
   if((*socketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)//Name of variable and functions clashing..
      {                 
         MessageBox(NULL,
                    "Socket not Created",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Socket Created//

   //Prepare the sockaddr_in structure//
   Passingsocket->sin_family = AF_INET;
   Passingsocket->sin_addr.s_addr = INADDR_ANY;
   Passingsocket->sin_port = htons( PORT );
 
   //Bind//
   if( bind(AH_glb_socketIdentifier ,(struct sockaddr *)&serverSocket  , sizeof(serverSocket )) == SOCKET_ERROR)
      {             
         MessageBox(NULL,
                    "Bind Failed",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Else Bind Done//
   MessageBox(NULL,
              "Bind Done",
              "SUCCESS :)",
              MB_ICONINFORMATION);
 
}





和乖乖这会起作用......



and surly this will work...


这篇关于将指针传递给函数中的SOCKADDR_IN和SOCKET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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