/*************************************************************************** Yvan Petillot Heriot Watt University Version 1.0 October, 2002 Yvan Petillot Initial Coding ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define BUFFER_SIZE 1000 #define PORT 10000 void SysError( char * ); /* Thread used to handle send and receive */ pthread_t chat_send; pthread_t chat_receive; /* Socket use by main and threads */ struct sockaddr_in my_addr; /* Structure storing socket info */ struct sockaddr_in their_addr; /* Structure storing socket info */ int fd; /* Socket id. On Unix a socket is a file descriptor */ /* function prototypes */ void thread_chat_send(void); void thread_chat_receive(void); char name[80]; /* Main Program */ main ( int argc, char *argv[] ) { int family = AF_INET; /* The default for most cases */ int type = SOCK_STREAM; /* Says it's a UDP connection */ int lsa = sizeof(my_addr); int on; void *status; /* Checks first we have the correct number of arguments */ if ( argc < 2 ) { printf( "The program expects arguments\n" ); printf( "udp Name\n" ); exit(0); } strcpy(name,argv[1]); /* Create socket*/ if ((fd = socket (family, type, 0)) < 0) { SysError ("Error on socket"); } /* Fills my_addr socket structure with correct fields */ my_addr.sin_family = family; my_addr.sin_port = (PORT); my_addr.sin_addr.s_addr = htonl(0xFFFFFFFF); //Any addr /* Fills their socket structure with correct fields */ their_addr.sin_family = family; their_addr.sin_port = (PORT); their_addr.sin_addr.s_addr = htonl(0xFFFFFFFF); // Any addr /*memcpy((char *)&their_addr.sin_addr, (char *)host->h_addr, host->h_length); printf("%x\n",host->h_addr);*/ on = 1; setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on)); /* First bind fd and sa structure*/ if (bind (fd, (struct sockaddr *)&my_addr, sizeof(my_addr) ) == -1) SysError ("Error on bind"); if(pthread_create(&chat_send,NULL, (void*) thread_chat_send,NULL)!=0) { SysError("Cannot create send thread"); } if(pthread_create(&chat_receive,NULL, (void*) thread_chat_receive,NULL)!=0) { SysError("Cannot create send thread"); } pthread_join(chat_send,&status); pthread_join(chat_receive,&status); } /* End of main */ void thread_chat_send() { int count; char console_buffer[BUFFER_SIZE]; char ip_input_buffer[BUFFER_SIZE]; char ip_output_buffer[BUFFER_SIZE]; /* First Say you are online! */ /* Make sure output buffer is empty */ bzero( ip_output_buffer, sizeof( ip_output_buffer )); /* Copy value to output buffer */ strcpy( ip_output_buffer, name); strcat( ip_output_buffer," is on line "); count = sendto(fd, ip_output_buffer, strlen(ip_output_buffer), 0,&their_addr,sizeof(their_addr) ); if (count == 0) SysError( "Error on send" ); /* Infinite loop until fault */ while( TRUE ) { /* Get a number from console */ printf( "%s> ",name); gets(&console_buffer ); /* Convert it to integer (-1 to exit)*/ if ( atoi( console_buffer ) == -1 ) { printf( "We Have Successfully Finished.\n" ); exit(0); } /* Make sure output buffer is empty */ bzero( ip_output_buffer, sizeof( ip_output_buffer )); /* Copy value to output buffer */ strcpy( ip_output_buffer, name); strcat( ip_output_buffer," says "); strcat( ip_output_buffer, console_buffer); //strcpy( ip_output_buffer, console_buffer ); /* Send value to other machine */ count = sendto(fd, ip_output_buffer, strlen(ip_output_buffer), 0,&their_addr,sizeof(their_addr) ); if (count == 0) SysError( "Error on send" ); } close(fd); } void thread_chat_receive() { int count; char console_buffer[BUFFER_SIZE]; char ip_input_buffer[BUFFER_SIZE]; char ip_output_buffer[BUFFER_SIZE]; /* Infinite loop until fault */ while( TRUE ) { bzero( ip_input_buffer, sizeof(ip_input_buffer) ); /* receive data back from machine */ count = recvfrom( fd, ip_input_buffer,sizeof(ip_input_buffer),0,&their_addr,sizeof(their_addr)); if (count == 0) SysError( "Error on recv" ); /*Print reception */ printf( "%s\n", ip_input_buffer ); } close(fd); } void SysError( char *string ) { printf( "Error found: String given is --> %s\n", string ); exit(0); }