Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-15-2007, 01:34 AM   #1
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES / FC/ OES / CentOS
Posts: 611

Rep: Reputation: 32
c code to make a socket mirror (loopback)


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

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 ?
Old 08-15-2007, 03:23 PM   #2
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,571

Rep: Reputation: 180Reputation: 180
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).
Old 08-16-2007, 12:55 AM   #3
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES / FC/ OES / CentOS
Posts: 611

Original Poster
Rep: Reputation: 32
Ah, well ok!

I got it working!

maybe the code:

#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("");
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");
perror("Fehler beim Verbindungsaufbau: ");

//daten senden
for(i=1; i<10; i++)
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." );
printf( "\n %d bytes vom socket gelesen.", l );

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





Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for developing a device driver code for loopback interface in linux sandeepvl Programming 2 01-14-2007 01:46 AM
optimize socket networking code powah Linux - Networking 0 09-26-2006 04:47 PM
Linux Socket Programming source code saied Programming 4 05-24-2005 02:18 AM
Problems in compiling socket code in c Prakhardeep Programming 2 12-22-2004 09:45 AM
Why is my socket unable to bind to the port in the C Code. mcp_achindra Programming 10 03-24-2004 08:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration