socket 发送接收结构体数据包
socket结构体传输时,字符串结构体成员要使用数组定义,如:
typedef struct
{
int id;
char info[100];
}Data_info;
不能使用指针,send发送的是一块连续的内存,结构体带指针的不能直接发送,因为指针传递到接收端就变成野指针,指针的话实际上结构体内存中存的只是个指针,即是个32位的unsigned int值,但要是数组的话,那么就是一块内存。
typedef struct
{
int id;
char *info; //这样是错的
}Data_info;
备注:sizeof(Data_info)==4,说明数组在结构体存的是地址,但是传输时是一整段内存。
demo测试程序:
client:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXLINE 4096
typedef struct
{
int id;
char info[100];
}Data_info;
int main(int argc, char** argv)
{
int sockfd, n;
char recvline[4096], sendline[4096];
struct sockaddr_in servaddr;
Data_info *datainfo;
char info0[100] = "this is test!";
datainfo = (Data_info *)malloc(sizeof(Data_info));
datainfo->id = 9;
memcpy(datainfo->info, info0, sizeof(info0));;
//datainfo->info = info0;
if( argc != 2){
printf("usage: ./client <ipaddress>\n");
exit(0);
}
if( (sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
printf("send msg to server: \n\n");
// fgets(sendline, 4096, stdin);
printf("send msg total00: %d\n", sizeof(datainfo));
printf("send msg id: %d\n", datainfo->id);
printf("send msg info: %s\n\n", datainfo->info);
if( send(sockfd, (void *)datainfo, 100, 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
free(datainfo);
datainfo = NULL;
close(sockfd);
exit(0);
}
server.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<linux/tcp.h>
#include <pthread.h>
#include<unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAXLINE 4096
typedef struct
{
int id;
char info[100];
}Data_info;
void *client_sock_recv_proc(void * arg);
void* client_listen_proc(void *arg);
static Data_info *datainfo;
int main(void)
{
pthread_t tid;
int ret;
ret = pthread_create(&tid, NULL, client_listen_proc, NULL);
pthread_join(tid, NULL);
if(ret == 0)
{
printf("create thread success!\n");
}
else
{
printf("create thread faild!\n");
return -1;
}
}
void* client_listen_proc(void *arg)
{
int listenfd, connfd, ret, n;
int opt = 1;
int flags = 0;
int nodelay = 1;
int select_ret = 0;
int bReuseaddr = 1;
int bDontLinger = 1;
char peerip[18];
char buff[4096];
struct timeval timeout;
fd_set fdr;
struct sockaddr_in servaddr, addr;
socklen_t addr_len;
pthread_t tid = 0;
//pthread_detach(pthread_self());
if( (listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 )
{
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
goto EXIT;
}
if ( -1 == setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(const void *)&opt,sizeof(opt)) )
{
printf("setsockopt failed...\n");
}
setsockopt(listenfd,IPPROTO_TCP,TCP_NODELAY,(const char*)&nodelay,sizeof(int));
flags = fcntl(listenfd, F_GETFL);
if ( fcntl(listenfd,F_SETFL, flags | O_NONBLOCK) == -1 )
{
printf("set socket is failed...\n");
goto EXIT;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6666);
if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
{
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
goto EXIT;
}
if( listen(listenfd, 10) == -1)
{
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
goto EXIT;
}
while(1)
{
FD_ZERO(&fdr);
FD_SET(listenfd, &fdr);
timeout.tv_sec = 3;
timeout.tv_usec = 0;
select_ret = select(listenfd + 1, &fdr,NULL, NULL,&timeout);
switch(select_ret)
{
case 0: //超时处理
printf("accept timeout\n");
break;
case -1: //出错处理
printf("select listen error\n");
goto EXIT;
default:
memset(&addr, 0, sizeof(struct sockaddr_in));
addr_len = sizeof(struct sockaddr);
if( (connfd = accept(listenfd, (struct sockaddr*)&addr, &addr_len)) == -1)
{
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
}
else
{
memset(&addr, 0, addr_len);
getpeername(connfd, (struct sockaddr *)&addr, &addr_len);
printf("IP: %d\n", addr.sin_addr.s_addr);
printf("address:%s:%d\n\n",inet_ntop(AF_INET, &addr.sin_addr, peerip, sizeof(peerip)), ntohs(addr.sin_port));
datainfo = (Data_info *)malloc(sizeof(Data_info));
setsockopt(connfd, IPPROTO_TCP,TCP_NODELAY,(char*)&nodelay,sizeof(int));
setsockopt(connfd,SOL_SOCKET ,SO_REUSEADDR,(const char*)&bReuseaddr,sizeof(int));
setsockopt(connfd,SOL_SOCKET,SO_LINGER,(const char*)&bDontLinger,sizeof(int));
ret = pthread_create(&tid, NULL, client_sock_recv_proc,(void*)connfd);
if ( ret == 0 )
{
printf("create thread success sock:%d\n", connfd);
}
else
{
printf("create thread failed %d\n", ret);
}
}
break;
}
}
EXIT:
if ( listenfd != -1 )
{
close(listenfd);
printf("close %d sock \n", listenfd);
listenfd = -1;
}
return NULL;
}
void *client_sock_recv_proc(void * arg)
{
int sock = (int)arg;
int n;
printf("create thread success sock9999:%d\n", sock);
pthread_detach(pthread_self());
n = recv(sock, datainfo, 200, 0);
printf("\n================\n");
printf("recv msg from client id333===: %d\n", datainfo->id);
printf("recv msg from client info333===: %s\n", datainfo->info);
close(sock);
printf("close recv\n");
return NULL;
}