occlum/test/client/main.c
He Sun dba6467c2d Fix server_epoll test
1. Change the port for server_poll to listen to avoid "address in use" conflict
between test/server and test/server_epoll, and add port as an argument for
test/client to send message
2. As posix-spwan may fail, change the fixed number of processes to spawn to
the number of processes successfully spawned in server_epoll
2019-10-07 04:36:12 +00:00

54 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <spawn.h>
#include <unistd.h>
int main(int argc, const char *argv[]) {
const char* message = "Hello world!";
int ret;
if (argc != 3) {
printf("usage: ./client <ipaddress> <port>\n");
return 0;
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons((uint16_t)strtol(argv[2], NULL, 10));
ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
if (ret <= 0) {
printf("inet_pton error for %s\n", argv[1]);
return -1;
}
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) {
printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
printf("send msg to server: %s\n", message);
ret = send(sockfd, message, strlen(message), 0);
if (ret < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
close(sockfd);
return 0;
}