2013-03-31 81 views
3

我期待實現此目標:服務器從客戶端接收一個字符串(文件名),他必須從文件夾中取出並通過已定義的bufferSize將其返回給客戶端從命令行。它必須通過UDP通信來實現。我熟悉TCP套接字,但我不知道如何獲得udp連接的fileDescriptor,因爲接受缺失。UDP多客戶端服務器基礎知識

所以我想這個:配置後我做了一個while循環在服務器,我得到'某種描述符',我會發送到一個新的線程,知道在哪裏發送數據返回... 有任何想法嗎?

我檢查了網絡,但沒有找到這種操作的具體解釋。

回答

3

我終於找到了一些有用的東西,也許它會對別人有用,所以這裏是:代碼被評論和真正清楚。 PS:很抱歉,長時間粘貼,但谷歌不讓我複製鏈接。

/* 
* udpserver.c - A simple UDP echo server 
* usage: udpserver <port> 
*/ 

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

#define BUFSIZE 1024 

/* 
* error - wrapper for perror 
*/ 
void error(char *msg) { 
    perror(msg); 
    exit(1); 
} 

int main(int argc, char **argv) { 
    int sockfd; /* socket */ 
    int portno; /* port to listen on */ 
    int clientlen; /* byte size of client's address */ 
    struct sockaddr_in serveraddr; /* server's addr */ 
    struct sockaddr_in clientaddr; /* client addr */ 
    struct hostent *hostp; /* client host info */ 
    char buf[BUFSIZE]; /* message buf */ 
    char *hostaddrp; /* dotted decimal host addr string */ 
    int optval; /* flag value for setsockopt */ 
    int n; /* message byte size */ 

    /* 
    * check command line arguments 
    */ 
    if (argc != 2) { 
    fprintf(stderr, "usage: %s <port>\n", argv[0]); 
    exit(1); 
    } 
    portno = atoi(argv[1]); 

    /* 
    * socket: create the parent socket 
    */ 
    sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
    if (sockfd < 0) 
    error("ERROR opening socket"); 

    /* setsockopt: Handy debugging trick that lets 
    * us rerun the server immediately after we kill it; 
    * otherwise we have to wait about 20 secs. 
    * Eliminates "ERROR on binding: Address already in use" error. 
    */ 
    optval = 1; 
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 
     (const void *)&optval , sizeof(int)); 

    /* 
    * build the server's Internet address 
    */ 
    bzero((char *) &serveraddr, sizeof(serveraddr)); 
    serveraddr.sin_family = AF_INET; 
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    serveraddr.sin_port = htons((unsigned short)portno); 

    /* 
    * bind: associate the parent socket with a port 
    */ 
    if (bind(sockfd, (struct sockaddr *) &serveraddr, 
     sizeof(serveraddr)) < 0) 
    error("ERROR on binding"); 

    /* 
    * main loop: wait for a datagram, then echo it 
    */ 
    clientlen = sizeof(clientaddr); 
    while (1) { 

    /* 
    * recvfrom: receive a UDP datagram from a client 
    */ 
    bzero(buf, BUFSIZE); 
    n = recvfrom(sockfd, buf, BUFSIZE, 0, 
     (struct sockaddr *) &clientaddr, &clientlen); 
    if (n < 0) 
     error("ERROR in recvfrom"); 

    /* 
    * gethostbyaddr: determine who sent the datagram 
    */ 
    hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
       sizeof(clientaddr.sin_addr.s_addr), AF_INET); 
    if (hostp == NULL) 
     error("ERROR on gethostbyaddr"); 
    hostaddrp = inet_ntoa(clientaddr.sin_addr); 
    if (hostaddrp == NULL) 
     error("ERROR on inet_ntoa\n"); 
    printf("server received datagram from %s (%s)\n", 
     hostp->h_name, hostaddrp); 
    printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf); 

    /* 
    * sendto: echo the input back to the client 
    */ 
    n = sendto(sockfd, buf, strlen(buf), 0, 
      (struct sockaddr *) &clientaddr, clientlen); 
    if (n < 0) 
     error("ERROR in sendto"); 
    } 
} 
+1

文件,可以發現:https://www.cs.cmu.edu/afs/cs/academic/class/15213-f99/www/class26/udpserver.c – loolooyyyy

3

你沒有得到與UDP的連接;您使用sendto()recvfrom()來發送和接收消息。

所以服務器將在套接字上調用recvfrom();從接收到的數據中解開請求,執行相應的操作,然後使用sendto()將響應發回給收到請求的客戶端。

客戶端將打電話給sendto()將消息打包到服務器,然後調用recvfrom()來獲得響應。請注意,UDP是不可靠的協議;不能保證每封郵件都會發送。客戶端必須實施超時以防服務器丟棄UDP請求。服務器也必須能夠處理重複的請求。