在不同的机器上运行 UDP 客户端和服务器 [英] Running UDP client and server in different machines

查看:48
本文介绍了在不同的机器上运行 UDP 客户端和服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UDP client and a server running fine when run on the same computer. (Here for the client i have given 127.0.0.1 as the destination IP on the UDP client).

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

However when run on different machines, and replacing 127.0.0.1 with my the ip of the machine where server.c is present, the program doesnt work. The problem is i'm not getting any error message.

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

Below is the code for server and client. Please tell me any modifications to be performed to run the same over different machines(I'm using LINUX OS).

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

Note: My machines serverIP(machine where server.c is present): 10.60.5.945, clientIP: 10.60.5.950

server.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);
}

client.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);
}

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

Above is the code working fine when it is run on the same system. Please tell me the modifications to be made to run the above code on different machines. Thanks in advance.

推荐答案

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

Have you tried changing char *arg="127.0.0.1"; to the servers IP address? The IP address 10.60.5.945mentioned in your question, is not a valid IP.

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

Even better would be to take the IP (or even a hostname that you resolve) from argv[1].

ps.您可以通过键入以下内容查看 Linux 机器的有效 IPv4 地址:ip -4 a l scope global

ps. You can see the valid IPv4 addresses of your Linux box by typing: ip -4 a l scope global

这篇关于在不同的机器上运行 UDP 客户端和服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆