我正在BeagleBone Black和我的电脑之间创建一个本地网络。两个设备都通过以太网连接。我的BleagleBone有IP地址=“192.168.1.15”,我的电脑有IP地址=“192.168.1.20”。我通过ssh连接连接到BeagleBone,以便将所有必要的文件传递给它。
另一方面,BleagleBone是此通信中的客户端,因为它会将数据字符串发送到muy PC(服务器)。
我使用以下代码:
server.cpp
#include <iostream>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
using namespace std;
#define PORT 20001
#define SERVER_BUFFER_SIZE 1024
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
// create a socket
// socket(int domain, int type, int protocol)
printf("\nHola1");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("\nHola2");
if (sockfd < 0){
error("ERROR opening socket");
}
printf("\nHola3");
// clear address structure
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
/* setup the host_addr structure for use in bind call */
// server byte order
serv_addr.sin_family = AF_INET;
// automatically be filled with current host's IP address
serv_addr.sin_addr.s_addr = INADDR_ANY;
// convert short integer value for port must be converted into network byte order
serv_addr.sin_port = htons(portno);
// bind(int fd, struct sockaddr *local_addr, socklen_t addr_length)
// bind() passes file descriptor, the address structure,
// and the length of the address structure
// This bind() call will bind the socket to the current IP address on port, portno
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
// This listen() call tells the socket to listen to the incoming connections.
// The listen() function places all incoming connection into a backlog queue
// until accept() call accepts the connection.
// Here, we set the maximum size for the backlog queue to 5.
listen(sockfd,5);
// The accept() call actually accepts an incoming connection
clilen = sizeof(cli_addr);
// This accept() function will write the connecting client's address info
// into the the address structure and the size of that structure is clilen.
// The accept() returns a new socket file descriptor for the accepted connection.
// So, the original socket file descriptor can continue to be used
// for accepting new connections while the new socker file descriptor is used for
// communicating with the connected client.
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
printf("server: got connection from %s port %d\n",inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
// This send() function sends the 13 bytes of the string to the new socket
send(newsockfd, "Hello, world!\n", 13, 0);
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
close(newsockfd);
close(sockfd);
return 0;
}
client.cpp
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n", buffer);
close(sockfd);
return 0;
}
首先,我通过以下命令在./Server 20001上运行服务器可执行文件 然后我通过./Client 192.168.1.20 20001
在BleagleBone中运行客户端我在BeagleBone上获得的是: ERROR连接:拒绝连接
我不明白为什么它不起作用。
注意:当我指定一个端口时,它不会绑定。只是停止在那里工作,它不会给出错误。