1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| 客户端 # include <sys/socket.h> # include <netdb.h> # include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h> # include <arpa/inet.h> //inet_addr
# define SERVERPORT 9999 # define LOCALHOST "127.0.0.1"
# define MYMSGLEN 2048
void question ( int sock, char * msg, struct sockaddr_in * addr, struct sockaddr_in * from ) { int code, length, ret ; socklen_t len; // Send the length of the string. length = strlen ( msg ) ; // + 1 ; // Send the string of characters. len = sizeof ( struct sockaddr_in ) ; code = sendto ( sock, msg, length, 0, ( struct sockaddr * ) addr, len ) ; if ( code == -1 ) { perror ( "sendto" ) ; close ( sock ) ; exit ( 1 ) ; }
// Wait for the answer. memset ( msg, 0, length ) ; len = sizeof ( * from ) ; code = recvfrom ( sock, ( char * ) msg, length, 0, ( struct sockaddr * ) from, & len ) ; if ( code == -1 ) { perror ( "recvfrom" ) ; close ( sock ) ; exit ( 1 ) ; }
// Empty message!!!! 0 value returned in UDP mode does not mean closing a connection since there is no connection // We release the resource and exit the program anyway if ( code == 0 ) { // Release ressources. code = close ( sock ) ; if ( code == -1 ) { perror ( "close" ) ; } exit ( 1 ) ; } }
int main ( int argc, char * argv [ ] ) { int item ; socklen_t len; // Ask for the string of characters. char string [ MYMSGLEN ] ; // Create UDP socket and init server address structure. int sock ; sock = socket ( AF_INET, SOCK_DGRAM, 0 ) ; if ( sock == -1 ) { perror ( "socket" ) ; exit ( 1 ) ; }
// Initialisation of the sockaddr_in data structure
struct sockaddr_in addr ; memset ( & addr, 0, sizeof ( struct sockaddr_in ) ) ; addr . sin_family = AF_INET ; addr . sin_port = htons ( SERVERPORT ) ; addr . sin_addr . s_addr = inet_addr ( LOCALHOST ) ;
while ( 1 ) { memset ( string, 0, sizeof( string ) ) ; printf ( "\n----------------------------------------------------------------------\n" ) ; printf ( "Please type a message to transfer for processing:" ) ; item = scanf ( "%[^\n]%*c", string ) ; //reads a hole line till a new line feed //This is in case of empty message if ( item == 0 ) { scanf("%*c"); continue; } printf ( "The message being sent is: '%s' \n", string ) ; printf( "----------------------------------------------------------------------\n" ) ;
if ( !memcmp ( "quit#", string, 5 ) ) { printf ( " Client exiting upon user request \n" ) ; close ( sock ) ; exit ( 1 ) ; } // Ask the question and wait for the answer. struct sockaddr_in from ; question ( sock, string, & addr, & from ) ; // Display the result. printf ( "\n----------------------------------------------------------------------\n" ) ; printf ( "The processed message is received from server %s, with port number: %d \n" , inet_ntoa( from.sin_addr ), ntohs( from.sin_port ) ) ;
printf ( "The message is : %s\n", string ) ; printf ( "----------------------------------------------------------------------\n" ) ; } return ( 0 ) ; }
|