Java Reference
In-Depth Information
actly the right files be included, and some files have to be listed in particular orders (Java's
import mechanism is much more flexible).
Example 13-1. C client setup—src/main/java/network/Connect.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
int
int
main ( int
int argc , char
char * argv [])
{
char
char * server_name = "localhost" ;
struct
struct hostent * host_info ;
int
int sock ;
struct
struct sockaddr_in server ;
/* Look up the remote host's IP address */
host_info = gethostbyname ( server_name );
iif ( host_info == NULL ) {
fprintf ( stderr , "%s: unknown host: %s\\n" , argv [ 0 ], server_name );
exit ( 1 );
}
/* Create the socket */
iif (( sock = socket ( AF_INET , SOCK_STREAM , 0 )) < 0 ) {
perror ( "creating client socket" );
exit ( 2 );
}
/* Set up the server's socket address */
server . sin_family = AF_INET ;
memcpy (( char
char * ) & server . sin_addr , host_info -> h_addr ,
host_info -> h_length );
server . sin_port = htons ( 80 );
/* Connect to the server */
iif ( connect ( sock ,( struct
struct sockaddr * ) & server , sizeof
sizeof server ) < 0 ) {
perror ( "connecting to server" );
exit ( 4 );
}
Search WWH ::




Custom Search