Уже второй день сижу... надо чтобы клинт если что-то на stdin появляется,это считыывал и отправлял серверу, а если что-то приходит от сервера, то выдавал это на консоль. Сервер в свою очередь должен слать клиету,то что получил от него... у меня после получения данных от сервера select постояннопотом пытаеться считать с сокета.. получает 0 байт в бесконечном цикле.
:conf used:
client.c
Код:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define BUFFER_SIZE (1<<16)
int main( int argc , char *argv [] )
{
int sd,n,done=0;
struct sockaddr_in sa;
time_t stime = 0;
char buf[BUFFER_SIZE];
fd_set rset;
if( argc != 2 )
{
printf( "Usage: %s ipv4 -address\n", argv [0] );
exit( EXIT_FAILURE );
}
/* TCP Socket anlegen */
if( ( sd = socket( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
{
printf( "socket () failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
}
memset( &sa , 0, sizeof( sa ) );
sa.sin_family = AF_INET; /* IPv4 */
sa.sin_port = htons( 13); /* Time Server Port */
if( inet_pton( AF_INET , argv [1], &sa.sin_addr ) != 1 )
{
printf( "inet_pton () failed .\n" );
close( sd );
exit( EXIT_FAILURE );
}
if( connect( sd , (struct sockaddr *)&sa ,sizeof( sa ) ) < 0 )
{
printf( "connect () failed: %s\n", strerror( errno ) );
close( sd );
exit( EXIT_FAILURE );
}
FD_ZERO(&rset);
while(!done) {
FD_SET(0, &rset);
FD_SET(sd, &rset);
printf("Selecting\n");
select(sd + 1, &rset, NULL, NULL, NULL);
if (FD_ISSET(0, &rset)) {
printf("reading from stdin\n");
bzero(buf, BUFFER_SIZE);
n = read(0, (void *) buf, sizeof(buf));
switch (buf[0]) {
case 'q': /* terminate the server */
done=1;
break;
}
if (n==0)
done = 1;
else
if(( send(sd, (const void *) buf, (size_t) n, 0)) < 0 )
{
printf( "send () failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
}
}
if (FD_ISSET(sd, &rset)) {
printf("Reading from socket\n");
n = recv(sd, (void *) buf, sizeof(buf), 0);
if (n == 0)
done = 1;
//shutdown(sd,0);
printf("0 Bytes");
else
printf("%.*s", (int)n, buf);
}
}
close(sd);
exit( EXIT_SUCCESS );
}
server.c
Код:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 13
#define BACKLOG 32
#define BUFFER_SIZE (1<<16)
int main( int argc , char *argv [] )
{
int sd , client;
struct sockaddr_in sa;
int n;
char buf[BUFFER_SIZE];
if( ( sd = socket( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
{
printf( "socket () failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
}
memset( &sa , 0, sizeof( sa ) );
sa.sin_family = AF_INET; /* IPv4 */
sa.sin_port = htons( PORT ); /* Time Server Port */
sa.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); /* Wildcard */
if( (bind ( sd , (struct sockaddr *)&sa ,sizeof( sa ) )) < 0 )
{
printf( "bind() failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
}
if( (listen( sd , BACKLOG )) < 0 )
{
printf( "listen () failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
}
for ( ; ; )
{
printf("Start..\n");
client = accept( sd , NULL , NULL );
if (client < 0 )
{
printf( "accept () failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
}
bzero(buf, BUFFER_SIZE);
n = read(client, (void*)buf , sizeof( buf) );
if( n < 0 )
{
printf( "read () failed: %s\n", strerror( errno ) );
exit( EXIT_FAILURE );
} else
printf("Empfangebe Daten: %s\n",buf);
send( client , &buf, sizeof( buf),0 );
printf("Gesendet\n");
//shutdown(client,2);
close( client );
}
close(sd);
}