LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c code to make a socket mirror (loopback) (https://www.linuxquestions.org/questions/programming-9/c-code-to-make-a-socket-mirror-loopback-577195/)

saavik 08-15-2007 01:34 AM

c code to make a socket mirror (loopback)
 
Hello!

I ` am not to good on c so I`ll need your help.

I need a socket loopback programm, which sends data to a socket and also listens if there is something send back.

So a loopback.

I found this programm

http://www.pronix.de/pronix-275.html

which actually works fine, in sending the data but it does not recieve data (as it is only designed as a client).

I read on that page (only german, sorry for that) that there a two options.

a) write()
b) read()

So I tought that i would only have to add a read (...) so i get the data which is send from the loop-back-device.

But I am not sure how that line should look or where i must add that line.

I used: socket (AF_INET, SOCK_STREAM, 0)

to open the socket so that should be bidirectional, not ?

Mara 08-15-2007 03:23 PM

The code does receive the data, but it then uses it to read user input (fgets). Use two different buffers and it should be fine. The socket created using socket (AF_INET, SOCK_STREAM, 0) is definitely bidirectional.

You may want to use non-blocking socket and/or select later, however, if blocking becomes a limitation (check further examples in your tutorial).

saavik 08-16-2007 12:55 AM

Ah, well ok!

I got it working!

maybe the code:


Quote:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
int socket_nummer;
int laenge;
struct sockaddr_in adressinfo;
int ergebnis;

char buf[ 100 ];
int sock_client = 0, i;

socket_nummer = socket(AF_INET, SOCK_STREAM, 0);
adressinfo.sin_family = AF_INET;
adressinfo.sin_addr.s_addr = inet_addr("10.10.20.234");
adressinfo.sin_port = htons(4004);
/*adressinfo.sin_port = htons(23);*/
laenge = sizeof(adressinfo);
ergebnis = connect(socket_nummer, (struct sockaddr *)&adressinfo, laenge);
if (ergebnis == 0)
{
printf("\nVerbindungsaufbau erfolgreich");
printf("\n*****************************");
}
else
{
perror("Fehler beim Verbindungsaufbau: ");
}

//sleep(1);
//daten senden
for(i=1; i<10; i++)
{
sleep(1);
printf("\nsende daten\n");
write(socket_nummer, "123456789", i);

// int l = read( socket_nummer, buf, sizeof( buf ) );
// l = write( STDOUT_FILENO, buf, i );

// read data from socket
int l = read( socket_nummer, buf, sizeof( buf ) );
if ( !l )
{
printf( "Client close socket." );
close( sock_client );
sock_client = 0;
}
else if ( l < 0 )
printf( "Unable to read data from socket." );
else
printf( "\n %d bytes vom socket gelesen.", l );

// send all data to stdout
l = write( STDOUT_FILENO, buf, i );
if ( i>8 )
{
i=0;
}

}


close(socket_nummer);
printf("\n\n");
}






All times are GMT -5. The time now is 04:51 PM.