Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 当我切换源ip地址时,C套接字绑定速度变慢_Sockets - Fatal编程技术网

Sockets 当我切换源ip地址时,C套接字绑定速度变慢

Sockets 当我切换源ip地址时,C套接字绑定速度变慢,sockets,Sockets,我有一个C脚本,它通过套接字连接到远程服务器并编写命令 我必须尽快做到这一点,我需要从源ip地址切换。问题是,当我从ip地址切换时,绑定速度会减慢几秒钟 我找不到解决办法 守则: #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h

我有一个C脚本,它通过套接字连接到远程服务器并编写命令

我必须尽快做到这一点,我需要从源ip地址切换。问题是,当我从ip地址切换时,绑定速度会减慢几秒钟

我找不到解决办法

守则:

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h> 

int main()
{

    struct sockaddr_in source, destination = {};  //two sockets declared as previously
    int sock = 0;
    int n = 0;
    int datalen = 0;
    int pkt = 0;


    char* ips[3] = {"10.0.0.1", "10.0.0.2", "10.0.0.3"};

    uint8_t *send_buffer;
    char recv_buffer[11];

    struct sockaddr_storage fromAddr;   // same as the previous entity struct sockaddr_storage serverStorage;
    unsigned int addrlen;  //in the previous example socklen_t addr_size;
    struct timeval tv;
    tv.tv_sec = 3;  /* 3 Seconds Time-out */
    tv.tv_usec = 0;

    /*Inititalize source to zero*/
    memset(&source, 0, sizeof(source));       //source is an instance of sockaddr_in. Initialization to zero
    /*Inititalize destinaton to zero*/
    memset(&destination, 0, sizeof(destination));


        /* setting the destination, i.e our OWN IP ADDRESS AND PORT */
        destination.sin_family = AF_INET;                 
        // destination.sin_addr.s_addr = inet_addr("123.456.789.123");  
        destination.sin_port = htons(43); 


            /*---- Configure settings of the source address struct, WHERE THE PACKET IS COMING FROM ----*/
        /* Address family = Internet */
        source.sin_family = AF_INET;    
        /* Set IP address to localhost */   
        // source.sin_addr.s_addr = INADDR_ANY;
        /* Set port number, using htons function to use proper byte order */
        source.sin_port = htons(43); 



        /* Set all bits of the padding field to 0 */
        memset(source.sin_zero, '\0', sizeof source.sin_zero); //optional


    int i;
    for (i=0; i<60; i++) {  

        /* creating the socket */         
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
            printf("Failed to create socket\n");

        /*set the socket options*/
        setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));



        if(inet_pton(AF_INET, ips[i%3], &source.sin_addr)<=0) //this is where is switch the ip addresses
        {
            printf("\n inet_pton error occured\n");
            return 1;
        } 


        /*bind socket to the source WHERE THE PACKET IS COMING FROM*/
        if (bind(sock, (struct sockaddr *) &source, sizeof(source)) < 0) 
            printf("Failed to bind socket");



        if(inet_pton(AF_INET, "94.198.154.139", &destination.sin_addr)<=0)
        {
            printf("\n inet_pton error occured\n");
            return 1;
        } 

        if(connect(sock, (struct sockaddr *)&destination, sizeof(destination)) < 0)
        {
           printf("\n Error : Connect Failed \n");
           return 1;
        } 

        printf("check\n");

        n = write(sock,"is liveresults.nl\r\n",21);
        if (n < 0) error("ERROR writing to socket");

        while ( (n = read(sock, recv_buffer, sizeof(recv_buffer)-1)) > 0)
        {
            recv_buffer[n] = 0;
            if(fputs(recv_buffer, stdout) == EOF)
            {
                printf("\n Error : Fputs error\n");
            }
        } 

        if(n < 0)
        {
            printf("\n Read error \n");
        } 


        close(sock);

    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
源中的struct sockaddr_,destination={};//前面声明的两个套接字
int-sock=0;
int n=0;
int datalen=0;
int-pkt=0;
char*ips[3]={“10.0.0.1”、“10.0.0.2”、“10.0.0.3”};
uint8_t*发送缓冲区;
字符接收缓冲区[11];
struct sockaddr\u storage fromAddr;//与前面的实体struct sockaddr\u storage serverStorage相同;
unsigned int addrlen;//在前面的示例中为socklen\u t addr\u size;
结构时间值电视;
tv.tv_sec=3;/*3秒超时*/
tv.tv_usec=0;
/*将源初始化为零*/
memset(&source,0,sizeof(source));//source是sockaddr_的一个实例。初始化为零
/*初始化目标为零*/
memset(&destination,0,sizeof(destination));
/*设置目的地,即我们自己的IP地址和端口*/
destination.sinu family=AF\u INET;
//destination.sin_addr.s_addr=inet_addr(“123.456.789.123”);
destination.sinu港口=htons(43);
/*----配置数据包来自的源地址结构的设置----*/
/*地址家庭=互联网*/
source.sin_family=AF_INET;
/*将IP地址设置为localhost*/
//source.sin\u addr.s\u addr=INADDR\u ANY;
/*设置端口号,使用htons函数使用正确的字节顺序*/
source.sin_port=htons(43);
/*将填充字段的所有位设置为0*/
memset(source.sin_zero,“\0”,source.sin_zero的大小);//可选
int i;

对于(i=0;i我必须将本地端口绑定到0,而不是与传出端口绑定到同一个端口。

我从来没有听说过
bind
需要很长时间才能完成。假设您在linux上运行,我会尝试在
strace
下运行您的程序。这可能会让您了解系统调用界面上发生的情况。
strace-e trace=network my_program
将输出限制为与网络相关的调用。您应该看到它在任何延迟的系统调用期间暂停打印。这里没有任何证据表明任何操作需要多长时间。您的假设都是由于
bind()
是没有根据的。@EJP当我删除绑定时速度很快,所以我建议绑定会减慢速度。@GilHamilton多亏了你!使用strace,我在绑定上发现了这个错误:=“-1 EADDRINUSE(地址已在使用)”。现在我找到了解决方案,我正在尝试将本地ip地址绑定到与传出端口相同的端口。当我将端口更改为零(0)时,脚本工作正常:)