//server端**
#include#include#include#include#include#include#include//使用不定長的結構體傳送資料的關鍵在於:結構體變數必須分配到堆中,而不是棧中
//即只能用malloc或者new來給結構體分配空間
typedef struct node
node;
int main()
server_addr.sin_family = af_inet;
server_addr.sin_port = htons(5050);
server_addr.sin_addr.s_addr = inaddr_any;
bzero(&(server_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)) == -1)
if(listen(sockfd,100) == -1)
printf("server is listening:\n");
while(1)
int recvsize = 0;//data total length
if(recv(new_fd,(char *)&recvsize,sizeof(recvsize),0) == -1)
int leftsize = sizeof(char) * (recvsize - sizeof(int));
char *databuf = (char *)malloc(leftsize);
memset(databuf,0,leftsize);
if(recv(new_fd,databuf,leftsize,0) == -1)
close(new_fd);
node *p = (node *)malloc(sizeof(char) * recvsize);
p->nodesize = recvsize;
memcpy((char *)(&p->bufsize),databuf,leftsize);
printf("nodesize = %d\n",p->nodesize);
printf("bufsize = %d\n",p->bufsize);
printf("buf = %s\n",p->buf);
} return 0;
}
//client端**
#include#include#include#include#include#include#include#includetypedef struct node
node;
int main()
server_addr.sin_family = af_inet;
server_addr.sin_port = htons(5050);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(server_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)) == -1)
printf("client is connecting\n");
const char *tmp = "this is a test!";
int tmplen = strlen(tmp);
node *databuf = (node *)malloc(sizeof(node) + tmplen + 1);
databuf->nodesize = sizeof(node) + tmplen + 1;
databuf->bufsize = tmplen;
memset(databuf->buf,0,tmplen + 1);
memcpy(databuf->buf,tmp,tmplen + 1);
printf("nodesize = %d\n",databuf->nodesize);
printf("bufsize = %d\n",databuf->bufsize);
printf("buf = %s\n",databuf->buf);
if(send(sockfd,(char *)databuf,databuf->nodesize,0) == -1)
close(sockfd);
return 0;
}
sqlalchemy的優雅 不定字段 不定長查詢
假設 tbuser 為乙個表模型,session 為與資料庫的會話 乙個普通的查詢大概是這個樣 session.query tbuser filter tbuser.name xiaokeai 這是為已知要查 tbuser 內的 name 欄位而設計的查詢,有這麼乙個需求,查某乙個字段裡面包含有字母...
資料結構 不定長順序棧
1 棧 stack 又名堆疊,它是一種運算受限的線性表。其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向乙個棧插入新元素又稱作進棧 入棧或壓棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素 從乙個棧刪除元素又稱作出棧或退棧,它是把棧頂元素刪除掉,使其...
資料結構 不定長順序表
順序表是在計算機記憶體中以陣列的形式儲存的線性表,所以順序表的儲存結構和陣列非常類似,而它最顯要的特點就是邏輯位址和實體地址都相連。alterlist.h pragma once pragma once是乙個比較常用的c c 預處理指令,只要在標頭檔案的最開始加入這條預處理指令,就能夠保證標頭檔案只...