使用数据报的并发服务器

时间:2013-03-26 00:49:23

标签: c pthreads datagram unix-socket

客户端必须向服务器发送2个字符串,服务器必须将字符串发送回客户端,并且还要并行处理客户端。这两个实体使用Unix下的数据报交换数据。我的问题是,当我使用线程时,我无法将数据发送回客户端,也无法读取它。即使我不使用线程,我也无法将正确的消息发送回客户端。

服务器是:

#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>


int sd = 0;
void* worker(void* argm) {
    struct sockaddr_in* adress = (struct sockaddr_in*) argm;
    char s[100],s2[100];
    int adressLen;

    recvfrom(sd, s, sizeof(s), 0, (struct sockaddr*) &adress, &adressLen);
    perror("recv");

Perror显示recv:非套接字上的套接字操作

    printf("I received: %s\n",s);

    strcat(s2,"sample");
    s2[strlen(s2)]='\0';
    sendto(sd, &s2, sizeof(s2),  0, (struct sockaddr*) &adress,sizeof(adress));
    perror("send");

}

最后perror()显示发送:非套接字上的套接字操作。 这也会导致客户端无限期地等待来自服务器的字符串。

int main(int argc, char* argv[]) {
    int sd;
    int servPort;

    struct sockaddr_in servAddr;  

    if (argc != 2){
        printf("usage %s <port server>\n", argv[0]);
        return 1;
    }

    if (1 != sscanf(argv[1], "%d", &servPort) || 0 > servPort || 0xffff < servPort){
        printf("%s invalid port\n",argv[1]);
        return 2;   
    }
        servPort = htons(servPort);

    sd = socket(AF_INET, SOCK_DGRAM, 0);
    perror("socket: ");


    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = servPort;

    bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    perror("bind");


    for(;;) {
        char s[100];
        struct sockaddr_in client;
        client.sin_family=AF_INET;
        int clientLen;



        recvfrom(sd, s, sizeof(s), 0, (struct sockaddr*) &client, &clientLen);
        perror("recv");
        printf("I received: %s\n",s);

        pthread_t thread;
        pthread_create(&thread, NULL, worker, (void*) &client);
        pthread_join(thread, NULL);

        }
    close(sd);
    return 0;
}

在控制台中打印的内容:

bind: Success
recv: Success
I received: da

recv: Socket operation on non-socket
I received: `���
send: Socket operation on non-socket
recv: Success
I received: nu

recv: Socket operation on non-socket
I received: `���
send: Socket operation on non-socket

客户是:

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


int main(int argc, char *argv[])
{
    int sd, r, lung, n;
    struct sockaddr_in servAddr;
    struct hostent *h;
    int servPort;


    if (argc != 3){
        printf("usage %s <IP server> <port server>\n", argv[0]);
        return 1;
    }

    h = gethostbyname(argv[1]);
    if(h==NULL) {
        printf("%s: unknown host '%s'\n",argv[0],argv[1]);
        return 1;
    }

        if (1 != sscanf(argv[2], "%d", &servPort) || 0 > servPort || 0xffff < servPort){
        printf("%s invalid port \n",argv[2]);
        return 2;   
    }
        servPort = htons(servPort);

    servAddr.sin_family = h->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = servPort;

    sd = socket(AF_INET,SOCK_DGRAM,0);
    perror("socket ");


    char s[100],s2[100],s3[100];
    printf("enter text 1:\n");
    fgets(s, 101, stdin);
    printf("enter text 2:\n");
    fgets(s2, 101, stdin);

    s[strlen(s)] = '\0';
    s2[strlen(s2)]='\0';

    sendto(sd, &s, sizeof(s), 0, (struct sockaddr*) &servAddr, sizeof(servAddr));
    perror("send");

    sendto(sd, &s2, sizeof(s2), 0, (struct sockaddr*) &servAddr, sizeof(servAddr));
    perror("send");


    n = recvfrom(sd, s3, sizeof(s3), 0, (struct sockaddr*) &servAddr, &lung);
    perror("recv");


    printf("I received : %s", s3);

    close(sd);
    return 0;
}

1 个答案:

答案 0 :(得分:3)

服务器中的主要问题是文件顶部有一个全局变量int sd = 0;,由worker()使用,main()中有一个局部变量也 - int sd;main()中的变量初始化为套接字;全局变量仍然指向标准输入(0),因此您尝试在非套接字上执行套接字操作。

工作代码 - server.c

使用我的stderr.cstderr.h代码报告错误。如果您需要代码,请通过电子邮件与我联系(参见我的个人资料)。我没有做任何标题最小化。

#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include "stderr.h"

int sd = 0;

static void *worker(void)
{
    struct sockaddr_in address;
    socklen_t          addressLen = sizeof(address);
    char msg[100];

    printf("%s(): sd = %d\n", __func__, sd);
    ssize_t n = recvfrom(sd, msg, sizeof(msg), 0, (struct sockaddr*) &address, &addressLen);
    if (n < 0)
        err_syserr("recvfrom() (worker) failed: ");
    else if (n == 0)
        err_report(ERR_EXIT, 0, "orderly shutdown by peer\n");

    err_remark("received: (%d bytes) %.*s\n", (int)n, (int)n, msg);
    msg[n] = '\0';

    strcat(msg, ": example");
    size_t len = strlen(msg);
    if ((n = sendto(sd, &msg, len, 0, (struct sockaddr*) &address, addressLen)) < 0)
        err_syserr("error on sendto(): ");
    err_remark("sent %d bytes\n", (int)n);
    return(0);
}

int main(int argc, char* argv[])
{
    int servPort;
    struct sockaddr_in servAddr;

    err_setarg0(argv[0]);
    err_setlogopts(ERR_PID|ERR_STAMP);  /* tag output with PID and time */

    if (argc != 2)
        err_usage("portnum");

    if (1 != sscanf(argv[1], "%i", &servPort) || 0 > servPort || 0xffff < servPort)
        err_error("invalid port %s\n", argv[1]);
    servPort = htons(servPort);

    if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        err_syserr("error on socket(): ");

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = servPort;

    if (bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
        err_syserr("error on bind(): ");

    for (;;)
    {
        char msg[100];
        struct sockaddr_in client;
        client.sin_family = AF_INET;
        socklen_t clientLen = sizeof(client);

        ssize_t n = recvfrom(sd, msg, sizeof(msg), 0, (struct sockaddr*) &client, &clientLen);
        if (n < 0)
            err_syserr("error on recvfrom(): ");
        if (n == 0)
            err_report(ERR_EXIT, 0, "orderly shutdown by peer\n");
        err_remark("received: (%d bytes) %.*s\n", (int)n, (int)n, msg);

        worker();
    }

    close(sd);
    return 0;
}

工作代码 - client.c

#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include "stderr.h"

int main(int argc, char *argv[])
{
    int sd;
    ssize_t n;
    struct sockaddr_in servAddr;
    struct hostent *h;
    int servPort;
    socklen_t addrlen;

    err_setarg0(argv[0]);

    if (argc != 3)
        err_usage("hostname portnum");

    h = gethostbyname(argv[1]);
    if (h==NULL)
        err_syserr("unknown host %s: ", argv[1]);

    if (1 != sscanf(argv[2], "%d", &servPort) || 0 > servPort || 0xffff < servPort)
        err_error("invalid port %s\n", argv[2]);
    servPort = htons(servPort);

    servAddr.sin_family = h->h_addrtype;
    memcpy(&servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = servPort;

    if ((sd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
        err_syserr("error on socket(): ");

    char s1[100],s2[100],s3[100];
    printf("enter text 1:\n");
    fgets(s1, sizeof(s1), stdin);
    printf("enter text 2:\n");
    fgets(s2, sizeof(s2), stdin);

    size_t s1_len = strlen(s1) - 1;
    size_t s2_len = strlen(s2) - 1;
    s1[s1_len] = '\0';  /* zap newline */
    s2[s2_len] = '\0';  /* zap newline */

    if (sendto(sd, &s1, s1_len, 0, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
        err_syserr("sendto() (s1) failed: ");

    if (sendto(sd, &s2, s2_len, 0, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
        err_syserr("sendto() (s2) failed: ");

    if ((n = recvfrom(sd, s3, sizeof(s3), 0, (struct sockaddr*) &servAddr, &addrlen)) < 0)
        err_syserr("recvfrom() (s3) failed: ");

    printf("Client received: (%d bytes) %.*s\n", (int)n, (int)n, s3);

    close(sd);
    return 0;
}

样本输出

跟踪构建程序,运行服务器以及运行两次客户端程序。 -I目录保存stderr.h-L目录保存libjl.a,其中包含从stderr.c构建的目标文件。

$ make server client
gcc -O3 -g -I/Users/jleffler/inc -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition server.c -o server -L/Users/jleffler/lib/64 -ljl
gcc -O3 -g -I/Users/jleffler/inc -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition client.c -o client -L/Users/jleffler/lib/64 -ljl
$ ./server 12345 &
$ ./client localhost 12345
enter text 1:
Goliath Beetle
enter text 2:
Golgafrincham Excesses
server: 2013-03-25 20:51:23 - pid=10895: received: (14 bytes) Goliath Beetle
worker(): sd = 3
server: 2013-03-25 20:51:23 - pid=10895: received: (22 bytes) Golgafrincham Excesses
server: 2013-03-25 20:51:23 - pid=10895: sent 31 bytes
Client received: (31 bytes) Golgafrincham Excesses: example
$ ./client localhost 12345
enter text 1:
Small talk - what people say to each other
enter text 2:
Smalltalk - an archetypal object-oriented programming language
server: 2013-03-25 20:52:14 - pid=10895: received: (42 bytes) Small talk - what people say to each other
worker(): sd = 3
server: 2013-03-25 20:52:14 - pid=10895: received: (62 bytes) Smalltalk - an archetypal object-oriented programming language
server: 2013-03-25 20:52:14 - pid=10895: sent 71 bytes
Client received: (71 bytes) Smalltalk - an archetypal object-oriented programming language: example
$