#include <iostream> #include "include/folly/net/NetworkSocket.h" #include "include/folly/net/NetOps.h" #ifdef __cplusplus extern "C" { #endif #include <sys/socket.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus } #endif using namespace folly; using namespace std; int main(int argc, char *argv[]) { if(argc!=3){ printf("please input:client ip port!\n"); } int socket_fd = socket(AF_INET, SOCK_DGRAM, 0); if (socket_fd == -1){ std::cout << "socket error!" << std::endl; } NetworkSocket network_socket = NetworkSocket(socket_fd); struct sockaddr_in dest_sockaddr_in = {0}; dest_sockaddr_in.sin_family = AF_INET; dest_sockaddr_in.sin_port = htons(atoi(argv[2])); dest_sockaddr_in.sin_addr.s_addr = inet_addr(argv[1]); char buf[1024] = {0}; while (1) { printf("please input msg:"); scanf("%s",buf); folly::netops::sendto(network_socket,buf,strlen(buf),0,(struct sockaddr *)&dest_sockaddr_in,sizeof(dest_sockaddr_in)); if(strcmp(buf, "exit") == 0) { break; } memset(buf,0,sizeof(buf)); } folly::netops::close(network_socket); return 0; }
#include <iostream> #include "include/folly/net/NetworkSocket.h" #include "include/folly/net/NetOps.h" using namespace folly; using namespace std; #ifdef __cplusplus extern "C" { #endif #include <sys/socket.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus } #endif int main(int argc,char *argv[]) { if(argc != 2) { printf("please input:server port\n"); return -1; } int socket_fd = socket(AF_INET,SOCK_DGRAM,0); if(socket_fd < 0 ) { printf("socket error!\n"); return -1; } NetworkSocket network_socket = NetworkSocket(socket_fd); struct sockaddr_in local_addr = {0}; local_addr.sin_family = AF_INET; local_addr.sin_port = htons(atoi(argv[1])); local_addr.sin_addr.s_addr = INADDR_ANY; int ret = folly::netops::bind(network_socket, (struct sockaddr*)&local_addr, sizeof(local_addr)); if(ret < 0) { printf("bind error!\n"); close(socket_fd); return -1; } else { printf("recvfrom ready!\n"); } struct sockaddr_in src_addr = {0}; int len = sizeof(src_addr); char buf[1024] = {0}; while(1) { ret =folly::netops::recvfrom(network_socket, buf, sizeof(buf), 0, (struct sockaddr *)&src_addr, (socklen_t*)&len); if(ret == -1) { break; } printf("ip:%s,port:%d,",inet_ntoa(src_addr.sin_addr),ntohs(src_addr.sin_port)); printf("buf=%s\n",buf); if(strcmp(buf, "exit") == 0) { break; } memset(buf, 0, sizeof(buf)); } folly::netops::close(network_socket); }