SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
4 | #include <string.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <netinet/in.h> | |
8 | #include <netdb.h> | |
9 | #include <arpa/inet.h> // inet_ntoa | |
10 | #include <time.h> // time | |
11 | //====================================================================== | |
12 | int error( int ern, const char *err ){ | |
13 | perror( err ); | |
14 | return ern; | |
15 | } | |
16 | //====================================================================== | |
17 | int main( void ) | |
18 | { | |
19 | /* zmienne predefiniowane *****************************************/ | |
20 | int port = 12345; | |
21 | char host[] = "192.168.10.150"; | |
22 | /* zmienne niezainicjowane ****************************************/ | |
23 | int fd, n; | |
24 | struct sockaddr_in serv_addr; | |
25 | struct hostent *server; | |
26 | char buffer[256]; | |
27 | ||
28 | /* tworzymy gniazdo ***********************************************/ | |
29 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
30 | ||
31 | if (fd < 0) | |
32 | return error( 1, "socket()"); | |
33 | ||
34 | /* pobieramy informacje o serwerze ********************************/ | |
35 | server = gethostbyname( host ); | |
36 | ||
37 | if( server == NULL ) | |
38 | return error( 2, "gethostbyname()" ); | |
39 | ||
40 | /* zapelniamy strukture zerami (inicjujemy) ***********************/ | |
41 | bzero((char *) &serv_addr, sizeof(serv_addr)); | |
42 | ||
43 | /* przypisujemy parametry *****************************************/ | |
44 | serv_addr.sin_family = AF_INET; // typ gniazda | |
45 | serv_addr.sin_port = htons(port); // port | |
46 | ||
47 | bcopy( (char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length ); | |
48 | ||
49 | /* nawiazujemy polaczenie *****************************************/ | |
50 | if( connect( fd, (struct sockaddr *) &serv_addr, sizeof( serv_addr ) ) < 0 ) | |
51 | return error( 3, "connect()" ); | |
52 | ||
53 | ||
54 | /* zerujemy bufor *************************************************/ | |
55 | bzero( buffer, 256 ); | |
56 | ||
57 | ||
58 | /* przekazujemy serwerowi informacje o biezacym czasie ****/ | |
59 | //sprintf( buffer, "Current time: %d", (int) time( NULL ) ); | |
60 | - | //sprintf(buffer, "%d", 21); |
60 | + | sprintf(buffer, "proba 12"); |
61 | - | buffer[0] = 'z'; |
61 | + | //buffer[0] = 'z'; |
62 | - | buffer[1] = 'b'; |
62 | + | //buffer[1] = 'b'; |
63 | - | n = write( fd, buffer, 2 ); |
63 | + | n = write( fd, buffer, sizeof(buffer) ); |
64 | if( n < 0 ) // sprawdzamy, czy wystapil blad .... | |
65 | perror( "write()" ); | |
66 | ||
67 | ||
68 | /* konczymy polaczenie ********************************************/ | |
69 | close(fd); | |
70 | ||
71 | //------------------------------------------------------------------ | |
72 | return 0; | |
73 | } | |
74 | //====================================================================== |