I'm fairly new to working with sockets, and so far have only been able to send a char []
over a socket.
Now though, I'm trying to send a struct
over a socket and am unsure of how to do this. I have the following struct
struct student_rec {
char name[25];
float gpa;
int pid;
};
In which I've initialized with the following
struct student_rec stu
strcpy(stu.name, "Mike McDonald");
stu.gpa = 2.5;
stu.pid = 0x12345678;
I can send stu.name
without any issue, but am unsure of what parameters to use for the method sendto()
when sending a struct
.
Client
#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);
}
Server
#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);
}
I think I'm getting confused as to what to replace strlen(stu.name)
with as I'm now sending the entire struct
.
Could I use a for
loop to read each element of my struct
, or is there some parameters I can pass to sendto()
to do this?
Aucun commentaire:
Enregistrer un commentaire