Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <arpa/inet.h> // inet_ntoa
- #include <time.h> // time
- //======================================================================
- int error( int ern, const char *err ){
- perror( err );
- return ern;
- }
- //======================================================================
- int main( void )
- {
- /* zmienne predefiniowane *****************************************/
- int port = 12345;
- char host[] = "192.168.10.150";
- /* zmienne niezainicjowane ****************************************/
- int fd, n;
- struct sockaddr_in serv_addr;
- struct hostent *server;
- char buffer[256];
- /* tworzymy gniazdo ***********************************************/
- fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd < 0)
- return error( 1, "socket()");
- /* pobieramy informacje o serwerze ********************************/
- server = gethostbyname( host );
- if( server == NULL )
- return error( 2, "gethostbyname()" );
- /* zapelniamy strukture zerami (inicjujemy) ***********************/
- bzero((char *) &serv_addr, sizeof(serv_addr));
- /* przypisujemy parametry *****************************************/
- serv_addr.sin_family = AF_INET; // typ gniazda
- serv_addr.sin_port = htons(port); // port
- bcopy( (char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length );
- /* nawiazujemy polaczenie *****************************************/
- if( connect( fd, (struct sockaddr *) &serv_addr, sizeof( serv_addr ) ) < 0 )
- return error( 3, "connect()" );
- /* zerujemy bufor *************************************************/
- bzero( buffer, 256 );
- /* przekazujemy serwerowi informacje o biezacym czasie ****/
- //sprintf( buffer, "Current time: %d", (int) time( NULL ) );
- sprintf(buffer, "proba 12");
- //buffer[0] = 'z';
- //buffer[1] = 'b';
- n = write( fd, buffer, sizeof(buffer) );
- if( n < 0 ) // sprawdzamy, czy wystapil blad ....
- perror( "write()" );
- /* konczymy polaczenie ********************************************/
- close(fd);
- //------------------------------------------------------------------
- return 0;
- }
- //======================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement