📄 keyboard_send.c
字号:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#define LOCALHOST "localhost"#define TEST_HOST 7002//#define TEST_HOST 1000#define BUFFER_LEN 1024#ifdef CROSS#include <uip/uip_arp.h>#include <uip/uip_greth.h>static void send_key(void){ char ch; char buf[BUFFER_LEN]; ch = 'i'; /* 随便初始化一个值,第一次进入循环时不退出就可以 */ while('q' != ch) { printf("put q to exit>"); ch = getchar(); buf[0] = ch; buf[1] = '\0'; uip_send(buf, 2); printf(" Sock send the %c\n", ch); }}int main(void){ u16_t ipaddr[2]; uip_init(); uip_ipaddr(ipaddr, 192,168,0,153); /* while(1) { if (uip_connected()) { printf("has connected\n"); break; } } */ uip_connect(ipaddr, HTONS(TEST_HOST)); send_key(); return 0;}#else /* do the x86 */#include <sys/socket.h>#include <sys/types.h>#include <arpa/inet.h>#include <netdb.h>static int get_connected_socket(void);static void send_key_by_sock(int sock);int main(int argc, char **argv) { int sock; sock = get_connected_socket(); send_key_by_sock(sock); close(sock); return 0;} static void send_key_by_sock(int sock){ char ch; char buf[BUFFER_LEN]; ch = 'i'; /* 随便初始化一个值,第一次进入循环时不退出就可以 */ while('q' != ch) { printf("put q to exit>"); ch = getchar(); buf[0] = ch; buf[1] = '\0'; if (send(sock, buf, 1, 0)) { perror("gsending on stream socket"); } printf(" Sock send the %c\n", ch); }}static int get_connected_socket(void){ int sock; struct sockaddr_in server; struct hostent *hp; /* 建立套接字 */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /* 使用命令行中指定的名字连接套接字 */ server.sin_family = AF_INET; hp = gethostbyname(LOCALHOST); if (NULL == hp) { fprintf(stderr, "g%s: unknown host \n", LOCALHOST); exit(2); } printf("host address is %s\n", (char *)hp->h_addr); memcpy((void *)&server.sin_addr, (void *)hp->h_addr, hp->h_length); server.sin_port = htons(TEST_HOST); if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { perror("gconnecting stream socket"); exit(3); } return sock;}#endif /* end x86 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -