为了成为自由自在的人而奋斗!
分类: 系统运维
2010-10-26 09:24:35
#include
#include
#include
#include
#include
#include
#include
#define MAXSIZE (8 * 4096)
int main(int argc, char *argv[])
{
int sockfd, ret;
unsigned long opt;
struct sockaddr_un serv_addr;
char recvmsg[MAXSIZE], sendmsg[MAXSIZE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket error");
return -1;
}
opt = 0;
/*
ret = ioctl(sockfd, FIONBIO, (unsigned long)&opt);
if (ret < 0) {
close(sockfd);
return -1;
}
*/
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy((char *)serv_addr.sun_path, "e:\\sockfile");
ret = 0;
if ((ret = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) < 0) {
perror("connect err");
return -1;
}
printf("connect to server success!\n");
while (1) {
memset(sendmsg, 0, sizeof(sendmsg));
memset(recvmsg, 0, sizeof(recvmsg));
printf("client:\n");
gets(sendmsg);
if (write(sockfd, sendmsg, strlen(sendmsg)) < 0) {
printf("send msg failed!\n");
break;
}
/*
if (send(sockfd, sendmsg, strlen(sendmsg) + 1, 0) < 0) {
printf("send msg failed!\n");
break;
}
*/
if (!strcmp(sendmsg, "quit")) {
break;
}
if (!strcmp(sendmsg, "quit server")) {
break;
}
if (read(sockfd, recvmsg, MAXSIZE) < 0) {
printf("recv msg failed!\n");
break;
}
/*
if (recv(sockfd, recvmsg, MAXSIZE, 0) < 0) {
printf("recv msg failed!\n");
break;
}
*/
printf("server:\n%s\n", recvmsg);
}
printf("\nclose connection!\n\n");
close(sockfd);
return 0;
}