繁体   English   中英

通过套接字发送结构

[英]Sending Struct over Socket

我对使用套接字非常陌生,到目前为止,只能通过套接字发送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发送stu.name ,但是不确定发送struct时方法sendto()使用哪些参数。

客户

#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()来执行此操作?

您应该将struct序列化为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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM