为什么80%的码农都做不了架构师?>>>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_TYPE 188
#define SERVER_INST 18
#define SERVER_CLIENT 2
#define BUF_SZ 10
#define SERVER_FILE "serverdata"
#define CLIENT_FILE "clientdata"
#define FILE_NAME_LEN 50
#define PICE_BUF_LEN 1000
struct fileinfo{
unsigned char uchfilename[FILE_NAME_LEN];
unsigned int uifile_total_len;
unsigned int uifile_already_len;
unsigned int uifile_every_len;
unsigned int uifile_picenumber;
unsigned int uifile_retry;
unsigned int uisend_finish;
unsigned char uchDatabuf[BUF_SZ];
};
struct server_rly{
unsigned int uisend_needresend;
};
int wait_for_server(struct tipc_name* name,int wait);
int tipc_client(int a );
int main(int argc, char **argv)
{
if(SERVER_CLIENT == 1)
{
unsigned char sbuff[128] = {0};
sprintf(sbuff, "ifconfig eth1-11 up");
system(sbuff);
memset(sbuff, 0, 128);
sprintf(sbuff, "sudo tipc-config -netid=1111 -addr=1.1.1 -be=eth:eth1-11");
system(sbuff);
if(!CWErr(CWCreateThread(&thread_server_TipcMsg, tipc_server, NULL, 0)))
{
WSMLog(L_EMERG, "Thread[thread_server_TipcMsg] initialize failed.\n");
exit(-1);
}
}
else if(SERVER_CLIENT == 2)
{
unsigned char cbuff[128] = {0};
sprintf(cbuff, "ifconfig eth0-2 up");
system(cbuff);
memset(cbuff, 0, 128);
sprintf(cbuff, "sudo tipc-config -netid=1111 -addr=1.1.2 -be=eth:eth0-2");
system(cbuff);
if(!CWErr(CWCreateThread(&thread_client_TipcMsg, tipc_client, NULL, 0)))
{
WSMLog(L_EMERG, "Thread[thread_server_TipcMsg] initialize failed.\n");
exit(-1);
}
}
else
{
printf("there is no tipc thread ....\n");
}
while(1)
{
tipc_client(10);
sleep(10000);
}
return 0;
}
int wait_for_server(struct tipc_name* name,int wait)
{
struct sockaddr_tipc topsrv;
struct tipc_subscr subscr = {{name->type,name->instance,name->instance},
wait,TIPC_SUB_SERVICE,{}};
struct tipc_event event;
int sd = socket (AF_TIPC, SOCK_SEQPACKET,0);
assert(sd > 0);
memset(&topsrv,0,sizeof(topsrv));
topsrv.family = AF_TIPC;
topsrv.addrtype = TIPC_ADDR_NAME;
topsrv.addr.name.name.type = TIPC_TOP_SRV ;
topsrv.addr.name.name.instance = TIPC_TOP_SRV ;
if (0 > connect(sd,(struct sockaddr*)&topsrv,sizeof(topsrv))){
perror("failed to connect to topology server");
return 1;
}
if (send(sd,&subscr,sizeof(subscr),0) != sizeof(subscr)){
perror("failed to send subscription");
return 1;
}
if (recv(sd,&event,sizeof(event),0) != sizeof(event)){
perror("Failed to receive event");
return 1;
}
if (event.event != TIPC_PUBLISHED){
printf("Server %u,%u not published within %u [s]\n",
name->type,name->instance,wait/1000);
return 1;
}
printf("\n****** close(sd)******\n");
close(sd);
return 0;
}
int tipc_client(int a)
{
int iCount = 0;
struct sockaddr_tipc server_addr;
int sd = socket (AF_TIPC, SOCK_RDM, 0);
FILE *fp = NULL;
int file_len = 0;
char chEof;
char chmsg[] = "1234567890";
char buf[sizeof(struct server_rly) + 1] = {0};
struct fileinfo fileinfo;
struct server_rly *serv_rly;
memset(fileinfo.uchDatabuf, 0, BUF_SZ);
memset(fileinfo.uchfilename, 0, FILE_NAME_LEN);
memcpy(fileinfo.uchfilename, CLIENT_FILE, sizeof(CLIENT_FILE));
printf("****** fp = fopen(); ******\n\n");
fp = fopen(CLIENT_FILE, "w+");
if(fp == NULL)
{
printf("open file : %s failed \n",CLIENT_FILE);
}
else
{
printf("open file : %s success \n",CLIENT_FILE);
for(iCount = 0; iCount < a; iCount++)
{
fputs(chmsg, fp);
}
fseek(fp, 0, SEEK_END);
file_len=ftell(fp);
fseek(fp,0,SEEK_SET);
fileinfo.uifile_total_len = file_len;
fileinfo.uifile_every_len = BUF_SZ;
fileinfo.uifile_picenumber = 0;
fileinfo.uifile_retry = 0;
fileinfo.uisend_finish = 0;
fileinfo.uifile_already_len = 0;
rewind(fp);
}
printf("****** fp = fopen(); end******\n\n");
server_addr.family = AF_TIPC;
server_addr.addrtype = TIPC_ADDR_NAME;
server_addr.addr.name.name.type = SERVER_TYPE;
server_addr.addr.name.name.instance = SERVER_INST;
server_addr.addr.name.domain = 0;
#if 1
if(0 == wait_for_server(&server_addr.addr.name.name,10000))
{
printf("******server ok*\n\n");
}
else
{
printf("******server not catch*\n\n");
}
#endif
for(;(chEof = fgetc(fp)) != EOF;)
{
fseek(fp, -1, SEEK_CUR);
fgets(fileinfo.uchDatabuf, BUF_SZ + 1, fp);
//printf("sendto*\n\n");
if (0 > sendto(sd,
(char*)&fileinfo,
sizeof(fileinfo),
0,
(struct sockaddr*)&server_addr,
sizeof(server_addr)))
{
perror("Client: Failed to send");
return 1;
}
else
{
fileinfo.uifile_already_len = fileinfo.uifile_picenumber * BUF_SZ;
fileinfo.uifile_picenumber++;
}
//printf("recv*\n\n");
if (0 >= recv(sd,buf,sizeof(buf), 0))
{
perror("Unexepected response");
return 1;
}
else
{
serv_rly = (struct server_rly *)&buf;
if(serv_rly->uisend_needresend == 1)
{
fseek(fp, -(BUF_SZ + 1), SEEK_CUR);
fileinfo.uifile_picenumber--;
fileinfo.uifile_already_len = fileinfo.uifile_picenumber * BUF_SZ;
}
}
}
fileinfo.uisend_finish = 1;
if (0 > sendto(sd, (char*)&fileinfo, sizeof(fileinfo), 0, (struct sockaddr*)&server_addr, sizeof(server_addr)))
{
perror("Client: Failed to send");
return 1;
}
fclose(fp);
sleep(5);
printf("\n****** TIPC client hello program finished ******\n");
close(sd);
return 0;
}