2

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

server: nc -u -l -v 1234   
client: nc -u 192.168.2.64 1234

server: nc -l -v 1234  
client: nc 192.168.2.64 1234 ,same PC Fine , different PCs .. no go.

客户端软件

#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);
}

服务器代码

#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);
      }
   }
 }

提前致谢

4

0 回答 0