转自:http://blog.csdn.net/quwu_bjut/article/details/3459051
Linux程序设计 Linux socket send and recevie structure
最近在开发一个Linux下的聊天软件,好久没有做C语言的开发了,感觉到很多东西已经生疏了,这下又碰到用Socket传递结构体的问题,google了一下,发现也有不少朋友遇到同样的问题,所以就打算写出自己的解决办法,跟大家分享。
Socket中的send函数可以发送字符串,但不能直接发送结构体,因此在发送端先把结构体转成字符串,然后用send发送,在接收端recv字符串,再转换成原先的结构体,这个就是解决问题的主要思路,实现中要注意的问题在下文阐述。
为了客户端之间能够互相通信,实现私聊,我采用服务器转发的方式,因此用户发送的每条消息中除了消息主体外,还必须包含有发送者、接收者ID等信息,如此采用结构体便是最佳的办法了。我定义的结构体如下:
struct send_info
{
char info_from[20]; //发送者ID
char info_to[20]; //接收者ID
int info_length; //发送的消息主体的长度
char info_content[1024]; //消息主体
};
发送端主要代码(为了简洁说明问题,我把用户输入的内容、长度等验证的代码去掉了):
struct send_info info1; //定义结构体变量
printf("This is client,please input message:");
//从键盘读取用户输入的数据,并写入info1.info_content
memset(info1.info_content,0,sizeof(info1.info_content));//清空缓存
info1.info_length=read(STDIN_FILENO,info1.info_content,1024) - 1;//读取用户输入的数据
memset(snd_buf,0,1024);//清空发送缓存,不清空的话可能导致接收时产生乱码,
//或者如果本次发送的内容少于上次的话,snd_buf中会包含有上次的内容
memcpy(snd_buf,&info1,sizeof(info1)); //结构体转换成字符串
send(connect_fd,snd_buf,sizeof(snd_buf),0);//发送信息
接收端主要代码:
struct send_info clt; //定义结构体变量
memset(recv_buf,'z',1024);//清空缓存
recv(fd,recv_buf,1024,0 );//读取数据
memset(&clt,0,sizeof(clt));//清空结构体
memcpy(&clt,recv_buf,sizeof(clt));//把接收到的信息转换成结构体
clt.info_content[clt.info_length]='';
//消息内容结束,没有这句的话,可能导致消息乱码或输出异常
//有网友建议说传递的结构体中尽量不要有string类型的字段,估计就是串尾符定位的问题
if(clt.info_content) //判断接收内容并输出
printf("nclt.info_from
is %snclt.info_to is %snclt.info_content is%snclt.info_length is
%dn",clt.info_from,clt.info_to,clt.info_content,clt.info_length);
//至此,结构体的发送与接收已经顺利结束了
例子:
服务端:
- 01.//*
- 02.//*Date:2012-07-03
- 03.//*Author :CplusHua
- 04.//*服务端发送结构体数据
- 05.//*www.it165.net
- 06.//*
- 07.#include <sys/types.h>
- 08.#include <sys/socket.h>
- 09.#include <netinet/in.h>
- 10.#include <arpa/inet.h>
- 11.#include <stdio.h>
- 12.#include <stdlib.h>
- 13.#include <string.h>
- 14.#include <unistd.h>
- 15.#include<signal.h>
- 16.#include<iostream>
- 17.using namespace std;
- 18.int main(void)
- 19.{
- 20.signal(SIGPIPE,SIG_IGN);
- 21.struct passwd
- 22.{
- 23.char pw_name[100]; // Username.
- 24.char pw_passwd[100]; // Pass<a href="" target="_blank" class="keylink">word</a>.
- 25.__uid_t pw_uid; // User ID.
- 26.__gid_t pw_gid; // Group ID.
- 27.char pw_gecos[100]; // Real name.
- 28.char pw_dir[100]; // Home directory.
- 29.char pw_shell[100]; // Shell program.
- 30.} ;
- 31.struct passwd p;
- 32.memset(&p,0x00,sizeof(struct passwd));
- 33.strcpy(p.pw_name,"testuserser");
- 34.strcpy(p.pw_passwd,"234234");
- 35.p.pw_gid=1000;
- 36.p.pw_uid=1012;
- 37.strcpy(p.pw_shell,"/usr/bin");
- 38.strcpy(p.pw_dir,"/home/");
- 39.char sendbuff[sizeof(struct passwd)];
- 40.memset(sendbuff,0,sizeof(sendbuff));
- 41.memcpy(sendbuff,&p,sizeof(struct passwd));
- 42.cout<<sendbuff<<"buff copy ok now!"<<endl;
- 43.struct sockaddr_in stSockAddr;
- 44.int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- 45.
- 46.if(-1 == SocketFD)
- 47.{
- 48.perror("can not create socket");
- 49.exit(EXIT_FAILURE);
- 50.}
- 51.
- 52.memset(&stSockAddr, 0, sizeof(struct sockaddr_in));
- 53.
- 54.stSockAddr.sin_family = AF_INET;
- 55.stSockAddr.sin_port = htons(5555);
- 56.stSockAddr.sin_addr.s_addr = INADDR_ANY;
- 57.
- 58.if(-1 == bind(SocketFD,(const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)))
- 59.{
- 60.perror("error bind failed");
- 61.close(SocketFD);
- 62.exit(EXIT_FAILURE);
- 63.}
- 64.
- 65.if(-1 == listen(SocketFD, 10))
- 66.{
- 67.perror("error listen failed");
- 68.close(SocketFD);
- 69.exit(EXIT_FAILURE);
- 70.}
- 71.cout<<"I Am Wating Client .............."<<endl;
- 72.for(;;)
- 73.{
- 74.int ConnectFD = accept(SocketFD, NULL, NULL);
- 75.if(0 > ConnectFD)
- 76.{
- 77.perror("error accept failed");
- 78.close(SocketFD);
- 79.exit(EXIT_FAILURE);
- 80.}
- 81.//注意此处的send的第一个参数是accept返回的描述
- 82.send(ConnectFD, &p, sizeof(p),0);
- 83.cout<<"数据发送成功!"<<endl;
- 84.cout<<"已经发送的数据"<<endl;
- 85.cout<<"用户名:"<<p.pw_name<<endl;
- 86.cout<<"密 码:"<<p.pw_passwd<<endl;
- 87.cout<<"G I D : "<<p.pw_gid<<endl;
- 88.cout<<"RealNa: "<<p.pw_gecos<<endl;
- 89.cout<<"文件夹: "<<p.pw_dir<<endl;
- 90.cout<<"权 限:"<<p.pw_shell<<endl;
- 91.shutdown(ConnectFD, SHUT_RDWR);
- 92.close(ConnectFD);
- 93.}
- 94.close(SocketFD);
- 95.return 0;
- 96.}
客户端:
- 01./*
- 02.*Date:2012-07-03
- 03.*Author :CplusHua
- 04.*客户端接收结构体
- 05.*www.it165.net
- 06.*/
- 07.#include <sys/types.h>
- 08.#include <sys/socket.h>
- 09.#include <netinet/in.h>
- 10.#include <arpa/inet.h>
- 11.#include <stdio.h>
- 12.#include <stdlib.h>
- 13.#include <string.h>
- 14.#include <unistd.h>
- 15.#include<iostream>
- 16.#include<signal.h>
- 17.using namespace std;
- 18.int main(void)
- 19.{
- 20.signal(SIGPIPE,SIG_IGN);//防止进程意外退出
- 21.//构造的结构体 demo
- 22.struct passwd
- 23.{
- 24.char pw_name[100]; // Username.
- 25.char pw_passwd[100]; // Pass<a href="" target="_blank" class="keylink">word</a>.
- 26.__uid_t pw_uid; // User ID.
- 27.__gid_t pw_gid; // Group ID.
- 28.char pw_gecos[100]; // Real name.
- 29.char pw_dir[100]; // Home directory.
- 30.char pw_shell[100]; // Shell program.
- 31.};
- 32.struct passwd p;
- 33.//将结构体清零
- 34.memset(&p,0,sizeof(struct passwd));
- 35.struct sockaddr_in stSockAddr;
- 36.int Res;
- 37.//套接字描述
- 38.int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- 39.if (-1 == SocketFD)
- 40.{
- 41.perror("cannot create socket");
- 42.exit(EXIT_FAILURE);
- 43.}
- 44.//清空
- 45.memset(&stSockAddr, 0, sizeof(struct sockaddr_in));
- 46.
- 47.stSockAddr.sin_family = AF_INET;
- 48.stSockAddr.sin_port = htons(5555);//服务器端口号
- 49.Res = inet_pton(AF_INET, "211.87.230.12", &stSockAddr.sin_addr);//服务器IP地址(转换为网络字节)
- 50.
- 51.if (0 > Res)
- 52.{
- 53.perror("error: first parameter is not a valid address family");
- 54.close(SocketFD);
- 55.exit(EXIT_FAILURE);
- 56.}
- 57.else if (0 == Res)
- 58.{
- 59.perror("char string (second parameter does not contain valid ipaddress");
- 60.close(SocketFD);
- 61.exit(EXIT_FAILURE);
- 62.}
- 63.
- 64.if (-1 == connect(SocketFD, (const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)))
- 65.{
- 66.perror("connect failed");
- 67.close(SocketFD);
- 68.exit(EXIT_FAILURE);
- 69.}
- 70.//定义一个与结构体相同大小的buffer
- 71.char buffer[sizeof(struct passwd)];
- 72./* perform read write operations ... */
- 73.memset(buffer,0x00,sizeof(buffer));
- 74.int readRes=read(SocketFD,buffer,sizeof(buffer));
- 75.if(readRes<0) cout<<"数据接收失败"<<endl;
- 76.
- 77.memcpy(&p,&buffer,sizeof(p));
- 78.cout<<"用户名:"<<p.pw_name<<endl;
- 79.cout<<"密 码:"<<p.pw_passwd<<endl;
- 80.cout<<"G I D : "<<p.pw_gid<<endl;
- 81.cout<<"RealNa: "<<p.pw_gecos<<endl;
- 82.cout<<"文件夹: "<<p.pw_dir<<endl;
- 83.cout<<"权 限:"<<p.pw_shell<<endl;
- 84.shutdown(SocketFD, SHUT_RDWR);
- 85.close(SocketFD);
- 86.return 0;
- 87.}
阅读(921) | 评论(0) | 转发(0) |