client:
#include <stdio.h>
#include "unp.h"
int main(int argc,char** argv)
{
int sockfd,n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;
if(argc !=2)
{
printf("usage: a.out <IP address>\n");
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("socket erro\n");
exit(0);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port =htons(3000);
if(inet_pton(AF_INET, argv[1],&servaddr.sin_addr)<=0)
{
printf("inet_pton error for %s\n", argv[1]);
exit(0);
}
if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))<0)
{
printf("sockfd = %d\n", sockfd);
printf("addr = %s\n", argv[1]);
printf("connect erro\n");
exit(0);
}
while((n = read(sockfd, recvline, MAXLINE))>0)
{
recvline[n]=0;
printf("read length = %d\n", n);
if(fputs(recvline,stdout)==EOF)
printf("fputs error");
}
if(n <0)
printf("read error\n");
exit(0);
}
server:
#include "unp.h"
#include <time.h>
int main(int argc,char** argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buffer[MAXLINE];
time_t ticks;
listenfd = Socket(AF_INET, SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr =htonl(INADDR_ANY);
servaddr.sin_port =htons(3000);
printf("linstenfd = %d\n", listenfd);
Bind(listenfd,(SA *)&servaddr,sizeof(servaddr));
Listen(listenfd, LISTENQ);
printf("listening...\n");
for(;;)
{
connfd = Accept(listenfd,(SA *)NULL,NULL);
printf("accept...\n"); ticks=time(NULL);
snprintf(buffer,sizeof(buffer),"%.24s\r\n", ctime(&ticks));
printf("time = %s", buffer);
Write(connfd, buffer, strlen(buffer));
Close(connfd);
}
}
compile on ubuntu 10.10
use some lib file from the resources dowload from the author's website, need change something....