connect()在UDP客户端服务器中为C语言中的IPv6地址提供错误

时间:2015-12-31 06:44:52

标签: c windows sockets gcc

这里我试图在Windows中使用IPV6地址从服务器发送和接收数据。在客户端代码中,我没有获取服务器的地址,以便发送数据或接收数据。在测试我的代码时,我发现客户端代码中的 Connect()给出了运行时错误。可能有可能我无法正确复制 memmove()中的地址。实际上我在客户端代码中尝试使用 gethostbyaddr(),但仍然无法发送或接收数据。请帮助我。那怎么能连接到服务器......还发送和接收数据..提前致谢

* / 客户端代码Udp_win_IP6_Client.c / *

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>   
#pragma comment(lib,"ws2_32.lib")    
int main() {
    struct sockaddr_in6 client6;
    WORD wVersionRequested;
    int wsaerr;
    socklen_t size;
    struct hostent *server;
    WSADATA wsaData;
    char buffer[1024] = "Hi Harshpal Server";   
    int k;
    printf("\n Ready for Sending...\n");
    wVersionRequested = MAKEWORD(2, 2);
    wsaerr = WSAStartup(wVersionRequested, &wsaData);
    SOCKET clientsockfd;
    clientsockfd = socket(AF_INET6, SOCK_DGRAM, 0);
    if (clientsockfd == INVALID_SOCKET)
        printf("\n Error.... ");
    else
        printf("\n Successful Creation....");

    server = gethostbyname("::1");
    if(server < 0)
        printf("\n Couldnt get hostname");
    else
        printf("\n Got the hostname");

    memset((char *) &client6, 0, sizeof (client6));
    printf("\n Memory set \n");
    client6.sin6_flowinfo = 0;
    client6.sin6_family = AF_INET6;            
    memmove((char *) &client6.sin6_addr.s6_addr, (char *)server->h_name, server->h_length);
    client6.sin6_port = htons(5000);

    size = sizeof client6;
   if (connect(clientsockfd, (struct sockaddr *) &client6, sizeof (client6)) == SOCKET_ERROR)
        printf("ERROR connecting");
   else
        printf("Connected...");
    printf("Type a sentence to send to server:\n");
    scanf("%s", buffer);
    printf("You typed: %s\n", buffer);

    int n = send(clientsockfd, buffer, strlen(buffer) + 1, 0);
    sendto(clientsockfd, buffer, n, 0, (struct sockaddr *) &client6, size);

    memset(buffer, 0, 1024);
    n = recvfrom(clientsockfd, buffer, 1024, 0, NULL, NULL);    
    printf("This is Message Received from server: %s\n", buffer);
    closesocket(clientsockfd);    
    return 0;
}

* / *服务器端代码Udp_win_IP6_Server.c /

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

int main() {
    WORD wVersionRequested;
    int wsaerr;
    WSADATA wsaData;
    char buffer[1024];
    int k;
    printf("\n Ready for Receiving...\n");
    wVersionRequested = MAKEWORD(2, 2);    
    wsaerr = WSAStartup(wVersionRequested, &wsaData);

    SOCKET sockfd;
    sockfd = socket(AF_INET6, SOCK_DGRAM, 0);

    if(sockfd == INVALID_SOCKET)
        printf("\n Error.... ");    
    else
        printf("\n Successful Creation....");      
    struct sockaddr_in6 server6;
    memset(&server6, 0, sizeof (server6));
    server6.sin6_family = AF_INET6;
    server6.sin6_addr = in6addr_any;
    server6.sin6_port = htons(5000);

    if(bind(sockfd, (struct sockaddr *)&server6 , sizeof(server6))==-1)
    {
        printf("\n Error in binding..\n");       
    }
    else{
        printf("\n Binding successful...\n");
    }
    k = sizeof(server6);

    recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *) &server6, &k);     
    printf("\n Message from client %s: ", buffer);

    strcpy(buffer,"Hi Harshpal Client...");
    sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*) &server6,k);
    closesocket(sockfd);
    return 0;
}
  • 使用gcc Udp_win_IP6_Server.c直接运行代码-o Udp_win_IP6_Server.o
  • 和gcc Udp_win_IP6_Client.c -o Udp_win_IP6_Client.o
  • 或者您可以在任何IDE中运行。
  • 运行代码时无需指定地址或端口,就像我在代码中给出的那样。

2 个答案:

答案 0 :(得分:1)

我认为"yyyy"检查不正确,因为if (server < 0)是指针,因此无符号。您只需要检查它是否为server。此外,NULL也已弃用,但请将其用于示例代码。

Memmove部分似乎也错了。您需要从gethostbyname()而非h_addr_list获取已解决的地址。所以你的h_name初始化应该是:

client6

答案 1 :(得分:0)

我刚刚得到了答案....这是我的代码...很高兴与您分享.....保持Server.c相同并且只通过以下代码替换client.c: - )

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
int main() {

    char Buffer[1024] = "This is Client \n";
    char FromServerBuffer[1024];
    char hostname[1024];

    SOCKADDR_STORAGE From;
    WSADATA wsaData;
    ADDRINFO Hints, *AddInf, *AI;
    int RetVal, gaddr, i;
    LPSOCKADDR sockaddr_ip;

    WSAStartup(MAKEWORD(2, 2), &wsaData);
    // Intializing dll required..
    memset(&Hints, 0, sizeof (Hints));

    char ipstringbuffer[50];
    DWORD ipbufferlength = 50;

    SOCKET ConnSocket;
    Hints.ai_family = PF_UNSPEC;
    Hints.ai_socktype = SOCK_DGRAM;
    Hints.ai_flags = AI_PASSIVE;
    // Setting family, socktype and flag.
    gaddr = getaddrinfo("::1", "5000", &Hints, &AddInf);
    // getaddrinfo() will give information about address.

    for (AI = AddInf; AI != NULL; AI = AI->ai_next) {
        sockaddr_ip = (LPSOCKADDR) AI->ai_addr;
        // This loop to place IP and port into the list of IP addresses.
        int iRetval = WSAAddressToString(sockaddr_ip, (DWORD) AI->ai_addrlen, NULL, ipstringbuffer, &ipbufferlength);
        // Convert address in readable format..
        printf("\tIPv6 address %s\n", ipstringbuffer);

        ConnSocket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
        // Creating Socket...
        if (ConnSocket == INVALID_SOCKET)
            printf("socket call failed.. \n");

        if (connect(ConnSocket, AI->ai_addr, (int) AI->ai_addrlen) != SOCKET_ERROR) {
            // Connecting client socket to server..
            printf("Connection Establish..\n");
        } else
            printf("Connection not establish..");

        int FromLen = sizeof (From);
        RetVal = send(ConnSocket, Buffer, sizeof (Buffer), 0);
        // Sending data to server using send().
        //RetVal = sendto(ConnSocket, Buffer, sizeof (Buffer), 0, (LPSOCKADDR) & From, FromLen);
        if (RetVal < 0) {
            printf("Sending failed..\n");

        } else
            printf("Sending Successful..\n");

        int AmountRead = recvfrom(ConnSocket, FromServerBuffer, sizeof (FromServerBuffer), 0, (LPSOCKADDR) & From, &FromLen);
        // Receiving data using recvfrom..
        printf("Data Received From Server: %s", FromServerBuffer);

    }
    closesocket(ConnSocket);
    return 0;
}