/* client.c */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include "error.h"
#define MAXLINE 80
#define SERV_PORT 8000
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr;
int sockfd, n;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN];
socklen_t servaddr_len;
//socket
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
//remote addr
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
while (fgets(buf, MAXLINE, stdin) != NULL)
{
//write(sockfd,buf,strlen(buf));
n = sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&servaddr,sizeof(servaddr));
if (n == -1)
perr_exit("sendto error");
//
n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0);
if (n == -1)
perr_exit("recvfrom error");
Write(STDOUT_FILENO, buf, n);
}
Close(sockfd);
return 0;
}
//write(sockfd,buf,count)
//send(sockfd,buf,count,flag)
//ssize_t send(int sockfd, const void *buf, size_t
//nbytes, int flags);
//sendto(sockfd,buf,count,flag,struct
/*ssize_t sendto(int sockfd, const void *buf, size_t
nbytes, int flags,
const struct sockaddr *destaddr,
socklen_t destlen);
*/
/*
read(sockfd,buf,count)
recv(sockfd,buf,count,flags)
recvfrom()
*/