📄 listener.c
字号:
// Phillip Hoang
// Arun Gupta
// May 31, 2001
//=============================================================================
// ----------------------------------------------------------------
// Version 0.2
// A while(1) loop was inserted so that it would constantly look
// for udp packets.
// The webpage also now includes a 1 second refresh
// ----------------------------------------------------------------
// Version 0.1
// Added file I/O to the program so that it creates a webpage.
// ----------------------------------------------------------------
// Version 0.0
// This receives temperature information from the udp packet.
// Takes buf[0] the first byte of the packet and does some
// calculations to display the temperature in degrees farenheit.
// ----------------------------------------------------------------
//=============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
//=============================================================================
#define MYPORT 4950 /* the port users will be sending to */
#define MAXBUFLEN 100
//=============================================================================
void create_html_file(float fahr, float remainder)
{
FILE *WebFile;
// Create File
WebFile = fopen("temperature.html", "w");
if( WebFile == NULL ) {
puts("Error opening file temperature.html");
exit(0);
}
// Write to file
fprintf(WebFile,"<html>\n");
// Meta Refresh
fprintf(WebFile,"<head>\n");
fprintf(WebFile,"<META HTTP-EQUIV=\"refresh\" content=\"1;URL=temperature.html\">");
fprintf(WebFile,"</head>\n");
// Display Temperature
fprintf(WebFile,"Temperature: %d.%d \n",(int)fahr,(int)remainder);
fprintf(WebFile,"</html>");
fclose(WebFile);
}
//=============================================================================
void print_temperature(char temp)
{
float fahr;
float remainder;
fahr = 1.8 * (int)temp + 32;
remainder = fahr;
remainder = fahr - (int)fahr;
remainder = remainder * 10;
printf("Temperature: %d.%d \n",(int)fahr,(int)remainder);
create_html_file(fahr, remainder);
}
//=============================================================================
int main(void)
{
int sockfd;
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int addr_len, numbytes;
char buf[MAXBUFLEN];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
// Open a Socket
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
// Wait for UDP packet with temperature information, and Display temperature.
while(1) {
if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
print_temperature(buf[0]);
}
close(sockfd);
return 0;
}
//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -