-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
整合INT #4
Comments
测试UDP: #include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXLINE 80
#define SERV_PORT 8888
struct INThdr
{
float t1;
float t2;
float t3;
}int_header;
struct sendData
{
struct INThdr inthdr;
char sendbuf[MAXLINE];
}send_data;
void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE];
/* connect to server */
if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1)
{
perror("connect error");
exit(1);
}
while(fgets(sendline, MAXLINE, fp) != NULL)
{
// add INT header
struct sendData senddata;
memset(&senddata, 0, sizeof(struct sendData));
memset(&senddata.inthdr, 0, sizeof(struct INThdr));
memcpy(&senddata.sendbuf, sendline, sizeof(sendline));
/* send to server */
write(sockfd, &senddata, sizeof(senddata));
/* receive data from server */
struct sendData recvdata;
n = read(sockfd, &recvdata, sizeof(recvdata));
if(n == -1)
{
perror("read error");
exit(1);
}
recvdata.sendbuf[n-sizeof(struct INThdr)] = 0; /* terminate string */
printf("INT: %f %f %f ", recvdata.inthdr.t1, recvdata.inthdr.t2, recvdata.inthdr.t3);
fputs(recvdata.sendbuf, stdout);
}
}
int main(int argc, char **argv)
{
int sockfd; // socket fd
struct sockaddr_in servaddr; // the server address
/* check args */
if(argc != 2)
{
printf("usage: udpclient <IPaddress>/n");
exit(1);
}
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("[%s] is not a valid IPaddress/n", argv[1]);
exit(1);
}
// set up the socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// send the msg
do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
return 0;
} server: #include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 80
#define SERV_PORT 8888
struct INThdr
{
float t1;
float t2;
float t3;
}int_header;
struct sendData
{
struct INThdr inthdr;
char sendbuf[MAXLINE];
}send_data;
void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for(;;)
{
len = clilen;
/* waiting for receive data */
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
/* sent data back to client */
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
int main(void)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
/* bind address and port to socket */
if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind error");
exit(1);
}
do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
return 0;
} compile: #!/bin/bash
gcc -Wall -o client client.c &&
gcc -Wall -o server server.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bpf_code:
run.py:
The text was updated successfully, but these errors were encountered: