Created
October 15, 2012 23:37
-
-
Save changemewtf/3896374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void TestReceives(CuTest *tc) { | |
struct sockaddr_in serv_port, cli_port, dest, from; | |
int server, client; | |
char recv_buffer[255]; | |
int sent_bytes, received_bytes; | |
socklen_t from_size; | |
server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
if (server <= 0) { fprintf(stderr, "no server\n"); exit(1); } | |
serv_port.sin_family = AF_INET; | |
serv_port.sin_addr.s_addr = INADDR_ANY; | |
serv_port.sin_port = htons((unsigned short) SERVER_PORT); | |
if (bind(server, (struct sockaddr*)&serv_port, sizeof(serv_port)) < 0) { fprintf(stderr, "no server bind\n"); exit(1); } | |
if (fcntl(server, F_SETFL, O_NONBLOCK, 1) == -1) { fprintf(stderr, "no server nonblock\n"); exit(1); } | |
client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
if (client <= 0) { fprintf(stderr, "no client\n"); exit(1); } | |
cli_port.sin_family = AF_INET; | |
cli_port.sin_addr.s_addr = INADDR_ANY; | |
cli_port.sin_port = htons((unsigned short) CLIENT_PORT); | |
if (bind(client, (struct sockaddr*)&cli_port, sizeof(cli_port)) < 0) { fprintf(stderr, "no client bind\n"); exit(1); } | |
if (fcntl(client, F_SETFL, O_NONBLOCK, 1) == -1) { fprintf(stderr, "no client nonblock\n"); exit(1); } | |
dest.sin_family = AF_INET; | |
dest.sin_port = htons((unsigned short) SERVER_PORT); | |
inet_aton("127.0.0.1", &dest.sin_addr); | |
//dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
//dest.sin_addr.s_addr = (1 << 24) | (0 << 16) | (0 << 8) | 127; | |
sent_bytes = sendto(client, "test", sizeof(char) * 5, 0, (struct sockaddr*)&dest, sizeof(dest)); | |
received_bytes = recvfrom(server, recv_buffer, sizeof(char) * 5, 0, (struct sockaddr*)&from, &from_size); | |
CuAssertIntEquals(tc, sizeof(char) * 5, sent_bytes); | |
CuAssertIntEquals(tc, sizeof(char) * 5, received_bytes); | |
CuAssertStrEquals(tc, "test", recv_buffer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment