我有2个树莓pi,我想在它们之间发送一些数据以同时在两者上启动功能,两者之间的连接是一条以太网电缆,并且都没有连接到Internet。
我遇到一个问题,即服务器代码一经接受就会挂起。我已经包含了服务器和客户端代码。我正在使用在客户端代码中运行服务器代码的Pi的IP。我认为这是客户端没有将连接发送到正确位置的问题,还是服务器没有被告知正确的接收位置的问题。
该代码基于此处的示例代码http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html。
服务器:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
//#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char *argv[]){
int sockfd, newsockfd, portno;
printf("Sock \n");
socklen_t clilen;
printf("clilen \n");
char buffer[256];
printf("buffer \n");
struct sockaddr_in serv_addr, cli_addr;
printf("struct \n");
int n;
printf("n \n");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("Sockfd \n");
bzero((char *) &serv_addr, sizeof(serv_addr));
printf("bzero \n");
portno = 6666;
printf("port \n");
serv_addr.sin_family = AF_INET;
printf("afinet \n");
serv_addr.sin_addr.s_addr = INADDR_ANY;
printf("inadder any \n");
serv_addr.sin_port = htons(portno);
printf("htons \n");
printf("listen \n");
cout << listen(sockfd,5) << "\n";
listen(sockfd,5);
clilen = sizeof(cli_addr);
cout << "&clilen: " << &clilen << "\n";
cout << "clilen: " << clilen << "\n";
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
printf("newsock \n");
bzero(buffer,256);
printf("bezero 2 \n");
n = read(newsockfd,buffer,255);
printf("read \n");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
printf("write \n");
return 0;
}
服务器输出:
Sock
clilen
buffer
struct
n
Sockfd
bzero
port
afinet
inadder any
htons
listen
0
&clilen: 0x7eeaaf0c
clilen: 16
客户代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
//#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char *argv[]){
int sockfd, portno, n;
printf("sock \n");
struct sockaddr_in serv_addr;
printf("sockaddr \n");
struct in_addr ipv4addr;
printf("ipv4 \n");
struct hostent *server;
printf("hostent \n");
char buffer[256];
printf("buffer \n");
portno = 6666;
printf("port \n");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("sockfd \n");
if (sockfd < 0)
printf("ERROR opening socket");
inet_pton(AF_INET, "169.254.59.109", &ipv4addr);
printf("inet \n");
server = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
cout << "server: " << server << "\n";
printf("server \n");
//if (server == NULL) {
//fprintf(stderr,"ERROR, no such host\n");
//exit(0);
//}
bzero((char *) &serv_addr, sizeof(serv_addr));
printf("bzero \n");
serv_addr.sin_family = AF_INET;
printf("af \n");
bcopy((char *)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->h_length);
printf("bcopy \n");
serv_addr.sin_port = htons(portno);
printf("htons \n");
cout << "connect: " << (connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))) << "\n";
cout << "sockfd: " << sockfd << "\n";
cout << "struct: " << (struct sockaddr *)&serv_addr << "\n";
cout << "serv: " << sizeof(serv_addr) << "\n";
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
printf("ERROR connecting\n");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
printf("write \n");
if (n < 0)
printf("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
printf("read \n");
if (n < 0)
printf("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}
客户端输出:
sock
sockaddr
ipv4
hostent
buffer
port
sockfd
inet
server: 0x76d3c5a4
server
bzero
af
bcopy
htons
connect: -1
sockfd: 3
struct: 0x7ea32ef0
serv: 16
ERROR connecting
Please enter the message: hello