Permalink
Browse files

Merge pull request #9 from arnatal/tcpudptests

tests: get addresses and ports from command line
  • Loading branch information...
2 parents 41567d8 + f3c7078 commit 19ace812f6b9badc6e2da31ba8012d29019cff10 Lori Jakab committed Mar 19, 2012
Showing with 242 additions and 267 deletions.
  1. +5 −14 tests/Makefile
  2. +0 −5 tests/clientserver.h
  3. +7 −0 tests/tcp_echo.h
  4. +50 −54 tests/tcp_echo_client.c
  5. +62 −103 tests/tcp_echo_server.c
  6. +59 −44 tests/udp_echo_client.c
  7. +59 −47 tests/udp_echo_server.c
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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;
+}
Oops, something went wrong.

0 comments on commit 19ace81

Please sign in to comment.