Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 套接字:客户端/服务器不能在不同的PC上工作,但可以在同一平台上工作_Sockets_Networking - Fatal编程技术网

Sockets 套接字:客户端/服务器不能在不同的PC上工作,但可以在同一平台上工作

Sockets 套接字:客户端/服务器不能在不同的PC上工作,但可以在同一平台上工作,sockets,networking,Sockets,Networking,我想使用套接字将数据从一个linux应用程序(手指阅读器)发送到PC。finger reader的资源非常有限,我选择使用UDP数据包向PC发送数据。我下载了用于客户端和服务器的“开始Linux编程”代码,它运行良好,但只在同一平台上运行。使用wireshark,我可以看到客户端数据(手指读取器)数据在服务器PC上到达,但应用程序只是忽略它,客户端重试,然后超时(仅TCP)。我可以从两边ping,所以我有通信(wireshark确认)。我还尝试了TCP,并使用netcat获得了类似的结果。这两个

我想使用套接字将数据从一个linux应用程序(手指阅读器)发送到PC。finger reader的资源非常有限,我选择使用UDP数据包向PC发送数据。我下载了用于客户端和服务器的“开始Linux编程”代码,它运行良好,但只在同一平台上运行。使用wireshark,我可以看到客户端数据(手指读取器)数据在服务器PC上到达,但应用程序只是忽略它,客户端重试,然后超时(仅TCP)。我可以从两边ping,所以我有通信(wireshark确认)。我还尝试了TCP,并使用netcat获得了类似的结果。这两个“设备”通过开关连接。我也在Ubuntu 13.04和OpenSuse 12.3上试过这个。我认为wireshark中显示的数据没有任何“问题”(使用ip.addr==xx或udp.port或tcp.port等作为筛选器) Netcat(别名nc)

客户端软件

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    char ch = 'A';

/*  Create a socket for the client.  */

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

/*  Name the socket, as agreed with the server.  */
    memset (&address,0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.2.52");
    address.sin_port = htons(9734);
    len = sizeof(address);

/*  Now connect our socket to the server's socket.  */
    result = connect(sockfd, (struct sockaddr *)&address, len);

    if(result == -1) {
        perror("oops: client3");
        exit(1);
    }
    /*  We can now read/write via sockfd.  */

    write(sockfd, &ch, 1);
    read(sockfd, &ch, 1);
    printf("char from server = %c\n", ch);
    close(sockfd);
    exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
int-sockfd;
内伦;
地址中的结构sockaddr_;
int结果;
char ch='A';
/*为客户端创建套接字*/
sockfd=套接字(AF_INET,SOCK_STREAM,0);
/*按照与服务器商定的方式命名套接字*/
memset(&address,0,sizeof(address));
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr(“192.168.2.52”);
address.sin_port=htons(9734);
len=sizeof(地址);
/*现在将我们的套接字连接到服务器的套接字*/
结果=连接(sockfd,(结构sockaddr*)和地址,len);
如果(结果==-1){
佩罗尔(“oops:client3”);
出口(1);
}
/*我们现在可以通过sockfd进行读/写*/
写入(sockfd和ch,1);
读取(sockfd和ch,1);
printf(“来自服务器的字符=%c\n”,ch);
关闭(sockfd);
出口(0);
}
服务器代码

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{ 
   int server_sockfd, client_sockfd;
   int server_len, client_len;
   struct sockaddr_in server_address;
   struct sockaddr_in client_address;

   //server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
   server_sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

   memset (&server_address,0, sizeof(server_address));
   server_address.sin_family = AF_INET;
   server_address.sin_addr.s_addr = htonl(INADDR_ANY);
   server_address.sin_port = htons(9734);
   server_len = sizeof(server_address);
   bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

   /*  Create a connection queue, ignore child exit details and wait for clients.  */

   listen(server_sockfd, 5);

   signal(SIGCHLD, SIG_IGN);

   while(1) {
      char ch;

      printf("server waiting\n");

  /*  Accept connection.  */

      client_len = sizeof(client_address);
      client_sockfd = accept(server_sockfd, 
                             (struct sockaddr *)&client_address, 
                             &client_len);

  /*  Fork to create a process for this client and perform a test to see
      whether we're the parent or the child.  */

      if(fork() == 0) {

   /*  If we're the child, we can now read/write to the client on client_sockfd.
       The five second delay is just for this demonstration.  */

          read(client_sockfd, &ch, 1);
          printf (">> Rx %c ",ch);
          //sleep(5);
          ch++;
          write(client_sockfd, &ch, 1);
          printf (" Tx %c ",ch);
          close(client_sockfd);
          exit(0);
       }

   /*  Otherwise, we must be the parent and our work for this client is finished.  */

      else {
          close(client_sockfd);
      }
   }
 }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{ 
int server_sockfd,client_sockfd;
int server_len,client_len;
服务器地址中的结构sockaddr\u;
客户端地址中的结构sockaddr\u;
//server\u sockfd=socket(AF\u INET,SOCK\u STREAM,0);
服务器\u sockfd=套接字(PF\u INET、SOCK\u流、IPPROTO\u TCP);
memset(&server_地址,0,sizeof(server_地址));
服务器地址.sin\u family=AF\u INET;
服务器地址sin\u addr.s\u addr=htonl(INADDR\u ANY);
服务器地址.sin\u端口=htons(9734);
server\u len=sizeof(服务器地址);
绑定(服务器\u sockfd,(结构sockaddr*)和服务器\u地址,服务器\u len);
/*创建连接队列,忽略子出口详细信息并等待客户端*/
听(服务器_sockfd,5);
信号(信号灯、信号灯);
而(1){
char ch;
printf(“服务器等待\n”);
/*接受连接*/
客户地址=sizeof(客户地址);
客户端\u sockfd=接受(服务器\u sockfd,
(结构sockaddr*)和客户端地址,
&客户(本地);;
/*Fork为该客户机创建一个进程,并执行测试以查看
不管我们是父母还是孩子*/
如果(fork()==0){
/*如果我们是孩子,我们现在可以在client_sockfd上读/写客户机。
五秒钟的延迟仅用于此演示*/
读取(客户机和通道,1);
printf(“>>接收%c”,ch);
//睡眠(5);
ch++;
写入(客户机和通道,1);
printf(“Tx%c”,ch);
关闭(客户_sockfd);
出口(0);
}
/*否则,我们必须是家长,我们为这个客户的工作就完成了*/
否则{
关闭(客户_sockfd);
}
}
}

提前感谢

您可以从尝试连接到服务器机器的机器执行telnet吗。请指定端口号作为服务器绑定的端口号。像“telnet ip地址端口号”之类的东西。如果出现连接被拒绝错误,则表示您的数据包被丢弃。。在这种情况下,我会尝试“服务iptables stop”。服务器PC上是否有防火墙?如果是这样,请尝试禁用防火墙,然后查看它是否工作。
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{ 
   int server_sockfd, client_sockfd;
   int server_len, client_len;
   struct sockaddr_in server_address;
   struct sockaddr_in client_address;

   //server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
   server_sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

   memset (&server_address,0, sizeof(server_address));
   server_address.sin_family = AF_INET;
   server_address.sin_addr.s_addr = htonl(INADDR_ANY);
   server_address.sin_port = htons(9734);
   server_len = sizeof(server_address);
   bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

   /*  Create a connection queue, ignore child exit details and wait for clients.  */

   listen(server_sockfd, 5);

   signal(SIGCHLD, SIG_IGN);

   while(1) {
      char ch;

      printf("server waiting\n");

  /*  Accept connection.  */

      client_len = sizeof(client_address);
      client_sockfd = accept(server_sockfd, 
                             (struct sockaddr *)&client_address, 
                             &client_len);

  /*  Fork to create a process for this client and perform a test to see
      whether we're the parent or the child.  */

      if(fork() == 0) {

   /*  If we're the child, we can now read/write to the client on client_sockfd.
       The five second delay is just for this demonstration.  */

          read(client_sockfd, &ch, 1);
          printf (">> Rx %c ",ch);
          //sleep(5);
          ch++;
          write(client_sockfd, &ch, 1);
          printf (" Tx %c ",ch);
          close(client_sockfd);
          exit(0);
       }

   /*  Otherwise, we must be the parent and our work for this client is finished.  */

      else {
          close(client_sockfd);
      }
   }
 }