jjzjj

c - 在函数中传递指向 SOCKADDR_IN 和 SOCKET 的指针

coder 2024-06-22 原文

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

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

为此,我将 socketIdentifier、*sockaddr_in* 和特定的端口号 作为指针传递给 createrServerSocket() 函数,以便创建的 socketIdentifier 和套接字必须可以在线程内访问.

下面是我的代码片段:

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(socketIdentifier ,(struct sockaddr *)&socket , sizeof(socket)) == SOCKET_ERROR)
      {             
         MessageBox(NULL,
                    "Bind Failed",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Else Bind Done//
   MessageBox(NULL,
              "Bind Done",
              "SUCCESS :)",
              MB_ICONINFORMATION);

}

这里是调用函​​数:

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;
}

这些是我得到的错误:

error C2064: term does not evaluate to a function taking 3 arguments
error C2228: left of '.sin_family' must have class/struct/union error C2228: left of '.sin_addr' must have class/struct/union
error C2228: left of '.S_un' must have class/struct/union
error C2228: left of '.S_addr' must have class/struct/union error C2228: left of '.sin_port' must have class/struct/union
error C2070: ''unknown-type'': illegal sizeof operand

线路: if((*socketIdentifier = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)

错误: error C2064: term 不计算为带 3 个参数的函数

最佳答案

你的 bind 调用是完全错误的,它应该是这样的:

bind(*socketIdentifier, (SOCKADDR*) socket, sizeof(*socket))

socket 结构指针的初始化中的优先级也有问题,这会导致您的编译器错误。要么用(*socket).sin_family等,要么用socket->sin_family等。最后一个是结构指针的常用用法。

关于c - 在函数中传递指向 SOCKADDR_IN 和 SOCKET 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16352179/

有关c - 在函数中传递指向 SOCKADDR_IN 和 SOCKET 的指针的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  3. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  4. ruby-on-rails - Rails 3 I18 : translation missing: da. datetime.distance_in_words.about_x_hours - 2

    我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  7. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  8. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  9. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  10. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

随机推荐