下面程序是第一章的習題,向一個服務器發送消息獲取時間.在linux用codeblocks運行出錯.而且卷2裡面有大量源代碼,不知道從何學起,怎麼實現?
#include
#include
#include
#include
#include
#include
#include
#define BUFFERSIZE 150
int main()
{
struct sockaddr_in serv;
char buff[BUFFERSIZE];
int sockfd, n;
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
printf("socket error");
bzero((char *) &serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr("140.252.1.32");
serv.sin_port = htons(13);
if (sendto(sockfd, buff, BUFFERSIZE, 0,
(struct sockaddr *) &serv, sizeof(serv)) != BUFFERSIZE)
printf("sendto error");
if ((n = recvfrom(sockfd, buff, BUFFERSIZE, 0,
(struct sockaddr *) NULL, (int *) NULL)) < 2)
printf("recvfrom error");
buff[n - 2] = 0;
printf("%s\n", buff);
exit(0);
}
看下是否缺少頭文件,一般有makefile自己找找。