static void initialize(char *type,char *bank_ip,int *bank_port,Message *send_Msg,ST_PACK *rPack,char *send_buf) { get_ip_port(bank_ip,bank_port,rPack); //银行服务器IP和银行服务器Port getdata(send_Msg,rPack); //获取数据 strcpy(send_Msg->txcode,type); //初始化发送数据交易代码 struct2str(send_Msg,send_buf); //拼装 print_send_msg(send_Msg); // 打印发送报文 }
void recv_data(u32 *ip, u16 *port, char *buf, u16 *buf_len) { char tmp[10], c; u16 len; u8 fd; u16 i; struct ip_port_map *map; while (1) { c = bus_recieve(); if (c == '+') { msleep(20); } else if (c == '\0') { msleep(20); continue; } else { msleep(1); continue; } /* read IPD */ for (i = 0; i < 4; i++) tmp[i] = bus_recieve(); if (strncmp(tmp, "IPD,", 4) != 0) continue; /* read fd */ for (i = 0; i < 2; i++) tmp[i] = bus_recieve(); //fd is from '0' to '9' fd = tmp[0] - '0'; /* get length */ len = get_len(tmp); for (i = 0; i < len; i++) buf[i] = bus_recieve(); break; } map = get_ip_port(fd); *ip = map->ip; *port = map->remote_port; *buf_len = len; }
static int do_net_open(char *iface) { int s, port; char ip[16]; struct sockaddr_in s_in; port = get_ip_port(iface, ip, sizeof(ip)-1); if (port == -1) return -1; s_in.sin_family = PF_INET; s_in.sin_port = htons(port); if (!inet_aton(ip, &s_in.sin_addr)) return -1; if ((s = socket(s_in.sin_family, SOCK_STREAM, IPPROTO_TCP)) == -1) return -1; printf("Connecting to %s port %d...\n", ip, port); if (connect(s, (struct sockaddr*) &s_in, sizeof(s_in)) == -1) { close(s); printf("Failed to connect\n"); return -1; } if (handshake(s) == -1) { close(s); printf("Failed to connect - handshake failed\n"); return -1; } printf("Connection successful\n"); return s; }
/************************************************ * 功能:初始化socket_client * 返回值:返回socket描述符 * * * *********************************************/ int init_socket_client(char *confname) { int sockfd; char ip[15]; int port; struct sockaddr_in servaddr; if(get_ip_port(confname, ip, &port) < 0) { printf("get_ip_port error\n"); return -1; } printf("ip is %s ", ip); printf("port is %d\n", port); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) { printf("create sockfd error\n"); return -1; } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); inet_pton(AF_INET, ip, &servaddr.sin_addr); if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { printf("connect server error\n"); return -1; } printf("start client!\n"); return sockfd; }