Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208206
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 824
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-12 21:40
个人简介

只有今天的埋头,才有明天的出头。

文章分类

全部博文(80)

文章存档

2014年(80)

我的朋友

分类: LINUX

2014-12-21 21:16:43

/* client.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define PORT  4321
#define BUFFER_SIZE 1024


int main(int argc,char *argv[])
{
struct sockaddr_in server_addr;
int sockfd,sendbytes;
char buf[BUFFER_SIZE];
struct hostent *host;


/* 指定输入参数为3个,否则出错 */
if(argc != 3)
{
printf("Usage: ./clinet IP address Text\n");
exit(1);
}


/* 地址解析函数 */
if((host = gethostbyname(argv[1]))==NULL)
{
perror("gethostbyname");
exit(1);
}
memset(buf,0,sizeof(buf));
sprintf(buf,"%s",argv[2]);


/* 建立socket连接,IPv4协议,字节流套接字 */
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
printf("Socket id = %d\n",sockfd);

/* 初始化sockaddr_in结构体 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);


/* 调用connect函数主动发起对服务器的连接 */
if((connect(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)))==-1)
{
perror("connect");
exit(1);
}
printf("Connect server success!\n");


/* 发送消息给服务器端 */
if((sendbytes = send(sockfd,buf,strlen(buf),0))==-1)
{
perror("send");
exit(1);
}

/* 关闭socket */
close(sockfd);
return 0;
}

+++++++++++++++++++++++++++++
/* server.c */
#include
#include
#include
#include
#include
#include
#include
#include


#define PORT  4321
#define BUFFER_SIZE 1024
#define MAX_QUE_CONN_NM 5


int main(void)
{
struct sockaddr_in server_addr,client_addr;
int sin_size,recvbytes;
int ser_fd,cli_fd;
char buf[BUFFER_SIZE];

/* 建立socket连接,IPv4协议,字节流套接字 */
if((ser_fd = socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
printf("Socket id = %d\n",ser_fd);

/* 初始化sockaddr_in结构体 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);


/* 绑定函数bind */
if(bind(ser_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
printf("Bind success!\n");


/* 调用listen函数,进行监听 */
if(listen(ser_fd,MAX_QUE_CONN_NM)==-1)
{
perror("listen");
exit(1);
}
printf("Listening......\n");


/* 调用accept函数,等待客户端的连接 */
if((cli_fd = accept(ser_fd,(struct sockaddr *)&client_addr,&sin_size))==-1)
{
perror("accept");
exit(1);
}
printf("Have client ready for connecting\n");

/* 调用recv函数接收客户端的请求 */
memset(buf,0,sizeof(buf));
if((recvbytes = recv(cli_fd,buf,BUFFER_SIZE,0))==-1)
{
perror("recv");
exit(1);
}
/* 将收到的信息(客服端发来的信息)打印出来 */
printf("Received a message:%s\n",buf);


/* 关闭socket */
close(ser_fd);
return 0;
}
+++++++++++++++++++++++++++

/* udp_client.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define PORT  8888
#define BUFFER_SIZE 1024


int main(int argc,char *argv[])
{
struct sockaddr_in server_addr;
int sockfd;
char buf[BUFFER_SIZE];
int sendbytes;


/* 指定输入参数为2个,否则出错 */
if(argc != 2)
{
printf("Usage: ./clinet Text\n");
exit(1);
}

memset(buf,0,sizeof(buf));
sprintf(buf,"%s",argv[1]);


/* 初始化sockaddr_in结构体 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(server_addr.sin_zero),8);

/* 建立socket连接,IPv4协议,数据报套接字 */
if((sockfd = socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket");
exit(1);
}
printf("Socket id = %d\n",sockfd);

/* 发送消息给服务器端 */
if((sendbytes = sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)&server_addr,sizeof(server_addr)))==-1)
{
perror("sendto");
exit(1);
}

/* 关闭socket */
close(sockfd);
return 0;
}

+++++++++++++++++++++++++++++

/* udp_server.c */
#include
#include
#include
#include
#include
#include
#include
#include


#define PORT  8888
#define BUFFER_SIZE 1024


int main(void)
{
struct sockaddr_in server_addr,client_addr;
int sockfd;
char buf[BUFFER_SIZE];
int ret,len;

/* 建立socket连接,IPv4协议,数据报套接字 */
if((sockfd = socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket");
exit(1);
}
printf("Socket id = %d\n",sockfd);

/* 初始化sockaddr_in结构体 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(server_addr.sin_zero),8);


len = sizeof(server_addr);


/* 绑定函数bind */
if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr))==-1)
{
perror("bind");
exit(1);
}
printf("Bind success!\n");


while(1)
{
/* 等待客户端发送过来的数据,一旦有数据就接收进来 */
if((ret = recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&server_addr,&len))==-1)
{
perror("recvfrom");
exit(1);
}
buf[len] = '0';
printf("The message is %s\n",buf);


if(strncmp(buf,"stop",4)==0)
{
printf("Stop to run!\n");
break;
}
/* 关闭socket */
close(sockfd);
exit(0);
}
return 0;
}




阅读(2203) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

suiyuemanbu2015-03-04 22:24:59

写的很好啊