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[128];
- /* 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()" );
- int buffSize = 128;
- /* zerujemy bufor *************************************************/
- bzero( buffer, 128 );
- //czytamy z STDIN
- int nsc = 0;
- int actIndex = 0;
- //while ( (nsc = scanf("%s", buffer)) != EOF){
- while( nsc = getchar() ){
- buffer[actIndex] = nsc;
- actIndex ++;
- if(actIndex >= 128 || nsc == EOF){
- n = write( fd, buffer, actIndex );
- actIndex = 0;
- if(nsc == EOF){ printf("%d", nsc); break;}
- }
- }
- //sprintf( buffer, "Current time: %d", (int) time( NULL ) );
- //sprintf(buffer, "proba 12");
- //buffer[0] = 'z';
- //buffer[1] = 'b';
- //while(fgets(
- // 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