-4

既存のソケット間でデータを送受信する方法がわかりません。現在、1 つの文字列を前後に送信して、それを見ることができるようにしようとしています。私が間違っていることについての助けをいただければ幸いです。

問題の部分はここにあります:

    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また、それが時期尚早に返されるか、さらにデータが到着するのを待っている間にブロックされる可能性があることに注意してください。これらの状況に自分で対応する必要があります。

また、UDP では、TCP とは異なり、データの配信の保証や確認がないことに注意してください。したがって、プログラムがデータを送信しても、クライアントへの送信中にデータが失われる可能性があります。

于 2012-05-09T21:53:07.667 に答える