Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!crdgw1!rpi!batcomputer!prove From: prove@batcomputer.tn.cornell.edu (Roger Ove) Newsgroups: comp.windows.news Subject: Re: sockets Summary: socket demo Keywords: sockets Message-ID: <9166@batcomputer.tn.cornell.edu> Date: 27 Oct 89 16:06:16 GMT References: <9126@batcomputer.tn.cornell.edu> <9150@batcomputer.tn.cornell.edu> <18065@bellcore.bellcore.com> Distribution: comp Organization: Theory Center, Cornell U., Ithaca NY Lines: 423 Here is a demo with the C client sending data to the NeWS process by an independent socket. The file show.c isn't used by the demo, it is an interface to fortran on a Cray. I thinkg the Makefile will have to be modified to compile the demo on anything but a Cray. Roger Ove # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by tralfaz!ove on Fri Oct 27 10:09:09 EST 1989 # Contents: Makefile README opensock.c replay.c show.c show.cps echo x - Makefile sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//' # C/NeWS pixrect image displayed over network OBJ = replay.o opensock.o LIBS = $(NEWSHOME)/clientsrc/lib/NeWS/libcps.a /usr/lib/libnet.a @.SUFFIXES : @.SUFFIXES : .o .c replay : $(OBJ) cc -o replay $(OBJ) $(LIBS) replay.o : show.h show.o : show.h show.h : show.cps cps show.cps @.c.o : cc -O -c -I$(NEWSHOME)/clientsrc/include $*.c @//E*O*F Makefile// chmod u=rw,g=rw,o=rw Makefile echo x - README sed 's/^@//' > "README" <<'@//E*O*F README//' This is an example of sending data from the C client to a NeWS process by a secondary socket connection. It displays a sequence of pixrect image files (names read from the command line). The Makefile is for a Cray, some changes will be necessary to compile it on a Sun (source should require no changes). This example sets up a new socket connection for each image, which probably isn't such a great idea. @//E*O*F README// chmod u=rw,g=,o= README echo x - opensock.c sed 's/^@//' > "opensock.c" <<'@//E*O*F opensock.c//' #include #include #include #include #include #include #include /* Open a socket to NeWS_addr.(port+1), for writing. Return the * stream. */ FILE *open_socket_handle() { FILE *handle ; char *server ; int fd ; char *semi, *dot ; struct sockaddr_in sin ; server = (char *) getenv("NEWSSERVER") ; fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ; if (server && fd >= 0) { extern char *rindex() ; register char *t ; if (semi = rindex(server, ';')) *semi = 0 ; if (dot = rindex(server, '.')) { /* 1 plus NeWS port */ *dot = 0 ; sin.sin_port = htons(atoi(dot + 1)+1); } sin.sin_addr.s_addr = htonl(inet_addr(server)) ; if (dot) *dot = '.' ; if (semi) *semi = ';' ; } else return(0) ; sin.sin_family = AF_INET ; if (connect(fd, &sin, sizeof(sin)) < 0) { close(fd) ; return(0) ; } handle = fdopen(fd, "w") ; return (handle) ; } @//E*O*F opensock.c// chmod u=rw,g=rw,o=rw opensock.c echo x - replay.c sed 's/^@//' > "replay.c" <<'@//E*O*F replay.c//' /* Replay * * Simple NeWS display of pixrect files across a network. * Invoke as * replay file1 file2 .... filen * where the files are pixrect image files */ #include #include "show.h" #define MAXLENGTH (512*1024) main(argc, argv) int argc ; char *argv[] ; { int i, j, len ; char s[MAXLENGTH] ; char filename[100] ; FILE *in, *sock, *open_socket_handle() ; for (i=1; i "show.c" <<'@//E*O*F show.c//' /* Show * simple NeWS display of an array */ #include #include #include #include "show.h" #include #define max(a,b) (a>=b ? a : b) NEWSSTOP() { ps_close_PostScript() ; } /* Display a field using NeWS. Creates a string representation of a * Pixrect file and uses the NeWS readimage facility. */ #define RAS_MAGIC 0x59a66a95 struct rasterfile { long ras_magic ; long ras_width ; long ras_height ; long ras_depth ; long ras_length ; long ras_type ; long ras_maptype ; long ras_maplength ; } ; NEWSSHOW(mxp, myp, array_fcd) int *mxp, *myp ; _fcd array_fcd ; /* fortran character descriptor */ { static int entered = 0 ; FILE *mapfile, *sock, *open_socket_handle() ; int mx = *mxp, my = *myp ; int i ; int len = mx*my + 4*8 + 3*256 ; unsigned char *array = (unsigned char*)_fcdtocp(array_fcd); unsigned char *s = malloc(len + 1) ; unsigned char *data = &s[4*8 + 3*256] ; unsigned char *red = &(s[4*8]) ; unsigned char *green = &(s[4*8+256]) ; unsigned char *blue = &(s[4*8+2*256]) ; struct rasterfile pixrect ; /* set up the pixrect header */ pixrect.ras_magic = RAS_MAGIC ; pixrect.ras_width = mx ; pixrect.ras_height = my ; pixrect.ras_depth = 8 ; pixrect.ras_length = mx*my ; pixrect.ras_type = 1 ; pixrect.ras_maptype = 1 ; pixrect.ras_maplength = 3*256 ; /* copy the header into s, can't just memcpy, struct on 64bit boundaries */ ((long*)s)[0] = (pixrect.ras_magic << 32) + pixrect.ras_width ; ((long*)s)[1] = (pixrect.ras_height << 32) + pixrect.ras_depth ; ((long*)s)[2] = (pixrect.ras_length << 32) + pixrect.ras_type ; ((long*)s)[3] = (pixrect.ras_maptype << 32) + pixrect.ras_maplength ; /* add the rgb color map */ if ( NULL == (mapfile = fopen( "color.map", "rb" )) ) abort( "shit, where did the color.map file go?" ) ; for ( i=0; i<256; i++ ) red[i] = (unsigned char)fgetc(mapfile) ; for ( i=0; i<256; i++ ) green[i] = (unsigned char)fgetc(mapfile) ; for ( i=0; i<256; i++ ) blue[i] = (unsigned char)fgetc(mapfile) ; fclose( mapfile ) ; /* copy the data, reversing rows to undo pixrect convention */ for (i=0; i 1 ) { xslope = x_dim - 1 ; xinter = x_dim - 1 ; xfact = x_size ; } else { xslope = 1 ; xinter = 0 ; xfact = 1 ; } if ( y_size > 1 ) { yslope = y_dim - 1 ; yinter = y_dim - 1 ; yfact = y_size ; } else { yslope = 1 ; yinter = 0 ; yfact = 1 ; } /* compress the data to fit the window */ if ( x_dim > x_size & y_dim > y_size ) { for ( y = 0; y < y_size; y++ ) { yindex = ( yslope*y + yinter )/yfact ; for ( x = 0; x < x_size; x++ ) { xindex = ( xslope*x + xinter )/xfact ; data[x + y*x_size] = data[xindex + yindex*x_dim] ; }} x_dim = x_size ; y_dim = y_size ; } else { if ( x_dim > x_size ) { for ( y = 0; y < y_dim; y++ ) { for ( x = 0; x < x_size; x++ ) { xindex = ( xslope*x + xinter )/xfact ; data[x + y*x_size] = data[xindex + y*x_dim] ; }} x_dim = x_size ; } else { if ( y_dim > y_size ) { for ( y = 0; y < y_size; y++ ) { yindex = ( yslope*y + yinter )/yfact ; for ( x = 0; x < x_dim; x++ ) { data[x + y*x_dim] = data[x + yindex*x_dim] ; }} y_dim = y_size ; } }} } @//E*O*F show.c// chmod u=rw,g=rw,o=rw show.c echo x - show.cps sed 's/^@//' > "show.cps" <<'@//E*O*F show.cps//' %! show.cps % % Definitions for simple array display program % Initialize pixrect viewer. Data received via socket on port 2001 cdef ps_initialize(string flabel) /painter { % Paint routine for client or icon canvas setcanvas clippath pathbbox scale pop pop Pixcanvas imagecanvas pause } def /win framebuffer /new DefaultWindow send def % Create a window { % Install display src /Pixcanvas currentcanvas newcanvas def /FrameLabel flabel def /PaintClient { ClientCanvas painter } def /PaintIcon { IconCanvas painter } def } win send /reshapefromuser win send % Shape it. % define routine to get the data via socket /getimage { clear (%socketl2001) (r) file acceptconnection % should be waiting here a lot dup readcanvas /Pixcanvas exch store PaintClient closefile % close socket } def % fork a process to receive data /listener_pid { {{getimage} win send} loop } fork def % Activate window (Damage causes PaintClient to be called) /map win send @//E*O*F show.cps// chmod u=rw,g=rw,o=rw show.cps exit 0