日期:2014-05-16 浏览次数:20889 次
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_IPADDR "192.168.1.89"
#define SERV_PORT 6000
#define QUIT_STR "quit"
int main(void)
{
int fd = -1;
int ret = -1;
struct sockaddr_in sin;
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0) {
perror("socket error");
return -1;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);
sin.sin_addr.s_addr = inet_addr(SERV_IPADDR);
do {
ret = connect(fd, (struct sockaddr *)&sin, sizeof(sin));
}while(ret < 0 && EINTR == errno);
if(ret < 0) {
perror("connect error");
exit(1);
}
char buf[BUFSIZ];
while(1) {
bzero(buf, BUFSIZ);
printf("Input string:\n");
fgets(buf, BUFSIZ-1, stdin);
buf[BUFSIZ-1] = '\0';
ret = write(fd, buf, strlen(buf));
if( ret < 0) {
perror("write error");
continue;
}
if(!strncasecmp(buf, QUIT