c - 通过套接字发送结构

标签 c sockets struct

我对使用套接字还很陌生,到目前为止,我只能通过套接字发送 char []

虽然现在,我正尝试通过套接字发送 struct,但不确定如何执行此操作。我有以下 struct

struct student_rec {
    char name[25];
    float gpa;
    int pid;
};

我在其中使用以下内容进行了初始化

struct student_rec stu

strcpy(stu.name, "Mike McDonald");
stu.gpa = 2.5;
stu.pid = 0x12345678;

我可以毫无问题地发送 stu.name,但是我不确定在发送 sendto() 时要为方法 struct 使用什么参数。

客户端

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

/* This program sends a message in datagram to the receiver and waits
   for reply. */

#define MSG "CIS 454/554 is a great course!!!"

#define BUFMAX 2048

struct student_rec {
    char name[25];
    float gpa;
    int pid;
};

main(argc,argv)
int argc;
char *argv[];
{

  int sk;
  char buf[BUFMAX];

  struct student_rec stu;
  struct sockaddr_in remote;
  struct hostent *hp;

  strcpy(stu.name, "Mike McDonald");
  stu.gpa = 2.5;
  stu.pid = 0x12345678;

  /* Create an Internet domain datagram socket */
  sk = socket(AF_INET,SOCK_DGRAM,0);

  remote.sin_family = AF_INET;

  /* Get the remote machine address from its symbolic name 
     given in the command line argument */

  hp = gethostbyname(argv[1]);
  if (hp == NULL) {
      printf("Can't find host name. %s\n", argv[1]);
      exit (1);
  }

  bcopy(hp->h_addr,&remote.sin_addr.s_addr,hp->h_length);
  /* get the remote port number from the command line */
  remote.sin_port = ntohs(atoi(argv[2]));

  sendto(sk,stu.name,strlen(stu.name)+1,0,&remote,sizeof(remote));/* Send the message */
  read(sk,buf,BUFMAX); /* Get the reply */
  printf("%s\n",buf); 

  close(sk);
}

服务器

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

/* This program creates an Internet domain datagram socket, binds a
   name to it, reads from it, and then replies to the sender. */

#define MSG "Are you sure ? I doubt!!!"

#define BUFMAX 2048

struct student_rec {
    char name[25];
    float gpa;
    int pid;
};

main()
{
  struct sockaddr_in local, remote;
  int sk,rlen=sizeof(remote),len=sizeof(local);
  char buf[BUFMAX];

  /* Create an Internet domain datagram socket from which to read */
  sk = socket(AF_INET,SOCK_DGRAM,0);

  struct student_rec stu;

  strcpy(stu.name, "Mike McDonald");
  stu.gpa = 2.5;
  stu.pid = 0x12345678;

  local.sin_family = AF_INET; /* Define the socket domain */
  local.sin_addr.s_addr = INADDR_ANY; /* Wildcard mach. addr */
  local.sin_port = 0; /* Let the system assign a port */
  bind(sk,&local,sizeof(local)); 

  getsockname(sk,&local,&len); /* Get the port number assigned */
  printf("socket has port %d\n",htons(local.sin_port)); /* Publish the number */


  /* Read from the socket */
  recvfrom(sk,buf,BUFMAX,0,&remote,&rlen);
  printf("%s\n",buf);
  sendto(sk,stu.name,strlen(stu.name)+1,0,&remote,sizeof(remote)); 

  close(sk);
}

我想我对用什么替换 strlen(stu.name) 感到困惑,因为我现在要发送整个 struct

我可以使用 for 循环来读取我的 struct 的每个元素,还是有一些参数我可以传递给 sendto() 来执行此操作?

最佳答案

您应该将结构序列化为 xml、json、文本或类似格式,并在另一端将其读回:

/* Tx */
struct student_rec s = {...};
char txt[100];
const int len = sprintf(txt, "%d,%f,%s", s.pid, s.gpa, s.name);
send_txt(txt);
/* Rx */
char txt[100];
recv_txt(txt);
struct student_rec s
sscanf(txt, "%d,%f,%s", &s.pid, &s.gpa, s.name);

关于c - 通过套接字发送结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28757708/

相关文章:

c - C中的循环问题

C - 使用 sin_addr 获取 IP 地址字符串

c - 如何复制指针以指向同一对象

c - 错误 : expression must have struct or union type in c

c - strcpy 中的指针赋值是如何工作的?

在这个简单的程序中看不到逻辑问题

java - 套接字连接超时

Java的InputStream会破坏信息,为什么?

c - Typedef 结构定义,将指向所述结构的指针作为其元素

c - 什么时候应该使用 struct node* head=NULL 和 struct node *head=NULL ?