gpt4 book ai didi

c - 绑定(bind) UDP 套接字时出现问题

转载 作者:行者123 更新时间:2023-11-30 20:34:32 25 4
gpt4 key购买 nike

我目前正在进行的一个项目要求客户端通过 UDP 联系服务器,然后使用服务器在对等点之间建立 token 环网络。服务器收集每个客户端/对等点的 IP 和端口,然后将它们存储到数组中。然后,服务器将这些数组的元素通过网络发送回对等点。然后,对等点使用此信息相互连接,而不是与服务器连接。我在同行之间传输和接收消息时遇到问题。使用打印语句,我已经能够确定地址结构包含正确的端口和地址。我可能正在处理缓冲区问题或某种计时问题。我已经多次重新配置我的代码,但似乎无法使其工作。此时任何提示将不胜感激。

编辑——使用wireshark,我已经能够确定该消息确实是通过网络发送的。程序在以下函数的 while 循环中停止。但是没有收到消息

//Thread function to check for messages on the socket
void receiveMessages(int socket) {
long int nBytes = -1;
char buffer[BUFFERSIZE];

//Wait to receive message from other peers
while (nBytes <= 0) {
nBytes = recvfrom(socket, buffer, BUFFERSIZE, 0, NULL, NULL);
}

//Print the message received
printf("%s", buffer);
}

主程序

//
// runPeer.c
// tokenRing
//
// Created by Bryce Hagar on 2/13/17.
// Copyright © 2017 Bryce Hagar. All rights reserved.
//

#include <stdio.h>
#include "bbpeer.h"

int main(int argc, const char * argv[]){
//Used for creating the Client-Server socket
int clientSocket;

//Create a local peer
Host localPeer = createHost(true, true);
//Get user information; update local peer
getUserInfo(localPeer, argc, argv);
//Configure socket structure with local peer info
configSockStruct(localPeer, INADDR_ANY, localPort);

//Create a remote server
Host remoteServer = createHost(false, false);
//Configure socket structure with remote server info
configSockStruct(remoteServer, remoteIP, remotePort);

//Create a socket bound to the local peer
clientSocket = createSocket(localPeer);

//Announce peer's existence to server and receive acceptance
announceToServer(localPeer, remoteServer, clientSocket);

//Create new remote peer host structure
Host remotePeer = getPeerInfo(clientSocket);
freeHost(remoteServer);

//Send messages to peers
sendMessageToPeer(clientSocket, remotePeer, localPeer);

//Receive messages from other peers
receiveMessages(clientSocket);

return 0;
}

对等 ADT

//
// bbpeer.c
// bbpeer
//
// Created by Bryce Hagar on 2/16/17.
// Copyright © 2017 Bryce Hagar. All rights reserved.
//

#include "bbpeer.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>


#define BUFFERSIZE 1024
#define ACCEPTED "accepted"
#define NEW "new"
#define TEST "test"

int localPort, remotePort; //User input globals
const char * remoteIP;
pthread_t newThread;

/* Get user input from command line */
void getUserInfo(Host localPeer, int argc, const char *argv[]) {
int j = 1;

//Error check for input issues
if (!localPeer->isPeer) {
printf("User input is only applicable for peers.\n");
exit(EXIT_FAILURE);
}

//Get user input from command line
if (argc == 6) {
//Connecting to new token ring (server),
if (!strcmp(argv[1],"-new")) {
localPeer->newRing = true;
j++;
}
}

//Assign arguments to global variables
localPort = atoi(argv[j++]);
remoteIP = argv[j++];
remotePort = atoi(argv[j]);
}

//Send announcement to server, and wait for acknoledgement
void announceToServer(Host localPeer, Host remoteServer, int clientSocket) {
long int nBytes; //Used to send messages
char buffer[BUFFERSIZE];

while(!localPeer->accepted){

//Announce new peer message to server
memset(buffer, 0, BUFFERSIZE);
strcpy(buffer, NEW);
nBytes = strlen(buffer) + 1;

//Send message to server
sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&remoteServer->sockAddr,
remoteServer->addr_size);

//Receive message from server
nBytes = recvfrom(clientSocket,buffer,BUFFERSIZE,0,NULL, NULL);

//If accepted, set flag
if (!strcmp(buffer, ACCEPTED)) {
localPeer->accepted = true;
printf("ACCEPTED BY SERVER\n");//(debug)
}
}
}

//Get info from server message to peers
Host getPeerInfo(int clientSocket) {
long int numBytes = -1;
int port;
char buffer[BUFFERSIZE];
char *ip;

//Clear buffer, wait for message from server
memset(buffer, 0, BUFFERSIZE);
while (numBytes <= 0) {
numBytes = recvfrom(clientSocket, buffer, BUFFERSIZE, 0, NULL, NULL);
}

//Tokenize the message into ip and port
ip = strtok(buffer, "\n");
port = atoi(strtok(NULL, "\n"));

//Create a new host struct for the remote peer
Host remotePeer = createHost(true, false);
//Configure the socket structure for the new remote peer
configSockStruct(remotePeer, ip, port);

return remotePeer;
}

////////////////////////// Possible Error //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
/////////// ////////////////////////////////////////////////////////////////////////
//Send message to remote peer from local peer
void sendMessageToPeer(int p2pSocket, Host remotePeer, Host localPeer) {
long int nBytes;
char buffer[BUFFERSIZE];
bzero(buffer, BUFFERSIZE);
strcpy(buffer, TEST);

//Send "test" message
nBytes = strlen(buffer)+1;

//Send message to the socket(debug)
sendto(p2pSocket,buffer,nBytes,0,(struct sockaddr *)&remotePeer->sockAddr,
remotePeer->addr_size);
//Print the ip and port stored in the remote peer host (debug)
printf("%s:%d\n", inet_ntoa(remotePeer->sockAddr.sin_addr),
ntohs(remotePeer->sockAddr.sin_port));
}

//Thread function to check for messages on the socket
void receiveMessages(int socket) {
long int nBytes = -1;
char buffer[BUFFERSIZE];

//Wait to receive message from other peers
while (nBytes <= 0) {
nBytes = recvfrom(socket, buffer, BUFFERSIZE, 0, NULL, NULL);
}

//Print the message received
printf("%s", buffer);
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////// Possible Error //////////////////////////////////////

主机ADT

//
// host.c
// bbserver
//
// Created by Bryce Hagar on 2/17/17.
// Copyright © 2017 Bryce Hagar. All rights reserved.
//

#include "host.h"

/* Allocate memory for new host struct,
assigns variables for peers and servers*/
Host createHost(bool isPeer, bool isLocal) {
Host newHost = malloc(sizeof(*newHost));
newHost->addr_size = sizeof newHost->sockAddr;
newHost->isPeer = isPeer;
newHost->isLocal = isLocal;
if (isPeer) {
newHost->newRing = false;
newHost->hasToken = false;
newHost->accepted = false;
}
return newHost;
}

void freeHost(Host thisHost) {
free(thisHost);
}

/* Configure socket structure */
void configSockStruct(Host thisHost, const char *ip, int port) {

memset((void *) &thisHost->sockAddr, 0, (size_t)sizeof(thisHost->sockAddr));
thisHost->sockAddr.sin_family = AF_INET;
memset(thisHost->sockAddr.sin_zero, '\0', sizeof thisHost->
sockAddr.sin_zero);
thisHost->sockAddr.sin_port = htons(port);

if (!thisHost->isLocal) {
//Get address from functions parameters
struct hostent *hostptr = gethostbyname(ip);
memcpy((void *)&thisHost->sockAddr.sin_addr,
(void *)hostptr->h_addr, hostptr->h_length);
}
else {
thisHost->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
}
}

int createSocket(Host thisHost) {
//Create UDP socket
int newSocket = socket(AF_INET, SOCK_DGRAM, 0);
bind(newSocket, (struct sockaddr *)&thisHost->sockAddr,
sizeof(thisHost->sockAddr));
return newSocket;
}

最佳答案

经过数小时徒劳的故障排除,缩小了问题范围后,我发现我的计算机 (Mac) 阻止我的程序接收消息。该程序第一次在大学的 SSH 服务器上尝试时就成功了。到目前为止,我已经使用 ssh 服务器作为我的客户端-服务器程序的服务器,并在本地创建了客户端(对等点)。在服务器上都尝试过之后,第一次就成功了。

关于c - 绑定(bind) UDP 套接字时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com