LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 08-15-2007, 01:34 AM   #1
saavik
Member
 
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES11 / FC20/ OES / CentOS
Posts: 595

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

Rep: Reputation: 147Reputation: 147
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
saavik
Member
 
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES11 / FC20/ OES / CentOS
Posts: 595

Original Poster
Rep: Reputation: 32
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");
}



 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 11:17 PM.

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