/* wtest.c Copyright 1997 Michael Temari All Rights Reserved */ #include #include #include #include #include #include #include "tcp.h" #define WTEST_PORT 812 int gotdata = 0; longword oa; word op; byte gdata[1410]; int glen; char odata[1410]; char *pip(longword addr) { static char buf[3+1+3+1+3+1+3+1]; unsigned char *p; p = (unsigned char *)&addr; sprintf(buf, "%d.%d.%d.%d", p[3], p[2], p[1], p[0]); return(buf); } char *myip(void) { return(pip(gethostid())); } int udp_send(udp_Socket *is, byte *data, int len, longword da, word dp) { static udp_Socket sock; udp_Socket *s; int status; int len2; s = &sock; if(!udp_open((udp_Socket *)s, WTEST_PORT, da, dp, NULL)) { printf("udp_send:\nudp_open error\nPacket not sent\n"); return(-1); } if((len2 = sock_write(s, (unsigned char *)data, len)) != len) { printf("udp_send:\nsock_write\n %d != %d\n", len, len2); return(-1); } sock_tick(s, &status); sock_close(s); status = 1; sock_wait_closed(s, sock_delay, NULL, &status); return(0); sock_err: if(status != 1) { printf("udp_send:\nSocket Error: %d\n\n", status); } return(0); } #if 1 int udp_recv(void *s, byte *data, int len, tcp_PseudoHeader *ph, void *up) { #else int udp_recv(void *s, byte *data, int len, tcp_PseudoHeader *ph) { char *up; up = (char *)data - UDP_LENGTH; #endif oa = intel(ph->src); op = intel16(*(word *)up); memcpy(gdata, data, len); glen = len; gotdata = 1; return(0); } int server(longword host) { static udp_Socket sock; udp_Socket *s; int status; printf("Server IP: %s\n", myip()); s = &sock; if(!udp_open((udp_Socket *)s, WTEST_PORT, host, 0, udp_recv)) { printf("server:\nudp_open error\n\n"); return(-1); } while(!kbhit()) { sock_tick(s, &status); if(gotdata) { gotdata = 0; printf("Rcvd: %s\n", gdata); (void) udp_send(s, gdata, glen, oa, op); } } sock_close(s); status = 1; sock_wait_closed(s, sock_delay, NULL, &status); return(0); sock_err: if(status != 1) { printf("server:\nSocket Error: %d\n\n", status); } return(0); } int client(char *hostname) { longword host; static udp_Socket sock; udp_Socket *s; int status; int len, size; time_t t; printf("Client IP: %s\n", myip()); if((host = resolve(hostname)) == 0uL) { printf("Could not resolve\n"); printf("%s\n\n", hostname); return(-1); } s = &sock; if(!udp_open((udp_Socket *)s, 0, host, WTEST_PORT, udp_recv)) { printf("No server found\n"); printf("at %s\n\n", pip(host)); return(-1); } printf("Enter data: "); while(fgets((char *)odata, sizeof(odata), stdin) != (char *) NULL) { odata[strlen(odata)-1] = '\0'; size = strlen(odata)+1; if(size == 1) break; printf("Sent: %s\n", odata); if((len = sock_write(s, (byte *)odata, size)) != size) { printf("client:\nsock_write\n%d != %d\n", size, len); return(-1); } gotdata = 0; (void) time(&t); while(!gotdata && (time(0) < t + 5)) { sock_tick(s, &status); len = sock_dataready(s); } if(gotdata) printf("Rcvd: %s\n", gdata); else printf("No Data recevied\n"); printf("\nEnter data: "); } sock_close(s); status = 1; sock_wait_closed(s, sock_delay, NULL, &status); return(0); sock_err: if(status != 1) { printf("client:\nsocket error %d\n\n", status); } return(0); } int main(int argc, char **argv ) { sock_init(); if(argc != 2) return(server(0xFFFFFFFFL)); else return(client(argv[1])); }