在套接字C / C ++上发送浮点值

时间:2016-07-21 18:01:21

标签: c++ c linux sockets networking

我是编程的新手;我需要从C ++中的程序向C中的另一个程序发送一些浮点值。我在互联网上找到了这个示例代码并设法让它正常工作:

服务器:

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

#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/
#define LISTENQ 8 /*maximum number of client connections */

int main (int argc, char **argv)
{
    int listenfd, connfd, n;
    socklen_t clilen;
    char buf[MAXLINE];
    struct sockaddr_in cliaddr, servaddr;

    //creation of the socket
    listenfd = socket (AF_INET, SOCK_STREAM, 0);

    //preparation of the socket address 
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    printf("%s\n","Server running...waiting for connections.");

    for ( ; ; ) {

        clilen = sizeof(cliaddr);
        connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
        printf("%s\n","Received request...");

        while ( (n = recv(connfd, buf, MAXLINE,0)) > 0)  {
            printf("%s","String received from and resent to the client:");
            puts(buf);
            send(connfd, buf, n, 0);
        }

        if (n < 0) {
            perror("Read error"); 
            exit(1);
        }
        close(connfd);

    }
    //close listening socket
    close(listenfd); 

}

客户端:

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


#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/

int
main(int argc, char **argv) 
{
    int sockfd;
    struct sockaddr_in servaddr;
    char sendline[MAXLINE], recvline[MAXLINE];

    //basic check of the arguments
    //additional checks can be inserted
    if (argc !=2) {
        perror("Usage: TCPClient <IP address of the server"); 
        exit(1);
    }

    //Create a socket for the client
    //If sockfd<0 there was an error in the creation of the socket
    if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
        perror("Problem in creating the socket");
        exit(2);
    }

    //Creation of the socket
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr= inet_addr(argv[1]);
    servaddr.sin_port =  htons(SERV_PORT); //convert to big-endian order

    //Connection of the client to the socket 
    if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
        perror("Problem in connecting to the server");
        exit(3);
    }

    while (fgets(sendline, MAXLINE, stdin) != NULL) {

        send(sockfd, sendline, strlen(sendline), 0);

        if (recv(sockfd, recvline, MAXLINE,0) == 0){
            //error: server terminated prematurely
            perror("The server terminated prematurely"); 
            exit(4);
        }
        printf("%s", "String received from the server: ");
        fputs(recvline, stdout);
    }

    exit(0);
}

那么我必须添加什么来向服务器发送浮点值并将其恢复?这些机器都是基于x86的,一个安装了Debian 6,另一个安装了Linux Mint。我试过两种方式(Debian with Client和Linux Mint with Server和Debian with Server和Linux Mint with Client),它运行正常。 我m thinking about making some casting between a char value and float... It可能吗?有什么方法可以在没有投射的情况下发送吗?我不需要char值,只需要一些浮点值。 谢谢你的帮助!

编辑:我尝试使用snprintf和sscanf,但我做错了,因为它无法正常工作。以下是客户端和服务器:

客户端:

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


#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/

int
main(int argc, char **argv) 
{
 int sockfd;
 struct sockaddr_in servaddr;
 char sendline[MAXLINE], recvline[MAXLINE], buffer [256];
//Añadidas por mi//
 float a, b, c;
 a = 0;
 b = 0;
 c = 0;
 c = a + b;

  printf("\nPrimer numero: ");
  scanf("%f", &a);
  printf ("\nSegundo numero: ");
  scanf ("%f", &b);

sprintf(buffer, "%f", sizeof c, c);


unsigned char len = strlen(buffer);

 //basic check of the arguments
 //additional checks can be inserted
 if (argc !=2) {
  perror("Usage: TCPClient <IP address of the server");
  exit(1);
 }

 //Create a socket for the client
 //If sockfd<0 there was an error in the creation of the socket
 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
  perror("Problem in creating the socket");
  exit(2);
 }

 //Creation of the socket
 memset(&servaddr, 0, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr= inet_addr(argv[1]);
 servaddr.sin_port =  htons(SERV_PORT); //convert to big-endian order

 //Connection of the client to the socket 
 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
  perror("Problem in connecting to the server");
  exit(3);
 }

 while (fgets(sendline, MAXLINE, stdin) != NULL) {

  send(sockfd, sendline, strlen(sendline), 0);
  send(sockfd, &len, sizeof len, 0);
  send (sockfd, buffer, sizeof buffer, 0);

  if (recv(sockfd, recvline, MAXLINE,0) == 0){
   //error: server terminated prematurely
   perror("The server terminated prematurely"); 
   exit(4);
  }
  printf("%s", "String received from the server: ");
  fputs(recvline, stdout);
 }

 exit(0);
}

服务器:

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

#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/
#define LISTENQ 8 /*maximum number of client connections */

int main (int argc, char **argv)
{
 float c;
 int listenfd, connfd, n;
 socklen_t clilen;
 char buf[MAXLINE], buf256[256], buffer[256];
 unsigned char len = strlen(buffer);
 struct sockaddr_in cliaddr, servaddr;

 //creation of the socket
 listenfd = socket (AF_INET, SOCK_STREAM, 0);

 //preparation of the socket address 
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 servaddr.sin_port = htons(SERV_PORT);

 bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

 listen(listenfd, LISTENQ);

 printf("%s\n","Server running...waiting for connections.");

 for ( ; ; ) {

  clilen = sizeof(cliaddr);
  connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
  printf("%s\n","Received request...");

  while ( (n = recv(connfd, buf, MAXLINE,0)) > 0)  {
   printf("%s","String received from and resent to the client:");
   puts(buf);


   recv(connfd, &len, sizeof len, 0);
   recv(connfd, buf256, len, 0);
   recv(connfd, buffer, 256, 0);
   buf256[len] = 0;
   sscanf(buffer, "%f", &c);
   puts(buffer);
   printf ("\n%f", &c);   
   send(connfd, buf, n, 0);
  }

 if (n < 0) {
  perror("Read error"); 
  exit(1);
 }
 close(connfd);

 }
 //close listening socket
 close(listenfd); 
}

程序很奇怪,它显示的值如0.0000e或有时不显示任何内容。我需要发送各种浮点值,所以这个程序只是一个关于如何发送一个浮点值的例子。任何人都可以看到我的程序中的错误在哪里?谢谢!

1 个答案:

答案 0 :(得分:1)

代码可以使用二进制表示发送float。这可能适用于匹配良好的系统,否则可能由于float表示或字节序不同而失败。 @clarasoft-it

float x = ...;

// send
send(sockfd, &x, sizeof x, 0);

// receive
recv(connfd, &x, sizeof x, 0);

替代代码可以使用sufficient digits打印字符串表示来发送float。这更便携。它没有endian-ness问题。它确实需要传送字符串长度信息。 @user3386109 @EOF

如果浮点格式的末尾不同,则范围和精度问题可能需要额外处理,无论使用何种方法,这都是一个问题。

#include <float.h>
#define FLT_EXPO_SIZE 5

// send
// create a buffer large enough
//       -   d   .     ddddd                 e   -       expo       \0
char buf[1 + 1 + 1 + (FLT_DECIMAL_DIG - 1) + 1 + 1 + FLT_EXPO_SIZE + 1];
// print the value to enough digits to distinguish x from all other float
unsigned char len = sprintf(buf, "%.*e", FLT_DECIMAL_DIG - 1, x);
send(sockfd, &len, sizeof len, 0);
send(sockfd, buf, sizeof buf, 0);

// receive
char buf256[256];
recv(connfd, &len, sizeof len, 0);
recv(connfd, buf256, len, 0);
buf256[len] = 0;
sscanf(buf256, "%f", &x);              // add sscanf() check

注意:

  1. 其他代码可用于更好地定义FLT_EXPO_SIZE而不是保守的5.基于FLT_MIN_EXPFLT_MAX_EXPFLT_TRUE_MIN的内容。

  2. 可以将sprintf("%a", x);用于十六进制表示。在这种情况下需要TBD缓冲区大小。仍然可以使用sscanf(buf, "%f", &x)

  3. 可以使用snprintf()

  4. 为简洁起见,省略了各种错误检查。