如何在raspberry pi上打开TCP端口端口5001? [英] How do I open a TCP port port 5001 on raspberry pi?

查看:143
本文介绍了如何在raspberry pi上打开TCP端口端口5001?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用带有jessie OS的raspberry pi 2.

i希望树莓(服务器)通过tcp ip与我的Windows计算机(客户端)通信。

i有在我的电脑上添加了一个端口。

我对如何打开树莓上的端口有疑问



电脑和覆盆子没有直接连接。他们连接在同一个网络中



我尝试过:



  / *  使用TCP 
的互联网域中的简单服务器端口number作为参数传递* /

#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h < span class =code-keyword>>
#include < span class =code-keyword>< sys / types.h >
#include < sys / socket.h >
#include < netinet / in.h >

void 错误( const char * msg)
{
perror(msg);
退出( 1 );
}

int main( int argc, char * argv [])
{
int sockfd,newsockfd,portno = 5001 ;
socklen_t clilen;
char buffer [ 256 ];
struct sockaddr_in serv_addr,cli_addr;
int n;
if (argc< 2 ){
fprintf(stderr, 错误,没有提供端口\ n);
退出( 1 );
}
sockfd = socket(AF_INET,SOCK_STREAM, 0 );
if (sockfd< 0
error( ERROR打开套接字);
bzero(( char *)& serv_addr, sizeof (serv_addr));
portno = atoi(argv [ 1 ]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd,( struct sockaddr *)& serv_addr,
sizeof (serv_addr))< 0
error( 绑定错误);
listen(sockfd, 5 );
clilen = sizeof (cli_addr);
newsockfd = accept(sockfd,
struct sockaddr *)& cli_addr,
& clilen);
if (newsockfd< 0
错误( 接受)时的错误;
bzero(缓冲区, 256 );
/ *
n = read(newsockfd,buffer,255);
if(n< 0)error(从套接字读取错误);
printf(这是消息:%s \ n,缓冲区);
n =写(newsockfd,我收到你的消息,18);
n = write(newsockfd,Server-Message,14);
* /

n = write(newsockfd, Server-Message 512 );
if (n< 0 )错误( ERROR写入套接字);
close(newsockfd);
close(sockfd);
return 0 ;
}





程序执行时没有错误。但我得到的输出是



错误,没有端口提供

解决方案

你的程序需要一个端口number在命令行上传递。所以在启动时通过它:

 myserver 5001 





或者你可以通过更改CPallini建议的代码来硬编码端口号。但是你必须评论/删除获取端口号的行:

 //当使用硬编码的端口号
// if(argc< 2){
// fprintf(stderr,ERROR,no port provided\\\
);
//退出(1);
//}
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd< 0)
error(ERROR open socket);
bzero((char *)& serv_addr,sizeof(serv_addr));
//使用硬编码端口号
// portno = atoi(argv [1]);


引用:

if(argc< 2){

fprintf(stderr,ERROR,no port provided\\\
);

退出(1);

}



由于你硬连线了端口号,你必须删除上面的行。


currently i am using raspberry pi 2 with jessie OS.
i want the raspberry(server) to communicate with my windows computer(client) via tcp ip.
i have added a port on my pc side.
I have a doubt regarding how to open up a port on the raspberry

the pc and raspberry are NOT connected directly. they are connected in the same network

What I have tried:

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include  < sys/types.h > 
#include < sys/socket.h >
#include < netinet/in.h >

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno=5001;
     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);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     /*
         n = read(newsockfd,buffer,255);
         if (n < 0) error("ERROR reading from socket");
         printf("Here is the message: %s\n",buffer);
         n = write(newsockfd,"I got your message",18);
        n = write(newsockfd,"Server-Message",14);
     */
     n = write(newsockfd,"Server-Message",512);
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);
     close(sockfd);
     return 0; 
}



the program executes without error. but the output i get is

ERROR, no port provided

解决方案

Your program requires that a port number is passed on the command line. So pass that when starting it:

myserver 5001



Alternatively you can hard code the port number by changing the code as suggested by CPallini. But then you have to comment / remove the line getting the port number too:

// Ignore this when using hardcoded port number
//if (argc < 2) {
//    fprintf(stderr,"ERROR, no port provided\n");
//    exit(1);
//}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) 
    error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
// Using hard coded port number
//portno = atoi(argv[1]);


Quote:

if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}


Since you hard-wired the port number, you have to remove the above lines.


这篇关于如何在raspberry pi上打开TCP端口端口5001?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆