日期:2014-05-16 浏览次数:20625 次
#define MAXLINE 4096 int main() { int unfd; char line[MAXLINE + 1]; bzero(&unaddr, sizeof unaddr); unaddr.sun_family = AF_LOCAL; strcpy(unaddr.sun_path, "/tmp/sinfor"); #if 1 if ((unfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) err_sys("socket error\n"); #ifdef CLI if (connect(unfd, (SA*)&unaddr, sizeof unaddr) < 0) err_sys("connect error\n");//输出参数字符串然后exit printf("established\n"); recv(unfd, line, MAXLINE, 0); printf("%s\n", line); #elif defined(SERV) if (bind(unfd, (struct sockaddr*)&unaddr, sizeof unaddr) < 0) err_sys("tcp bind error\n"); if (listen(unfd, LISTENQ) < 0) err_sys("listen error\n"); //for(;;){ connfd = accept(unfd, (struct sockaddr*)&unaddr, &len); printf("established\n"); fgets(line, MAXLINE, stdin); send(unfd, line, strlen(line), 0); //} #endif printf("closing\n"); close(unfd); #endif }