问题描述
我有从 github 下载的 TCP 客户端-服务器 c 程序。代码在主机(x86)中运行良好。但是当我为arm64架构板编译并运行相同的程序时,客户端“连接被拒绝”错误来了。
测试设置和程序
- 目标板和主机使用单个以太网连接。
- 分配静态 IP 以便两者都在同一网络中。
- 目标板和主机都能够 PING 对方。
- 服务器在 arm64 目标板和主机上的客户端中运行。
输出:
套接字创建成功.. 与服务器的连接失败...:连接被拒绝
观察
- 当客户端和服务器都在目标板上运行时,连接正在建立。 当 TCP 服务器和客户端在两种不同的架构中运行时会遇到此问题。
以下是代码
**// Server code
//#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 10500
#define SA struct sockaddr
//print error message and exit process
void die(char *msg)
{
perror(msg);
exit(0); // EXIT_FAILURE
}
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff,MAX);
// read the message from client and copy it in buffer
read(sockfd,buff,sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ",buff);
bzero(buff,MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd,sizeof(buff));
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit",4) == 0) {
printf("Server Exit...\n");
break;
}
}
}
// Driver function
int main()
{
int sockfd,connfd;
unsigned int len;
struct sockaddr_in servaddr,cli;
// socket create and verification
sockfd = socket(AF_INET,SOCK_STREAM,0); // same as socket(AF_INET,IPPROTO_TCP)
if (sockfd == -1)
die("socket creation Failed...");
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr)); // memset
// assign IP,PORT
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* host-to-network endian */
servaddr.sin_port = htons(PORT); /* for listening */
// Binding newly created socket to given IP and verification
if ((bind(sockfd,(SA*)&servaddr,sizeof(servaddr))) != 0)
die("socket bind Failed...");
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd,5)) != 0) // max 5 connections
die("Listen Failed...");
else
printf("Server listening for any client..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd,(SA*)&cli,&len);
if (connfd < 0)
die("server acccept Failed...");
else
printf("server acccepted the client connection...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd);
}**
//client code
// Write CPP code here
//#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
//#include <netinet/in.h>
#define MAX 80
#define PORT 10500
#define SA struct sockaddr
void die(char *msg)
{
perror(msg);
exit(0); // EXIT_FAILURE
}
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff,sizeof(buff));
printf("Enter the string for sending to server: ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,sizeof(buff));
printf("From Server : %s",buff);
if ((strncmp(buff,"exit",4)) == 0) { // if Exit message received from Server
printf("Client Exit...\n");
break; //chat ended
}
}
}
int main()
{
int sockfd;
struct sockaddr_in servaddr;
// socket create and varification
sockfd = socket(AF_INET,IPPROTO_TCP);
if (sockfd == -1)
die("socket creation Failed...");
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr)); // memset
// assign IP,PORT
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT); /* for connecting */
// connect the client socket to server socket
if (connect(sockfd,sizeof(servaddr)) != 0)
die("connection with the server Failed...");
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
这个问题令人困惑。我请求帮助我解决问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)