标签:person tin while [1] memset 结构 argc 代码实现 c语言
1.废话不多说,本文介绍的是Linux下C语言实现TCP传递结构体数据:
a.TCP Server一直在等待接收数据
b.TCP Client发送结构体数据
c.TCP Server接收数据并解析出来
2.结构体介绍:
a.实际上要发送的结构体是:
struct TCP_STRUCT_DATA
{
int m_cmd;//命令 可以用宏定义或者枚举区分不同的命令
int m_data_len;//要发的数据的长度
char *data;//要发的数据
}DATA_SEND, *PDATA_SEND;
b.char *data是一个指针,是无法发过去的,故而拆分为数据头跟数据部分发送,数据头就是一个结构体,包含命令及将要发送的数据的长度。即是:
struct TCP_STRUCT_DATA
{
int m_cmd;//命令 可以用宏定义或者枚举区分不同的命令
int m_data_len;//要发的数据的长度
//char *data;//要发的数据
}DATA_SEND, *PDATA_SEND;
3.代码实现
/*
* =====================================================================================
*
* Filename: struct_server.c
*
* Description:
*
* Version: 1.0
* Created: 03/05/2017 12:11:01 AM
* Revision: none
* Compiler: gcc struct_server.c -o struct_server
*
* Author: LI JUN LIANG
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#define SERVER_PORT 6666
#define MAX_CONNETCION_COUNT 20 //最大连接数
#define BUFFER_SIZE 1024
#define MAX_SIZE_OF_FILE_NAME 512
#define DEBUG_LOG 0
struct TCP_STRUCT_DATA
{
int m_cmd;
int m_data_len;
}DATA_SEND, *PDATA_SEND;
struct TCP_STRUCT_DATA
{
int m_cmd;//命令 可以用宏定义或者枚举区分不同的命令
int m_data_len;//要发的数据的长度
//char *data;//要发的数据
}DATA_SEND, *PDATA_SEND;
void parse_xml(char *back_str, const char *xml_str, const char *pre_str, const char *suf_str)
{
if(DEBUG_LOG)
printf("####L(%d) xml_str:%s pre_str:%s suf_str:%s \n", __LINE__, xml_str, pre_str, suf_str);
int offset = strlen(pre_str);
char *ptr_first = strstr(xml_str, pre_str);
char *ptr_end = strstr(xml_str, suf_str);
if((ptr_first != NULL) && (ptr_end != NULL))
{
ptr_first = ptr_first + offset;
strncpy(back_str, ptr_first, (ptr_end - ptr_first));
if(DEBUG_LOG)
printf("####L(%d) xml data back_str:%s\n", __LINE__, back_str);
}
}
int main(int argc, char *argv[])
{
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);//允许任何IP连接server
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
int server_socket = socket(PF_INET,SOCK_STREAM,0);//创建socket套接字
if( server_socket < 0)
{
printf("####L(%d) create socket failed!",__LINE__);
exit(1);
}
if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))//绑定端口
{
printf("####L(%d) bind port : %d failed!", __LINE__,SERVER_PORT);
exit(1);
}
if ( listen(server_socket, MAX_CONNETCION_COUNT) )//监听
{
printf("####L(%d) server listen failed!",__LINE__);
exit(1);
}
while (1) //服务器端一直运行
{
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
int new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);
if ( new_server_socket < 0)
{
printf("####L(%d) server accept failed!\n",__LINE__);
break;
}
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
length = recv(new_server_socket,buffer,BUFFER_SIZE,0);
if (length < 0)
{
printf("####L(%d) server recieve data failed!\n",__LINE__);
break;
}
/*关键代码*/
struct TCP_STRUCT_DATA struct_data;
memset(&struct_data,0x0,sizeof(struct_data));
memcpy(&struct_data,buffer,sizeof(buffer));//把结构体的数据通过memcpy的方式拷贝到struct_data中
printf("####L(%d) cmd:%d data_len:%d\n",__LINE__,struct_data.m_cmd,struct_data.m_data_len);
if(struct_data.m_data_len>0)//m_data_len>0 则仍有数据要接收
{
printf("####L(%d) going to recv data...\n",__LINE__);
length = recv(new_server_socket,buffer,struct_data.m_data_len,0);
if (length < 0)
{
printf("####L(%d) server recieve data failed!\n",__LINE__);
break;
}
printf("####L(%d) data:%s\n",__LINE__,buffer);//打印要接收的数据部分
//解析xml
char name[16] = {0};
char age_tmp[8] = {0};
parse_xml(name,buffer,"<name>","</name>");
parse_xml(age_tmp,buffer,"<age>","</age>");
if(NULL!=name)
{
printf("####L(%d) name:%s, age:%d\n",__LINE__,name,atoi(age_tmp));
}
else
{
printf("####L(%d) parse xml err!\n",__LINE__);
}
}
//关闭与客户端的连接
close(new_server_socket);
usleep(100*1000);
}
//关闭监听用的socket
close(server_socket);
return 0;
}
//end struct_server.c
/*
* =====================================================================================
*
* Filename: struct_client.c
*
* Description:
*
* Version: 1.0
* Created: 03/05/2017 14:13:24 AM
* Revision: none
* Compiler: gcc struct_client.c -o struct_client
*
* Author: LI JUN LIANG
* Organization:
*
* =====================================================================================
*/
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SERVER_PORT 6666
#define BUFFER_SIZE 1024
#define NAME_DATA "<person><name>%s</name><age>%d</age></person>"
struct TCP_STRUCT_DATA
{
int m_cmd;
int m_data_len;
};
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("####L(%d) usage: ./%s serverIpAddress\n",__LINE__,argv[0]);
exit(1);
}
struct sockaddr_in client_addr;
bzero(&client_addr,sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htons(INADDR_ANY);
client_addr.sin_port = htons(0); //0:自动分配一个空闲端口
int client_socket = socket(AF_INET,SOCK_STREAM,0);//创建socket套接字
if( client_socket < 0)
{
printf("####L(%d) create socket failed!\n",__LINE__);
exit(1);
}
if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr)))//绑定
{
printf("####L(%d) client bind port failed!\n",__LINE__);
exit(1);
}
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
if(0 == inet_aton(argv[1],&server_addr.sin_addr)) //服务器的IP地址来自程序的参数
{
printf("####L(%d) Server IP Address Error!\n",__LINE__);
exit(1);
}
server_addr.sin_port = htons(SERVER_PORT);
socklen_t server_addr_length = sizeof(server_addr);
if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printf("####L(%d) Can Not Connect To %s!\n",__LINE__,argv[1]);
exit(1);
}
char name[32] = "Li Ming";
int age = 18;
char data_tmp[512] = {0};
char buffer[BUFFER_SIZE];
bzero(buffer,BUFFER_SIZE);
struct TCP_STRUCT_DATA struct_data;
memset(&struct_data,0x0,sizeof(struct_data));
sprintf(data_tmp,NAME_DATA,name,age);//要发送的数据部分内容
struct_data.m_cmd =1;//发生数据请求
struct_data.m_data_len =strlen(data_tmp);//数据部分真实的长度
//struct_data.m_data_len =sizeof(data_tmp);//512是错误的
/*关键部分*/
int send_len = sizeof(struct_data);
memcpy(buffer,&struct_data,send_len);
//向服务器发送buffer中的数据
int len = send(client_socket,buffer,send_len,0);//发数据头部分
if(len<0)
{
printf("####L(%d) send err...\n", __LINE__);//发生失败
}
else
{
//发数据部分
printf("####L(%d) send succeed send len[%d]...\n", __LINE__, len);
usleep(500*1000);//要休眠一下 否则第二次发过去的数据可能来不及接收到
len = send(client_socket,data_tmp,struct_data.m_data_len,0);
if(len<0)
{
printf("####L(%d) send err...\n", __LINE__);//发生失败
}
else
printf("####L(%d) send succeed send len[%d] data:%s...\n", __LINE__, len,data_tmp);
}
//关闭socket
close(client_socket);
return 0;
}
//end struct_client.c
标签:person tin while [1] memset 结构 argc 代码实现 c语言
原文地址:http://www.cnblogs.com/jllee/p/6668509.html