Chinaunix首页 | 论坛 | 博客
  • 博客访问: 206626
  • 博文数量: 32
  • 博客积分: 306
  • 博客等级: 二等列兵
  • 技术积分: 337
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-15 10:02
文章分类
文章存档

2013年(21)

2011年(11)

我的朋友

分类: LINUX

2013-01-29 10:54:57

linux下send函数发送结构体

llinux下send函数发送结构体与发送字符串相似,只需在服务器端定义一个与客户端同类型的结构体即可。
客户端代码:
#include
#include
#include
#include
#include
#include
#include
#include

int main(){
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result; 
      //定义结构体  
    struct send_info  {  
        char info_from[20]; //发送者ID  
        char info_to[20]; //接收者ID         
        int info_length; //发送的消息主体的长度  
        char info_content[1024]; //消息主体  
     };        
    struct send_info info1;    //向服务器发送的结构体 
    struct send_info info2;    //接收服务器传送结构体
    memset(&info1,'\0',sizeof(struct send_info));     //清空缓存        
/******************初始化结构体******************/ 
    strcpy(info1.info_from,"abc");  
    strcpy(info1.info_to,"def"); 
    info1.info_length=1024;  
    strcpy(info1.info_content,"this string is sended to the server!");   
    sockfd = socket (AF_INET, SOCK_STREAM, 0);    //AF_UNIX为UNIX内部的文件系统套接字
  /**************根据服务器的设置情况给这个套接字起个名字*************/  
    address.sin_family = AF_INET; 
    address.sin_addr.s_addr = inet_addr("127.0.0.1");//表示本机 
    address.sin_port = htons(9734);//端口号 
    len = sizeof(address);
  /*****客户端连接服务器,调用connect在一个未命名套接字和服务器的监听套接字之间建立一个连接*****/  
    result = connect(sockfd, (struct sockaddr *)&address, len);        
    if(result == -1) {
        perror("oops: client1");exit(1);  
    }       
     printf(
            "client ID= % s\n   server  ID=%s\n   information  length  =%d\n  the content  is %s\n",  info1.info_from,
                        info1.info_to,info1.info_length,info1.info_content);        
/******************对sockfd进行读写*****************/
    send(sockfd, &info1,sizeof(struct send_info),0);//发送结构体至服务器
    memset(&info2,'\0',sizeof(struct send_info)); //清空缓存,准备接收服务器发送回的结构体
    recv(sockfd, &info2,sizeof(struct send_info),0);//接收
    printf ("client ID=% s\n  server ID=% s\n  information length=% d\n  the  content   is% s",  info2.info_from,info2.info_to,
            info2.info_length,info2.info_content);//显示结构体//
    sleep(1);
    close(sockfd);
    exit(0);
}
 
 
 
服务器代码:
 
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(){
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;  
 /********************定义结构体**********************/ 
    struct recv_info  {  
        char info_from[20]; //发送者ID  
        char info_to[20]; //接收者ID        
        int info_length; //发送的消息主体的长度 
        char info_content[1024]; //消息主体  
    };   
    struct recv_info info1;
    memset(&info1,'\0',sizeof(struct recv_info));//清空内存,防止出现乱码。
   
    //为服务器创建一个命名套接字
    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);       
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(9734);
    server_len = sizeof(server_address);
    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);//捆绑至规定地址和端口
    //创建一个连接队列,开始等待客户的到来
    listen(server_sockfd, 5);//队列5个字节长
    while(1){
        printf("server waiting...\n");
        //接受一个连接,accept系统调用会等到有客户程序试图连接到由socket参数指定的套接字时才返回
        //accept函数将创建一个新的套接字来与该客户进行通信,返回的是与之对应的文件描述符
        client_len = sizeof(client_address);
        //创建新的套接字描述符/*****对client_sockfd套接字上的客户进行读写*****/
        client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); 
        recv(client_sockfd,&info1, sizeof(struct recv_info),0);//接收结构体
        printf("client ID= %s\n server ID=%s\n information length =%d\n the content is %s\n", info1.info_from,info1.info_to,info1.info_length,info1.info_content);//显示结构体内容
        send(client_sockfd,&info1, sizeof(struct recv_info),0);//发送回客户端
        close(client_sockfd);
     }
}
阅读(2624) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~