LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 08-14-2017, 02:55 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Connecting client socket to server socket only once in socket programming


I am trying to connect client socket to server socket only once when program starts. After that it should only send and receive.

Also I written this client program inside my server socket.Since my requirement is my server will receive some data from original client and send same data(as client) to another server and receive from server and send same data to original client

Three things are
1. Original client(Some device which act as client you can say)
2. Server(which also contains client like program in thread to send and receive)
3. External Server( Which I will hit from second step)


The code I am having for second step is shown below link.
https://pastebin.com/UmPQJ70w

My question is more related to C++ Where exactly I need to put below code in program.

Code:
 TCPConnector* connector_client = new TCPConnector();
        printf("ip_client = %s\tport_client = %s\tport_client_int = %d\n", ip_client.c_str(), port_client.c_str(),atoi(port_client.c_str()));
        TCPStream* stream_client = connector_client->connect(ip_client.c_str(), atoi(port_client.c_str()));
-- If I am writing inside While loop it works but it connect every time.
-- If I write outside for loop as shown in paste bin. It works only one time after that due to delete stream_client; as shown below in code. If I remove delete stream_client it works but memory will not free.

Code:
-------
-------
--------
len = stream_client->receive(line, sizeof(line));
line[len] = NULL;
printf("received - %s\n", line);
delete stream_client;
So what is the smartest way I can handle in my application.
 
Old 08-14-2017, 04:57 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
This thing is called 'proxy'. Here is two way you could choose:

1. for every incoming connection create an outgoing connection -- that's how rinetd works.

2. share one (or a few) outgoing connection between more incoming connection, if the protocol allows that. (cf: multiplexing)
 
2 members found this post helpful.
Old 08-16-2017, 06:37 AM   #3
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
Hi Nevem,

I found point no.2 useful for my project.Is there is any sample code to perform this or any document I can refer.
 
Old 08-17-2017, 04:09 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
You should store the data of your outgoing connections, sg like this:

Code:
typedef struct OutConnData {
    int state;        /* -1/0/1 = unused/free/in-use */
    int sockethandle; /* -1 if unused */
    ...
} OutConnData;

typedef struct OutConnData_Collection {
    OutConnData *ptr; /* pointer to n_allocated elements */
    int n_allocated;
    int n_unsed;
    int n_free;
} OutConnData_Collection;
from now on it is easy: you only have to write the operations of these structures
 
Old 08-19-2017, 12:18 AM   #5
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
Hi Nevem,
Thanks for giving few inputs.
After doing little bit research I found rinetd can solve problem without writing any extra code(but keeping my original code as option for time being). I am facing two problems.
==>Question 1
I am running server socket program in one computer ./server_local 5000 192.168.1.7 and hitting from client ./client_local 5000 192.168.1.7

and made configuration in /etc/rinetd.conf
PHP Code:
#bind address    bindport     connectaddress   connectport
192.168.1.7       5000        202.52.134.163    5010 
rinetd restared.
and running remote server socket in ./server_remote 1 5010 10.2.4.102. So from any client program if hit giving ip address as 202.52.134.163 and port number 5010 I am able to send data to this server.

Now when I hit from client_local to server_local I am thinking rinetd will redirect from (192.168.1.7:5000) to 202.52.134.163:5010 and get data in this server. But it is not happening. Is my understanding of rinetd wrong or any configuration issue.
Note:- Pardon me for asking networking related question in programming forum.Since it is related to original question not opening new thread

==> Question 2:-
My aim is to do connection only once i.e TCP connection should be established only once after that send receive should happen .Whether rinetd solve this problem. Because my remote server connects only once and if we try to connect in parallel many connection shows connection refused.
I tried to read documents regarding this but not getting exact solution. So I am trying to work on working example as mentioned in Question 1

Last edited by srinietrx; 08-19-2017 at 12:44 AM.
 
Old 08-20-2017, 11:53 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
I don't really get your example. Let's assume you have three computers, A, B, and C, with the following IP-addresses:
Code:
1.1.1.1 A
2.2.2.2 B
3.3.3.3 C
Now let A be the client, B the proxy, C the server (port 3333). You should run rinetd on B, with the following parameters:
Code:
# host_B:/etc/rinetd.conf
#bind_address bindport connect_address connect_port
2.2.2.2       4444     3.3.3.3         3333
Now the client on 'A' can connect to 'B' (i.e. 2.2.2.2:4444), and it will be forwarded to 'C' (i.e. 3.3.3.3:3333)

Last edited by NevemTeve; 08-22-2017 at 02:39 AM. Reason: more comment
 
  


Reply

Tags
c++, sockets, tcp



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
How to transfer hexadecimal data from client to server(socket programming)..? vktafs Programming 1 11-05-2012 05:50 PM
[SOLVED] How to connect a java client to a BSD server using socket programming sanju.lnt Programming 2 08-06-2011 02:57 PM
running socket programming (client/server) on single computer rahb20 Linux - Networking 5 09-16-2008 11:09 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
how to implement fork() in socket programming b/w server/client in voice communicatio timeout4me Programming 1 08-05-2005 04:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration