先是建立服务器程序:
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 5858
#define BACKLOG 10
#define MAXSIZE 80
#define BUFSIZE 100
//单独的数据处理 就是简单的从client端接受数据, 然后又发给client端
void str_ser(int new_fd) {
char buf[MAXSIZE];
int n = 0;
while(1) {
if((n=recv(new_fd, buf, MAXSIZE, 0)) == 0) {
printf("error in recv!\n");
return ;
}
printf("server data is %s", buf);
send(new_fd, buf, n, 0);
bzero(buf, MAXSIZE);
}
}
int main() {
int socketfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
pid_t pid;
if((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error in socket function!\n");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero), 8);
if((bind(socketfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) < 0) {
printf("error in bind function!\n");
exit(1);
}
if(listen(socketfd, BACKLOG) < 0) {
printf("error in listen function!\n");
exit(1);
}
while(1) {
printf("start accept\n");
sin_size = sizeof(struct sockaddr_in);
if((new_fd=accept(socketfd, (struct sockaddr *)&their_addr ,&sin_size)) < 0) {
printf("error in accept() function!\n");
exit(1);
}
if((pid=fork())==0) {
close(socketfd);
str_ser(new_fd);
close(new_fd);
exit(0);
}else {
close(new_fd);
}
}
close(socketfd);
exit(0);
}
//下面是client端的程序: boxiang.blogbus.com
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSIZE 100
#define MYPORT 5858
extern int errno;
//单独处理数据的函数,发给服务器, 然后接受服务器的数据
void str_cli(int sockfd) {
char sends[MAXSIZE];
char recvs[MAXSIZE];
int n=0;
while(fgets(sends, MAXSIZE, stdin) != NULL) {
printf("client data is %s\n", sends);
send(sockfd, sends, strlen(sends), 0);
if((n=recv(sockfd, recvs, MAXSIZE, 0)) == 0) {
printf("error in recv !\n");
exit(1);
}
recvs[n] = 0;
printf("client recv data is %s\n", recvs);
//fputs(recvs, stdout);
bzero(recvs, n);
bzero(sends, MAXSIZE);
}
}
int main(int args, char *argv[]) {
int sockfd;
int ret;
struct servent *serv;
struct hostent *sh;
struct in_addr **addrs;
struct sockaddr_in ser_addr;
struct sockaddr_in addr;
char **addres;
char ip[]="127.0.0.1";
if(args != 2) {
printf("error in paramter!\n");
exit(1);
}
/*if((sh=gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr), AF_INET)) == NULL) {
printf("error in gethostbyaddr!\n");
exit(1);
}*/
/*if((serv=getservbyname(sh->h_name, NULL)) == NULL) {
printf("error in getservbyname!\n");
perror("getservbyname");
exit(1);
}*/
///////////////////首先通过服务器域名获得其IP地址,然后创建一个socket,调用connect函数与服务器建立连
//接, 要进行connect链接 首先要有服务器的ip和port端口,port首先可以自己定义,看看服务器端口,端口port定义是5858, 所以client端口port也就是5858,(说点小插曲,以前想通过gethostbyaddr,获得h->home, 肯定是可以的 但是hostent数据结构里面是没有port的,我就想通过getservbyname,因为servent里面有port这个属性,可是就是得不到 呵呵呵 真是笨啊 根本就得不到 getservent只能获得相应服务的对应端口) 地方必须说一下,我自己理解是gethostbyname是先在/etc/hosts文件里面去读取 你对应的域名,得到ip地址, 如果没有就是去电脑的dns里面去找域名相应的ip地址,而那个getservbyname是去/etc/services去读取相应服务的端口啊
if((sh=gethostbyname(argv[1]))==NULL) {
printf("error in gethostbyname !\n");
exit(1);
}
addrs = (struct in_addr **)sh->h_addr_list;
for(; (*addrs)!=NULL; addrs++) {
if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error in socket!\n");
exit(1);
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(MYPORT);
//memcpy(&(ser_addr.sin_addr),(struct in_addr *)ip, sizeof(struct in_addr));
printf("ip is :%s\n", inet_ntoa(**addrs));
memcpy(&(ser_addr.sin_addr), *addrs, sizeof(struct in_addr));
ret = connect(sockfd, (struct sockaddr*)&ser_addr, sizeof(struct sockaddr));
if(ret == 0) {
printf("is ok\n");
break;
}else {
printf("error in connect function!\n");
close(sockfd);
}
}
if((*addrs) == NULL) {
printf("error in found addr and port in this server!\n");
exit(1);
}
str_cli(sockfd);
close(sockfd);
exit(0);
}