Mail Archives: cygwin/2001/03/01/05:17:59
Hello,
trying to write to a socket-connection between a simple C-program and a
TCL-script, I got a problem. The output does not immediatly occur at the
reading end, but after stop the connection. Also explicite fflush() does not
help. Here is the code for both programs (The C-Code is the example from the
GNU-C-Library-Documentation). Any suggestions ? Thanks.
André Lasslop
> E-mail: mailto:andre DOT lasslop AT vs DOT dasa DOT de
>
>
>
################################## The C-Client-Program
#########################################################
/*
* Test of socket open, close and data transmission.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#define PORT 5555
#define MESSAGE "Yow!!! Are we having fun yet?!?"
#define SERVERHOST "lasan"
void
init_sockaddr(
struct sockaddr_in *name,
const char *hostname,
unsigned short int port )
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons( port );
hostinfo = gethostbyname( hostname );
if ( hostinfo == NULL ) {
fprintf( stderr, "Unknown host %s,\n", hostname );
exit( EXIT_FAILURE );
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
}
void
/* write_to_server(int filedes) */
write_to_server(FILE * pfd)
{
int nbytes;
printf("writing data\n");
nbytes = write( fileno(pfd), MESSAGE, strlen(MESSAGE) );
fflush(pfd);
/* nbytes = write( filedes, MESSAGE, strlen(MESSAGE) ); */
if ( nbytes < 0 ) {
perror( "write_to_server" );
exit( EXIT_FAILURE );
}
}
int
main( void )
{
extern void init_sockaddr(struct sockaddr_in *name,
const char *hostname,
unsigned short int port);
int i;
int sock;
struct sockaddr_in servername;
char hostname[0xff];
FILE * pfd;
gethostname( hostname, 0xff );
printf( "Max. Hostname-Length : %d\n", MAXHOSTNAMELEN );
printf( "Hostname : >%s<\n", hostname );
/* Create the socket */
sock = socket( PF_INET, SOCK_STREAM, 0);
if ( sock < 0 ) {
perror( "create socket (client)" );
exit( EXIT_FAILURE );
}
/* Create a stream-id */
pfd = fdopen( sock, "w");
/* Connect to the server. */
init_sockaddr( &servername, SERVERHOST, PORT );
if ( 0 > connect( sock,
(struct sockaddr *) &servername,
sizeof(servername) )) {
perror( "connect socket (client)" );
exit( EXIT_FAILURE );
}
/* Send data to the server */
for ( i=5 ; i ; i--) {
write_to_server( pfd );
/* write_to_server( sock ); */
sleep(1);
}
close( sock );
exit( EXIT_SUCCESS );
}
######################################### The TCL-Server-Script
####################################
#!/bin/sh
# the next line restarts using tclsh\
exec cygtclsh80 "$0" "$@"
global eth
set eth(debug) 1
proc svc { chan } {
global eth
if [eof $chan] {
if $eth(debug) { puts "Closing Channel" }
close $chan
set eth(stop) $chan
} else {
binary scan [read $chan] c* temp
puts "Channel : $temp"
flush stdout
}
}
proc acc { chan addr port } {
global eth
if $eth(debug) { puts "Opening Channel" }
fconfigure $chan -translation binary
fileevent $chan readable [list svc $chan]
}
set sockid [socket -server acc 5555]
vwait eth(stop)
close $sockid
exit
#################################### End of Code
#####################################
--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple
- Raw text -