-4

我似乎无法弄清楚如何在现有套接字之间发送和接收数据。现在我只是想来回发送一个字符串并能够看到它。任何关于我做错了什么的帮助将不胜感激。

问题部分在这里:

    char hostm[10];
    printw("you are host!\n");
    printw("Type the number you would like to pick %s: ", req.hostname);//gets row1
    scanw("%s", &hostm);
    printw("%s\n", hostm);

                write( (struct sockaddr *)&req.sa, &hostm, sizeof(hostm));
                char response[80];
                read( (struct sockaddr *)&resp.sa, &response, sizeof(response));

                printw("Response: %s\n", response);
    recvfrom(sock, (void *)&resp, sizeof (matchrequest), 0, (struct sockaddr *)&sa, &fromlen);

     char response[80];
char response2[80];
read( (struct sockaddr *) &host.sa, &response, sizeof(response));
read( (struct sockaddr *) &req.sa, &response2, sizeof(response2));
printf("\n\nresponse was %s\n%s\n", &response, &response2);
        write( (struct sockaddr *) &req.sa, &response, sizeof(response));
write((struct sockaddr *) &host.sa, &response2, sizeof(response2));

我也不知道它是否在正确的位置,所以这里是所有代码:

对等体.c

#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "match.h"

int sock;
struct sockaddr_in serveraddr;
matchrequest req;
int matched = 0;

void *thread_start(void *args) {
int bytes_sent;
while(!matched) {
    printf("waiting for peers...\n");
    bytes_sent = sendto(sock, &req, sizeof(matchrequest), 0,(struct sockaddr*)&serveraddr, sizeof serveraddr);
    if (bytes_sent < 0) {
        printf("Error sending packet: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    sleep(3);
}
}
void writeResponse(char * response) {
write(sock, &response, sizeof(response));
}

int main(int argc, char *argv[])
{
pthread_t th;
struct sockaddr_in sa; 

matchrequest resp;
ssize_t recsize;
socklen_t fromlen;


sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

if (argc != 3) {
    printf("specify name and game type for this peer!\n");
    return;
}

req.gametype = atoi(argv[2]);
req.state = 0;
strncpy(req.peername, argv[1], 100);

printf("using name: %s\n", req.peername);

memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = 0;

if (-1 == bind(sock,(struct sockaddr *) &sa, sizeof(sa)))
{
        perror("error bind failed");
        close(sock);
        exit(EXIT_FAILURE);
}

memset(&serveraddr, 0, sizeof serveraddr);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serveraddr.sin_port = htons(7652);
pthread_create(&th, NULL, &thread_start, NULL);

for (;;) 
{
    fromlen = sizeof(sa);
        recsize = recvfrom(sock, (void *)&resp, sizeof (matchrequest), 0, (struct sockaddr *)&sa, &fromlen);
        if (recsize < 0) {
            fprintf(stderr, "%s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
    if (recsize != sizeof(matchrequest)) {
        fprintf(stderr, "invalid datagram received");
        continue;
    }


    initscr();
        printw("matched with peer %s.  Game can start!\n", resp.peername);

    if(strcmp(resp.hostname, req.peername) == 0)
    {

        char hostm[10];
        printw("you are host!\n");
        printw("Type the number you would like to pick %s: ", req.hostname);//gets row1
        scanw("%s", &hostm);
        printw("%s\n", hostm);

                    write( (struct sockaddr *)&req.sa, &hostm, sizeof(hostm));
                    char response[80];
                    read( (struct sockaddr *)&resp.sa, &response, sizeof(response));

                    printw("Response: %s\n", response);
        recvfrom(sock, (void *)&resp, sizeof (matchrequest), 0, (struct sockaddr *)&sa, &fromlen);


    }
    else
    {
        char peerm[10];
                    printw("you are peer!\n");
                    printw("Type the number you would like to pick %s: ", req.peername);//gets row1
                    scanw("%s", &peerm);
                    printw("%s\n", &peerm);

                    // send to client, transmitted fine
                    write(sock, &peerm, sizeof(peerm));
                    char response[80];
                    read(sock, &response, sizeof(response));
                    // Prints the missing character symbol, and 'random' letters
                    printw("Response: %s\n", response);

    }
    endwin();
}
close(sock); /* close the socket */
return 0;
}

匹配的.c

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

#define MAX_REQUESTS 100

matchrequest ready[MAX_REQUESTS];
int outsock;

void handlerequest(struct sockaddr_in sa, matchrequest req) {

matchrequest host;
int idx = req.gametype % MAX_REQUESTS;

req.state = READY;
req.sa = sa;

printf("%s on %s:%d requested game type %d \n", req.peername, inet_ntoa(req.sa.sin_addr), req.sa.sin_port, req.gametype);

// if room, place in ready position
if (strncmp(ready[idx].peername, req.peername, 100) == 0) {
    // peer is just checking in
} else if (ready[idx].state == MATCHED) {
    ready[idx] = req;
    printf("%s selected as host.  waiting for peers...\n", req.peername);
    strcpy(req.hostname, req.peername);
} else {
// match with existing peer
    printf("%s matched with existing host\n", req.peername);
    req.state = MATCHED;
    host = ready[idx];
    host.state = HOST;
    ready[idx] = req;
    strcpy(req.hostname, host.peername);
    req.peermove = req.hostmove;
    req.hostmove = req.peermove;

    char response[80];
    char response2[80];
    read( (struct sockaddr *) &host.sa, &response, sizeof(response));
    read( (struct sockaddr *) &req.sa, &response2, sizeof(response2));
    printf("\n\nresponse was %s\n%s\n", &response, &response2);
            write( (struct sockaddr *) &req.sa, &response, sizeof(response));
    write((struct sockaddr *) &host.sa, &response2, sizeof(response2));

 //     read(outsock, &response, sizeof(response));
 //     printf("\n\nresponse was %s\n\n", &response);
 //     write(outsock, &response, sizeof(response));


    printf("sending response to %s at %s:%d\n", host.peername, inet_ntoa(host.sa.sin_addr), host.sa.sin_port);
    sendto(outsock, (void *) &req, sizeof(matchrequest), 0, (struct sockaddr *) &host.sa, sizeof(host.sa));
    printf("sending response to %s at %s:%d\n", req.peername, inet_ntoa(req.sa.sin_addr), req.sa.sin_port);
    sendto(outsock, (void *) &host, sizeof(matchrequest), 0, (struct sockaddr *) &req.sa, sizeof(req.sa));
        fprintf(stderr, "%s\n", strerror(errno));
}
}

int main(void)
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa; 
matchrequest req;
ssize_t recsize;
socklen_t fromlen;

outsock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(7652);

if (-1 == bind(sock,(struct sockaddr *)&sa, sizeof(sa)))
{
        perror("error bind failed");
        close(sock);
        exit(EXIT_FAILURE);
} 
for (;;) 
{
    fromlen = sizeof(sa);
        printf ("receiving....\n");

        recsize = recvfrom(sock, (void *)&req, sizeof (matchrequest), 0, (struct sockaddr *)&sa, &fromlen);
        if (recsize < 0) {
            fprintf(stderr, "%s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
    if (recsize != sizeof(matchrequest)) {
        fprintf(stderr, "invalid datagram received");
        continue;
    }
    handlerequest(sa, req);
}
}

匹配.h

 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>

 typedef enum matchstate {MATCHED = 0, READY, HOST} matchstate;

 typedef struct matchrequest {
    matchstate state;
    int gametype;
    char board[4][4];
    char covboard[4][4];
    int player1pts;
    int player2pts;
    int gameover;
    int turn;
    int hostmove;
    int peermove;
    char peername[100];
    char hostname[100];
    struct sockaddr_in sa;
 } matchrequest;
4

1 回答 1

0

我认为问题在于readwrite期望文件描述符作为第一个参数,但您完全提供了其他东西。请参阅文档write并注意您没有按照应有的方式调用它。另外,我没有阅读您的所有代码,但它似乎会产生很多您不应该忽略的警告。

另请注意,这read可能会过早返回,或者在等待更多数据到达时可能会阻塞。您必须自己应对这些情况。

此外,请记住,与 TCP 不同,使用 UDP 并不能保证或确认数据的传递。因此,尽管您的程序已经发送了数据,但它可能会在传输到客户端时丢失。

于 2012-05-09T21:53:07.667 回答