1

在同一台计算机上运行时,我有一个 UDP 客户端和一个运行良好的服务器。(这里对于客户端,我已将127.0.0.1作为 UDP 客户端上的目标 IP)。

但是,当在不同的机器上运行并用我的 server.c 所在机器的 ip 替换127.0.0.1时,程序不起作用。问题是我没有收到任何错误消息。

下面是服务器和客户端的代码。请告诉我要执行的任何修改以在不同的机器上运行相同的内容(我使用的是 LINUX 操作系统)。

注意:我的机器serverIP(存在 server.c 的机器):10.60.5.945clientIP:10.60.5.950

服务器.c:

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
    int cont,create_socket,new_socket,addrlen,fd;
    int bufsize = 1024; 
    int nameLen=0;
    int client_address_size=0;
    char *buffer = malloc(10);
    char fname[256];
    struct sockaddr_in address,client;

    if ((create_socket = socket(AF_INET,SOCK_DGRAM,0)) > 0)
    printf("The socket was created\n");

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;

    address.sin_port = htons(15000);

    if (bind(create_socket,(struct sockaddr *)&address,sizeof(address))== 0)
        printf("Binding Socket\n");

    nameLen=sizeof(address);

    if (getsockname(create_socket,(struct sockaddr *)&address,&nameLen)<0)
    {
        printf("\n\ngetsockname() error\n");
        exit(3);
    }

    printf("Port assigned is %d\n", ntohs(address.sin_port));

    client_address_size=sizeof(client);



    if(recvfrom(create_socket,fname, 255,0,(struct sockaddr *) &client,&client_address_size)<0)
    {
        printf("\n\nrecvfrom() failed\n");
        exit(4);
    }

    printf("A request for filename %s Received..\n", fname);

    if ((fd=open(fname, O_RDONLY))<0)
    {
        perror("File Open Failed");
        exit(0);
    }

    while((cont=read(fd, buffer, 10))>0) 
    {
        sleep(1);
        sendto(create_socket,buffer,cont,0,(struct sockaddr *) &client,client_address_size);
        printf("\n\nPacket sent\n");
    }

    sendto(create_socket,"*",1,0,(struct sockaddr *) &client,client_address_size);

    printf("Request Completed\n");
    return close(create_socket);
}

客户端.c:

#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>

int main()
{
    int create_socket,cont,res;
    char *arg="127.0.0.1";

    int bufsize = 1024;
    int server_address_size=0;
    char *buffer = malloc(10);
    char fname[256];
    struct sockaddr_in address;
    int serv_addr_size = sizeof(address);

    if ((create_socket = socket(AF_INET,SOCK_DGRAM,0)) > 0)
        printf("The Socket was created\n");

    address.sin_family = AF_INET;
    address.sin_port = htons(15000);
    address.sin_addr.s_addr=inet_addr(arg);

    if (connect(create_socket,(struct sockaddr *) &address,sizeof(address)) == 0)
        printf("The connection was accepted with the server %s...\n",arg);

    printf("Enter The Filename to Request : ");
    scanf("%s",fname);
    res=sendto(create_socket, fname, sizeof(fname), 0,(struct sockaddr *) &address,sizeof(address));
    if(res<0)
    {
        printf("\n\nSendto falied...\n");
        exit(0);
    }
    printf("Request Accepted... Receiving File...\n\n");

    server_address_size=sizeof(address);

    printf("The contents of file are...\n\n");

    while((cont=recvfrom(create_socket, buffer, 10, 0,(struct sockaddr *) &address,&serv_addr_size))>0) 
    {
        if(buffer[cont-1]=='*')
           break;
        write(1, buffer, cont);

    }

    printf("\nEOF\n");
    return close(create_socket);
}

以上是在同一系统上运行时运行良好的代码。请告诉我在不同机器上运行上述代码需要做的修改。提前致谢。

4

1 回答 1

2

您是否尝试更改char *arg="127.0.0.1";为服务器 IP 地址?您的问题中提到的 IP 地址10.60.5.945不是有效的 IP。

更好的是从 argv[1] 获取 IP(甚至是您解析的主机名)。

附言。您可以通过键入以下命令查看 Linux 机器的有效 IPv4 地址:ip -4 al scope global

于 2013-03-15T11:18:07.343 回答