Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #9 from arnatal/tcpudptests

tests: get addresses and ports from command line
  • Loading branch information...
commit 19ace812f6b9badc6e2da31ba8012d29019cff10 2 parents 41567d8 + f3c7078
@ljakab ljakab authored
View
19 tests/Makefile
@@ -1,14 +1,5 @@
-all: tests
-
-tests: udp tcp
-
-udp:
- gcc -o udp_echo_server udp_echo_server.c
- gcc -o udp_echo_client udp_echo_client.c
-
-tcp:
- gcc -o tcp_echo_server tcp_echo_server.c
- gcc -o tcp_echo_client tcp_echo_client.c
-
-clean:
- rm -f udp_echo_server udp_echo_client tcp_echo_server tcp_echo_client
+all: tests tests: udp tcp udp:
+gcc - o udp_echo_server udp_echo_server.c gcc - o udp_echo_client udp_echo_client.c tcp:
+gcc - o tcp_echo_server tcp_echo_server.c gcc - o tcp_echo_client tcp_echo_client.c clean:
+rm - f udp_echo_server udp_echo_client tcp_echo_server
+ tcp_echo_client
View
5 tests/clientserver.h
@@ -1,7 +1,2 @@
#define BUFLEN 512
#define NPACK 1000000
-#define SPORT 50000
-#define SADDR "127.0.0.1" /* Server address */
-
-/* Max pending connection requests (TCP only)*/
-#define QUEUELENGTH 2
View
7 tests/tcp_echo.h
@@ -0,0 +1,7 @@
+#define BUFLEN 512
+#define NPACK 1000000
+#define SPORT 50000
+#define SADDR "127.0.0.1" /* Server address */
+
+/* Max pending connection requests (TCP only)*/
+#define QUEUELENGTH 2
View
104 tests/tcp_echo_client.c
@@ -7,69 +7,65 @@
#include <sys/types.h>
#include <sys/socket.h>
-#include "clientserver.h"
+#include "tcp_echo.h"
int main(int argc, char **argv)
{
- struct sockaddr_in si_server;
- int port, s, i, slen=sizeof(si_server);
- char buf[BUFLEN];
- char srv_addr[16];
- fd_set readfds;
- struct timeval tv;
- int ret;
+ struct sockaddr_in si_server;
+ int port, s, i, slen = sizeof(si_server);
+ char buf[BUFLEN];
+ char srv_addr[16];
+ fd_set readfds;
+ struct timeval tv;
+ int ret;
- /* TCP socket creation */
- if ((s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==-1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
+ /* TCP socket creation */
+ if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
+ perror("socket");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Server sockaddr structure */
+ memset((char *) &si_server, 0, sizeof(si_server));
+ si_server.sin_family = AF_INET;
+ si_server.sin_port = htons(SPORT);
+ if (inet_aton(SADDR, &si_server.sin_addr) == 0) {
+ fprintf(stderr, "inet_aton() failed\n");
+ exit(EXIT_FAILURE);
+ }
- /* Server sockaddr structure */
- memset((char *) &si_server, 0, sizeof(si_server));
- si_server.sin_family = AF_INET;
- si_server.sin_port = htons(SPORT);
- if (inet_aton(SADDR, &si_server.sin_addr)==0)
- {
- fprintf(stderr, "inet_aton() failed\n");
- exit(EXIT_FAILURE);
- }
-
- /* Establish connection */
- if (connect(s, (struct sockaddr *) &si_server, sizeof(si_server)) == -1)
- {
- fprintf(stderr, "connect() failed\n");
- exit(EXIT_FAILURE);
- }
-
- /* Send-Recv loop */
- for (i=0; i<NPACK; i++)
- {
- sprintf(buf, "DATA PACKET # %d", i);
- printf("Sending -- %s -- to %s:%d\n", buf, inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
+ /* Establish connection */
+ if (connect(s, (struct sockaddr *) &si_server, sizeof(si_server)) ==
+ -1) {
+ fprintf(stderr, "connect() failed\n");
+ exit(EXIT_FAILURE);
+ }
- if (send(s, buf, BUFLEN, 0) == -1)
- {
- perror("send()");
- exit(EXIT_FAILURE);
- }
+ /* Send-Recv loop */
+ for (i = 0; i < NPACK; i++) {
+ sprintf(buf, "DATA PACKET # %d", i);
+ printf("Sending -- %s -- to %s:%d\n", buf,
+ inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
- ret = recv(s, buf, BUFLEN, 0);
- if (ret > 0)
- {
- printf("Received -- %s -- from %s:%d\n", buf, inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
+ if (send(s, buf, BUFLEN, 0) == -1) {
+ perror("send()");
+ exit(EXIT_FAILURE);
}
- else
- {
- perror("recv()");
- exit(EXIT_FAILURE);
+
+ ret = recv(s, buf, BUFLEN, 0);
+ if (ret > 0) {
+ printf("Received -- %s -- from %s:%d\n", buf,
+ inet_ntoa(si_server.sin_addr),
+ ntohs(si_server.sin_port));
+ } else {
+ perror("recv()");
+ exit(EXIT_FAILURE);
}
-
+
sleep(2);
}
-
-
- close(s);
- return 0;
+
+
+ close(s);
+ return 0;
}
View
165 tests/tcp_echo_server.c
@@ -7,106 +7,65 @@
#include <sys/types.h>
#include <sys/socket.h>
-#include "clientserver.h"
-
-
- void handle_client(int sock, struct sockaddr_in si_remote) {
- char buf[BUFLEN];
- int rec = -1;
-
- /* Message received */
- if ((rec = recv(sock, buf, BUFLEN, 0)) == -1)
- {
- perror("recv");
- exit(EXIT_FAILURE);
- }
- printf("Received -- %s -- from %s:%d\n", buf, inet_ntoa(si_remote.sin_addr), ntohs(si_remote.sin_port));
-
- /* While the client is sending data */
- while (rec > 0)
- {
-
- /* Send back data */
- printf("Sending -- %s -- to %s:%d\n", buf, inet_ntoa(si_remote.sin_addr), ntohs(si_remote.sin_port));
-
- if (send(sock, buf, rec, 0) == -1)
- {
- perror("send");
- exit(EXIT_FAILURE);
- }
-
- /* More data? */
- if ((rec = recv(sock, buf, BUFLEN, 0)) == -1)
- {
- perror("send");
- exit(EXIT_FAILURE);
- }
- else
- {
- printf("Received -- %s -- from %s:%d\n", buf, inet_ntoa(si_remote.sin_addr), ntohs(si_remote.sin_port));
- }
-
- }
- close(sock);
- }
-
-
- int main(int argc, char **argv)
- {
- struct sockaddr_in si_local, si_remote;
- int s_loc,s_rem;
- int port;
- int slen;
- char buf[BUFLEN];
-
- slen = sizeof(si_remote);
-
- /* Socket creation */
- if ((s_loc=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==-1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
-
- /* Server sockaddr structure */
- memset((char *) &si_local, 0, sizeof(si_local));
- si_local.sin_family = AF_INET;
- si_local.sin_port = htons(SPORT);
- si_local.sin_addr.s_addr = INADDR_ANY; /* Any interface */
-
- if (bind(s_loc, (const struct sockaddr *)&si_local, sizeof(si_local))==-1)
- {
- perror("bind");
- exit(EXIT_FAILURE);
- }
-
- if (listen(s_loc, QUEUELENGTH) == -1)
- {
- perror("listen");
- exit(EXIT_FAILURE);
- }
-
-
- while(1)
- {
-
- slen = sizeof(si_remote);
-
- /* Accept connection */
- if ((s_rem = accept(s_loc, (struct sockaddr *)&si_remote, &slen)) == -1)
- {
- perror("accept");
- exit(EXIT_FAILURE);
- }
-
- printf("Connection from %s:%d\n", inet_ntoa(si_remote.sin_addr), ntohs(si_remote.sin_port));
-
- /* Per client management */
- handle_client(s_rem, si_remote);
-
- }
-
- close(s_loc);
- exit(EXIT_SUCCESS);
- }
-
+#include "tcp_echo.h"
+
+int main(int argc, char **argv)
+{
+ struct sockaddr_in si_server;
+ int port, s, i, slen = sizeof(si_server);
+ char buf[BUFLEN];
+ char srv_addr[16];
+ fd_set readfds;
+ struct timeval tv;
+ int ret;
+
+ /* TCP socket creation */
+ if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
+ perror("socket");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Server sockaddr structure */
+ memset((char *) &si_server, 0, sizeof(si_server));
+ si_server.sin_family = AF_INET;
+ si_server.sin_port = htons(SPORT);
+ if (inet_aton(SADDR, &si_server.sin_addr) == 0) {
+ fprintf(stderr, "inet_aton() failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Establish connection */
+ if (connect(s, (struct sockaddr *) &si_server, sizeof(si_server)) ==
+ -1) {
+ fprintf(stderr, "connect() failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Send-Recv loop */
+ for (i = 0; i < NPACK; i++) {
+ sprintf(buf, "DATA PACKET # %d", i);
+ printf("Sending -- %s -- to %s:%d\n", buf,
+ inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
+
+ if (send(s, buf, BUFLEN, 0) == -1) {
+ perror("send()");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = recv(s, buf, BUFLEN, 0);
+ if (ret > 0) {
+ printf("Received -- %s -- from %s:%d\n", buf,
+ inet_ntoa(si_server.sin_addr),
+ ntohs(si_server.sin_port));
+ } else {
+ perror("recv()");
+ exit(EXIT_FAILURE);
+ }
+
+ sleep(2);
+ }
+
+
+ close(s);
+ return 0;
+}
View
103 tests/udp_echo_client.c
@@ -9,59 +9,74 @@
#include "clientserver.h"
+void error(const char *msg)
+{
+ perror(msg);
+ exit(EXIT_FAILURE);
+}
+
+
int main(int argc, char **argv)
{
- struct sockaddr_in si_server;
- int port, s, i, slen=sizeof(si_server);
- char buf[BUFLEN];
- char srv_addr[16];
- fd_set readfds;
- struct timeval tv;
- int ret;
+ struct sockaddr_in si_server;
+ int port, s, i, slen = sizeof(si_server);
+ char buf[BUFLEN];
+ char *srv_addr;
+ fd_set readfds;
+ struct timeval tv;
+ int ret;
+
+ if (argc < 3) {
+ printf("Usage: %s ip_add port\n", argv[0]);
+ exit(1);
+ }
- if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
+ port = atoi(argv[2]);
+ srv_addr = argv[1];
- memset((char *) &si_server, 0, sizeof(si_server));
- si_server.sin_family = AF_INET;
- si_server.sin_port = htons(SPORT);
- if (inet_aton(SADDR, &si_server.sin_addr)==0)
- {
- fprintf(stderr, "inet_aton() failed\n");
- exit(EXIT_FAILURE);
- }
- for (i=0; i<NPACK; i++)
- {
- sprintf(buf, "DATA PACKET # %d", i);
- printf("Sending -- %s -- to %s:%d\n", buf, inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
- slen = sizeof(si_server);
- if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *)&si_server, slen)==-1)
- {
- perror("sendto()");
- exit(EXIT_FAILURE);
- }
+ if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
+ error("socket");
+ }
+
+ memset((char *) &si_server, 0, sizeof(si_server));
+ si_server.sin_family = AF_INET;
+ si_server.sin_port = htons(port);
+ if (inet_aton(srv_addr, &si_server.sin_addr) == 0) {
+ fprintf(stderr, "inet_aton() failed\n");
+ exit(EXIT_FAILURE);
+ }
+ for (i = 0; i < NPACK; i++) {
+ sprintf(buf, "DATA PACKET # %d", i);
+ printf("Sending -- %s -- to %s:%d\n", buf,
+ inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
+ slen = sizeof(si_server);
+ if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *) &si_server, slen)
+ == -1) {
+ error("sendto()");
+ }
FD_ZERO(&readfds);
- FD_SET(s,&readfds);
+ FD_SET(s, &readfds);
- tv.tv_sec = 1;
- tv.tv_usec = 0;
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
- if (select(s+1,&readfds,NULL,NULL,&tv) == -1) {
- perror("select()");
- exit (EXIT_FAILURE);
- }
- if (FD_ISSET(s,&readfds)) {
- ret = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_server, &slen);
- if (ret > 0)
- printf("Received -- %s -- from %s:%d\n", buf, inet_ntoa(si_server.sin_addr), ntohs(si_server.sin_port));
+ if (select(s + 1, &readfds, NULL, NULL, &tv) == -1) {
+ error("select()");
}
- sleep(2);
+ if (FD_ISSET(s, &readfds)) {
+ ret =
+ recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_server,
+ &slen);
+ if (ret > 0) {
+ printf("Received -- %s -- from %s:%d\n", buf,
+ inet_ntoa(si_server.sin_addr),
+ ntohs(si_server.sin_port));
+ }
}
- close(s);
- return 0;
+ sleep(2);
+ }
+ close(s);
+ return 0;
}
View
106 tests/udp_echo_server.c
@@ -9,58 +9,70 @@
#include "clientserver.h"
+void error(const char *msg)
+{
+ perror(msg);
+ exit(EXIT_FAILURE);
+}
+
int main(int argc, char **argv)
{
- struct sockaddr_in si_local, si_remote;
- int s;
- int port;
- int slen;
- char buf[BUFLEN];
+ struct sockaddr_in si_local, si_remote;
+ int s;
+ int port;
+ int slen;
+ char buf[BUFLEN];
- slen = sizeof(si_remote);
- if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- memset((char *) &si_local, 0, sizeof(si_local));
- si_local.sin_family = AF_INET;
- si_local.sin_port = htons(SPORT);
- if (inet_aton(SADDR, &si_local.sin_addr)==0)
- {
- perror("inet_aton()");
- exit (EXIT_FAILURE);
+ if (argc < 2) {
+ printf("Usage: %s port\n", argv[0]);
+ exit(1);
+ }
- }
- if (bind(s, (const struct sockaddr *)&si_local, sizeof(si_local))==-1)
- {
- perror("bind");
- exit(EXIT_FAILURE);
- }
+ port = atoi(argv[1]);
+
+ slen = sizeof(si_remote);
+ if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
+ error("socket");
+ }
+
+ memset((char *) &si_local, 0, sizeof(si_local));
+ si_local.sin_family = AF_INET;
+ si_local.sin_port = htons(port);
+ si_local.sin_addr.s_addr = INADDR_ANY;
- while(1)
- {
- slen = sizeof(si_remote);
- memset(buf, 0, sizeof(char)*BUFLEN);
- memset(&si_remote, 0, sizeof(si_remote));
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, &slen)==-1)
- {
- perror("recvfrom()");
- exit(EXIT_FAILURE);
- }
- {
- printf("Received -- %s -- from %s:%d\n", buf, inet_ntoa(si_remote.sin_addr), ntohs(si_remote.sin_port));
- printf("Sending -- %s -- to %s:%d\n", buf, inet_ntoa(si_remote.sin_addr), ntohs(si_remote.sin_port));
- slen = sizeof(si_remote);
- if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, slen)==-1)
- {
- perror("recvfrom()");
- exit(EXIT_FAILURE);
- }
- }
- }
- close(s);
- exit(EXIT_SUCCESS);
+ if (bind(s, (const struct sockaddr *) &si_local, sizeof(si_local)) ==
+ -1) {
+ error("bind");
+ }
+
+ while (1) {
+ slen = sizeof(si_remote);
+ memset(buf, 0, sizeof(char) * BUFLEN);
+ memset(&si_remote, 0, sizeof(si_remote));
+
+ if (recvfrom
+ (s, buf, BUFLEN, 0, (struct sockaddr *) &si_remote,
+ &slen) == -1) {
+ error("recvfrom()");
+ } else {
+ printf("Received -- %s -- from %s:%d\n", buf,
+ inet_ntoa(si_remote.sin_addr),
+ ntohs(si_remote.sin_port));
+ printf("Sending -- %s -- to %s:%d\n", buf,
+ inet_ntoa(si_remote.sin_addr),
+ ntohs(si_remote.sin_port));
+ slen = sizeof(si_remote);
+
+ if (sendto
+ (s, buf, BUFLEN, 0, (struct sockaddr *) &si_remote,
+ slen) == -1) {
+ error("recvfrom()");
+ }
+ }
+ }
+
+ close(s);
+ exit(EXIT_SUCCESS);
}
Please sign in to comment.
Something went wrong with that request. Please try again.