c - windows下绑定(bind)C语言

标签 c sockets bind

我在 Windows(C 语言)上使用绑定(bind)方法时遇到问题。出色地。我的代码应该执行以下操作:

  1. 从用户处获取 2 个地址:一个必须是本地计算机的 IP 地址,另一个必须是远程地址(例如 Google 地址)。
  2. 我正在编写的应用程序将绑定(bind)到本地地址
  3. 应用程序将连接到第二个地址。

我从互联网上得到了一些代码。你可以看到我在评论中查阅过的所有来源。

This application will only accepts a string from the client (which specifies the local
address which the application will bind), then, the app will connect to a remote service
google, youtube or whatever. The main purpose that this app serves is to know if, when
 binded to a local address, the operative system (in this case Windows Based system) gives priority to:
- SA/DA rule
- Forwarding table




SOURCES:

http://stackoverflow.com/questions/2065495/using-a-specific-network-interface-for-a-socket-in-windows
http://stackoverflow.com/questions/2605182/when-binding-a-client-tcp-socket-to-a-specific-local-port-with-winsock-so-reuse
http://www.delta-search.com/?q=error+on+binding+to+local+address%2Cwindows&s=web&as=0&rlz=0&babsrc=HP_ss
http://social.msdn.microsoft.com/Forums/zh/vcgeneral/thread/763a00ab-0e7f-44c6-9794-840fc6cb2e07
http://www.delta-search.com/?q=add+ws2_32.lib+visual+studio+2010&babsrc=HP_ss&s=web&rlz=0&as=3&ac=0%2C331
http://stackoverflow.com/questions/5220309/why-am-i-getting-linker-errors-for-ws2-32-dll-in-my-c-program
http://msdn.microsoft.com/en-US/windows/desktop/aa904949
http://msdn.microsoft.com/en-us/library/windows/desktop/ms737550(v=vs.85).aspx



#pragma once
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <Ws2tcpip.h>
#include <stdio.h>


void Abort(char *msg);

void main(int argc,char*argv[]){

    int ret, fd;
    struct sockaddr_in sa_dst;
    struct sockaddr_in sa_loc;

    if(argc < 3)
        Abort("Syntax: SourceIpAddress(to bind) DestinationIpAddress(to connect)");


    fd = socket(AF_INET, SOCK_STREAM, 0);

    // Local
    memset(&sa_loc, 0, sizeof(struct sockaddr_in));
    sa_loc.sin_family = AF_INET;
    sa_loc.sin_port = htons(0);
    sa_loc.sin_addr.s_addr = inet_addr(argv[1]);

    if((ret = bind(fd, (struct sockaddr *)&sa_loc, sizeof(struct sockaddr))) < 0)
        Abort("Binding to local address");


    // Remote
    memset(&sa_dst, 0, sizeof(struct sockaddr_in));
    sa_dst.sin_family = AF_INET;
    sa_dst.sin_port = htons(80);
    sa_dst.sin_addr.s_addr = inet_addr(argv[2]); // google :)

    if((ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr))) < 0)
        Abort("Connect to remote address");

    printf("\n\nConnection Successfully made!!\n\n");    }

void Abort(char *msg){
fprintf(stderr,"\n\n<ERROR>: <%s>\n",msg);
perror("\n\nExiting...");
exit(EXIT_FAILURE);       }

问题是bind 返回-1。我已经测试过了。

谁能帮我解决这个问题吗?

提前致谢

最佳答案

当Winsock函数失败时,您可以调用WSAGetLastError()来找出原因。

在这种情况下,您没有调用 WSAStartup(),因此 socket() 失败并出现您忽略的 WSANOTINITIALISED 错误,所以你最终将一个无效的套接字传递给bind()

关于c - windows下绑定(bind)C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15163800/

相关文章:

encryption - 是什么导致 XOR 加密返回 "blank"?

c - 在嵌入式系统中存储/验证校验和

c - 有没有办法告诉操作系统丢弃任何缓冲的传出 TCP 数据?

Python 2.7 : Tkinter, bind方法如何使用?

iphone - 在 xcode for iphone 中带有 LIKE 语句的 sqlite

jquery - Backbone - 查看元素持续触发 Mouseenter/Mouseleave

c - C 中单个线程中的多个队列

c - 使用 Park&Miller RNG 制作大样本?

c# - 为服务器应用程序使用 3 个端口是否明智?

javascript - 在你的 redux 应用程序中哪里有套接字对象?