inter-process communication between linux and dotnet using mkfifo - c

We have an application written in c that sends events/notifications to an application written in c#. Both applications run on the same linux computer.
The C application:
The C application is Asterisk and we modified the source code (it is open source) so that it can send events to our dotnet console application. The way we currently send events is simply by appending text to a file. For example this is how we send an event that a new peer (ip-phone) connected:
// place this on chan_sip.c
// Example: 1-LN-48T6-E3C5-OFWT|10.0.0.103:5868|189.217.18.244|10216|Z 3.9.32144 r32121
if(!ast_sockaddr_isnull(&peer->addr))
{
// lock
ast_mutex_lock(&some_lock);
// write to file
FILE *pFile;
pFile=fopen("/var/log/asterisk/peer-subscriptions.txt", "a");
if(pFile==NULL) { perror("Error opening file."); }
else {
fprintf(pFile,"%s|%s|%s|%s|%s\n",
/* 1-LN-48T6-E3C5-OFWT */ peer->name,
/* 10.0.0.103:5868 */ pvt->initviasentby,
/* 189.217.18.244 */ ast_sockaddr_stringify_addr(&peer->addr),
/* 10216 */ ast_strdupa(ast_sockaddr_stringify_port(&peer->addr)),
/* Z 3.9.32144 */ peer->useragent
// Other:
// peer->fullcontact, // sip:1-LN-48T6-E3C5-OFWT#189.217.18.244:10216;rinstance=8b4135488f735cbf;transport=UDP
// pvt->via // SIP/2.0/UDP 54.81.92.135:20001;branch=z9hG4bK58525e18;rport
);
}
fclose(pFile);
// unlock
ast_mutex_lock(&some_lock);
}
The C# application
The c# application is a console application that opens that file for reading events nothing fancy.
So basically the C application is writing to a text file and the c# application is reading from that text file.
Question
Over time the file gets to large and I do not want to get in the trouble of truncating it and creating another lock meanwhile it truncates etc... making use of mkfifo seems to be exactly what I want. Since I am relatively new to linux I want to make sure I understand how it works before using it. I know the basics of C (I am no expert) and will like to use a more efficient approach. Do you guys recommend using mkfifo, namedpipes or tcp?
Example 1:
mkfifo works amazing with few lines but when I try to read a lot of lines it fails. Take this example:
mkfifo foo.pipe # create a file of type pipe
On terminal one write to that file
echo "hello world" >> foo.pipe # writes hello world AND blocks until someone READS from it
On a separate terminal I do:
cat foo.pipe # it will output hello world. This will block too until someone WRITES to that file
Example 2:
mkfifo foo.pipe # create a file of type pipe. If it exists already do not create again
On terminal 1 read from that file
tail -f foo.pipe # similar to cat foo.pipe but it keeps reading
On Terminal 2 write to that file but a lot of data
echo ~/.bashrc >> foo.pipe # write the content of file ~/.bashrc to that file
This does not work and only a few lines of that file are displayed on the console. How can I make proper use of mkfifo in order to read all text? Should I use a different approach and use tcp instead?

I would use an AF_UNIX socket connection.

I just ended using tcp. I am sending 10,000 short messages in 10 seconds with no problem.
C code (client)
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
int send_data(void)
{
int socket_desc;
struct sockaddr_in server;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket \n");
return 1;
}
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 11234 );
//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
printf("connect error \n");
close(socket_desc);
return 2;
}
char *message;
message = "hello world";
if( send(socket_desc , message , strlen(message) , 0) < 0)
{
printf("Send failed \n");
close(socket_desc);
return 3;
}
close(socket_desc);
return 0;
}
int main(int argc , char *argv[])
{
// send 1000 messages
for(int i=0; i<1000; i++)
{
send_data();
// 10 milliseconds
usleep(10000);
}
return 0;
}
C# code (server)
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class Ipc_Tcp
{
// Thread signal.
public static ManualResetEvent _semaphore = new ManualResetEvent(false);
// maximum length of the pending connections queue.
const int _max_length_pending_connections_queue = 50;
const ushort _port = 11234;
static int _counter = 0;
public static void StartListening()
{
IPEndPoint localEndPoint = new IPEndPoint(System.Net.IPAddress.Loopback, _port);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(_max_length_pending_connections_queue);
Console.WriteLine("Waiting for a connection...");
while (true)
{
// Set the event to nonsignaled state.
_semaphore.Reset();
// Start an asynchronous socket to listen for connections.
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
// Wait until a connection is made before continuing.
_semaphore.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine("Something bad happened:");
Console.WriteLine(e.ToString());
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
}
// On new connection
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
_semaphore.Set();
var cntr = Interlocked.Increment(ref _counter);
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket socket = listener.EndAccept(ar);
var data = new byte[1024];
var i = socket.Receive(data);
// print message every 100 times
if (cntr % 100 == 0)
Console.WriteLine($"[{cntr}] Received data: {System.Text.Encoding.UTF8.GetString(data, 0, i)}");
// close socket we are only receiving events
socket.Close();
}
public static int Main(String[] args)
{
StartListening();
return 0;
}
}

As #resiliware stated it is probably best to use a unix socket.
This example shows how to communicate between C and C# using a unix socket:
Client (written in C running on ubuntu)
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<unistd.h>
int send_data(void)
{
int sock;
int conn;
struct sockaddr saddr = {AF_UNIX, "/tmp/foo.sock"};
socklen_t saddrlen = sizeof(struct sockaddr) + 6;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
conn = connect(sock, &saddr, saddrlen);
char BUFF[1024];
char *message;
message = "hello world";
if( send(sock , message , strlen(message) , 0) < 0)
{
printf("Send failed \n");
close(sock);
return 3;
}
// I am not sure if I should close both or only the socket.
close(conn);
close(sock);
return 0;
}
int main(int argc , char *argv[])
{
// send 5000 messages
for(int i=0; i<4000; i++)
{
send_data();
// sleep 1 millisecond
usleep(1000);
}
return 0;
}
Server (written in C# running on same ubuntu machine)
using System;
using System.Net.Sockets;
using System.Threading;
class Program
{
// unix Endpoint that we will use
const string path = "/tmp/foo.sock";
// Thread signal.
public static ManualResetEvent _semaphore = new ManualResetEvent(false);
// maximum length of the pending connections queue.
const int _max_length_pending_connections_queue = 100;
// Counts the number of messages received
static int _counter = 0;
public static void StartListening()
{
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
// create unix socket
var listener = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
try
{
// listener.Bind(localEndPoint);
listener.Bind(new UnixDomainSocketEndPoint(path));
listener.Listen(_max_length_pending_connections_queue);
Console.WriteLine("Waiting for a connection...");
// keep listening for connections
while (true)
{
// Set the event to nonsignaled state.
_semaphore.Reset();
// Start an asynchronous socket to listen for connections.
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
// Wait until a connection is made before continuing.
_semaphore.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine("Something bad happened:");
Console.WriteLine(e.ToString());
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
}
// On new connection
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
_semaphore.Set();
var cntr = Interlocked.Increment(ref _counter);
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket socket = listener.EndAccept(ar);
var data = new byte[1024];
var i = socket.Receive(data);
// print message every 100 times
//if (cntr % 100 == 0)
Console.WriteLine($"[{cntr}] Received data: {System.Text.Encoding.UTF8.GetString(data, 0, i)}");
// close socket we are only receiving events
socket.Close();
}
static void Main(string[] args)
{
StartListening();
}
}
Client (If you will like the code of the client to be written on C# instead of C)
using (var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
{
socket.Connect(new UnixDomainSocketEndPoint(path));
// send hello world
var dataToSend = System.Text.Encoding.UTF8.GetBytes("Hello-world!");
socket.Send(dataToSend);
}

Related

How to establish a continuous Bluetooth connection with BlueZ so I can transfer to and receive data from an Arduino?

I've been making a map making robot car with Arduino for class. I want to make a user interface for it in C (on a PC running Linux) that would work like this: the user can press a Start and a Stop button, or click a specific area of the map to send the robot to there. Right now my test setup code looks like this:
Arduino:
`
if (BTSerial.available() > 0) {
c = BTSerial.readStringUntil('\n').toInt();
BTSerial.write(c);
if(c == 8) {
Buzzing(SOS);
BTSerial.println("eight");
}
}
**PC program**:
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 };
int s, status;
char dest[18] = "98:DA:60:03:F2:92";
// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( dest, &addr.rc_bdaddr );
// connect to server
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
// send a message
if( status == 0 ) {
status = write(s, "8", 2);
}
if( status < 0 ) perror("uh oh");
int client, bytes_read;
char buf[1024] = { 0 };
// put socket into listening mode
listen(s, 1);
// read data from the client
bytes_read = read(client, buf, sizeof(buf));
if( bytes_read > 0 ) {
printf("received [%s]\n", buf);
}
close(s);
return 0;
}
`
Ideally if I send the number 8 to the Arduino it would send back the string "eight". When I run my PC program, my PC connects to the Arduino (I get a notification from the OS that my PC is connected and also the led on my HC-06 Bluetooth module connected to the Arduino stops blinking signaling that a device was connected to it) and the buzzer connected to the Arduino starts buzzing the morse code of SOS as expected. However after a second my program terminates, the Bluetooth connection ends (I get a notification that my PC is disconnected and the led on the Bluetooth module starts blinking again) and I don't get back the expected "eight" string.
I'm still just a beginner when it comes to the C language and since I can not find a detailed documentation of BlueZ, I'm kind of stuck. Any help would be greatly appreciated!
I tried to combine the server and the client code from this site: https://people.csail.mit.edu/albert/bluez-intro/x502.html#rfcomm-server.c
I also tested my code on the Arduino using Putty on PC and it worked with it properly.
Calling listen on the socket doesn't do what you think it does. Listening does not mean "wait for data". It means "wait for connect". And you cannot read from the listening socket; you can only accept the connection.
Your socket is already connected. Don't listen. Just read.
So after a bit of work I finally could get it working. I only needed to change the first parameter of the read() function. Here's my final code:
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 }, rem_addr = { 0 };
int s, status;
char dest[18] = "98:DA:60:03:F2:92";
// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( dest, &addr.rc_bdaddr );
// connect to server
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
// send a message
if( status == 0 ) {
status = write(s, "8", 2);
}
if( status < 0 ) perror("uh oh");
int bytes_read;
char buf[1024] = { 0 };
// read data from the client
bytes_read = read(s, buf, sizeof(buf));
if( bytes_read > 0 ) {
printf("%s", buf);
}
close(s);
return 0;
}
This code sends the number "8" to the Arduino, to which the Arduino replies with the string "eight". It's probably not the nicest C code for Bluetooth connection, but at least it's working I guess.

uv_start_read on windows named pipe client waits until the server closes its pipe and results in EOF

I have two programs, one that I've written and works as a pipe server and uses io completion ports .
the other isn't written by me but it's open source and it uses libuv heavily for its async processing .
now I want to edit this program to add the pipe functionality through libuv and to be able to contact the server .
I can connect to the server using uv_pipe_connect and I get the connect callback triggered , then I start reading with uv_read_start which returns 0 (no error) so I expect the allocation callback and read callback get triggered once the server writes to the client .
however when the server writes some bytes and I receive a notification through iocp that an amount of bytes were written, the client doesn't receive anything and no any callbacks are invoked . And on closing the server process the read callback is invoked with error EOF
this is the libuv related code I'm using :
class IPipeListener;
class PipeClient
{
public:
PipeClient(IPipeListener* listener, const std::string& pipe_name);
~PipeClient();
void stop();
private:
static void onAllocBuff(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
static void onConnect(uv_connect_t *connect_req, int result);
static void onRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
static void onWrite(uv_write_t* req, int result);
// a pod structure used to read and write data
PipeCommand command;
IPipeListener* m_listener = nullptr;
uv_pipe_t m_pipe;
uv_connect_t m_connect;
uv_read_t read_req;
uv_write_t write_req;
};
PipeClient::PipeClient(IPipeListener* listener, const std::string& pipe_name)
{
if (!listener || pipe_name.empty())
return;
m_listener = listener;
uv_pipe_init(uv_default_loop(), &m_pipe, 1);
m_connect.data = this;
write_req.data = this;
read_req.data = this;
uv_pipe_connect(&m_connect, &m_pipe, pipe_name.c_str(), onConnect);
}
void PipeClient::onAllocBuff(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
{
MessageBoxA(0, "onAllocBuff", "onAllocBuff", 0);
auto pipe = static_cast<PipeClient*>(handle->data);
buf->base = reinterpret_cast<char*>(&pipe->command);
buf->len = sizeof(pipe->command);
}
void PipeClient::onConnect(uv_connect_t* connect_req, int result)
{
MessageBoxA(0, "onConnect", "onConnect", 0);
auto pipe = static_cast<PipeClient*>(connect_req->data);
if (result < 0)
{
pipe->command.code = PipeCommand::OpCode::Error;
pipe->m_listener->onPipeCommand(pipe->command);
return;
}
MessageBoxA(0, "starting read", "notify", 0);
int r = uv_read_start(connect_req->handle, onAllocBuff, onRead);
if (r != 0)
{
std::string err_msg = "failed to start reading with error : ";
err_msg += uv_err_name(r);
MessageBoxA(0, err_msg.c_str(), "error", 0);
}
}
void PipeClient::onRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
{
MessageBoxA(0, "onRead", "onRead", 0);
auto pipe = static_cast<PipeClient*>(stream->data);
uv_read_stop(stream);
if (nread < 0)
{
std::string err_msg = "failed to read with error : ";
err_msg += uv_err_name(nread);
MessageBoxA(0, err_msg.c_str(), "error", 0);
pipe->command.code = PipeCommand::OpCode::Error;
pipe->m_listener->onPipeCommand(pipe->command);
return;
}
pipe->m_listener->onPipeCommand(pipe->command);
uv_buf_t write_buff;
write_buff.base = reinterpret_cast<char*>(&pipe->command);
write_buff.len = sizeof(pipe->command);
uv_write(&pipe->write_req,
stream, &write_buff, 1, onWrite);
}
I had to set the ipc flag to 0 in uv_pipe_init and set the data of m_pipe to point to my PipeClient class .
from here :
https://github.com/libuv/libuv/blob/v1.x/src/win/pipe.c
I saw that the pipe reads writes aren't ordinary iocp reads writes if the ipc flag is set , instead the take another methods that I didn't want

Websocket connection keeps prematurely closing the connection

So, I am trying to build a C application which functions as a Websockets Server.There are many related questions on here, but none of them seem to be able to help with the problem. The program is able to initially establish a connection, complete the handshake for the websocket successfully, however, cannot seem to be able to keep the connection open. On the client side, I get the error
WebSocket.js:7605 Uncaught Error: Attempt to send message on unopened or closed WebSocket
at _8b2.window.WebSocket._8d2.send (WebSocket.js:7605)
at (index):34
whenever I try to use the send() function on the websocket connection on the client side. And also it gives me the error
WebSocket connection to 'ws://127.0.0.1:5959/?.kl=Y' failed: WebSocket is closed before the connection is established.
Here is the source code of the server:
int new_socket;
int activity;
int i;
int sd;
int max_sd;
int bytes_read;
fd_set readfds;
int master_socket;
noPollConn* new_conn;
/*Create no poll context*/
noPollCtx* ctx = nopoll_ctx_new();
noPollConn* conn;
char buffer[3000];
/*Create a connection listener at 127.0.0.1 (loopback) on port 5959*/
noPollConn * listener = nopoll_listener_new(ctx, "127.0.0.1","5959");
/*Get the socket of the lister*/
master_socket = nopoll_conn_socket(listener);
if(!nopoll_conn_is_ok(listener)) {
perror("There was an error creating the listener!\n");
exit(EXIT_FAILURE);
}
puts("Waiting for connections ...");
while(TRUE){
printf("Start of the while loop\n");
FD_ZERO(&readfds);
FD_SET(master_socket, &readfds);
max_sd = master_socket;
printf("The number of connections is %i\n",cons);
for (i = 0 ; i < cons ; i++) {
sd = nopoll_conn_socket((noPollConn*)vector_get(clients,i));
if(sd > 0)
FD_SET(sd,&readfds);
if(sd > max_sd)
max_sd = sd;
}
printf("The max fd is %i\n",max_sd);
activity = select(max_sd + 1 , &readfds , NULL , NULL , NULL);
if ((activity < 0) && (errno!=EINTR)) {
puts("select error");
exit(EXIT_FAILURE);
}
if (FD_ISSET(master_socket, &readfds)){
new_conn = nopoll_conn_accept(ctx,listener);
puts("Waiting for the connection to be ready");
nopoll_conn_is_ready(conn);
/*Vector is actually a doubly linked list*/
vector_push(&clients,new_conn);
cons++;
}
/*TODO: Implement disconnect*/
for (i = 0; i < cons; i++){
printf("Checking on user %i\n",i);
conn = (noPollConn*)vector_get(clients,i);
sd = nopoll_conn_socket(conn);
if (FD_ISSET(sd, &readfds)){
printf("Receiving info from socket no. %d...\n",sd);
bytes_read = recv(sd,buffer,4000,MSG_DONTWAIT);
buffer[bytes_read] = '\0';
printf("Received the msg --> %s\n",buffer);
}
}
memset(buffer,0,3000);
}
Just a warning though, this code does go in an infinite loop right now as I have not implemented disconnection from the client on the server side.
For the client
<pre><code>
var connection = new WebSocket('ws://127.0.0.1:5959');
connection.onopen = function(){
connection.send("TEST");
alert("Connected");
}
connection.onerror = function(error){
console.log('Error detected: ' + error);
}
connection.onmessage = function (event) {
alert(event.data);
}
connection.close();
</code></pre>
Perhaps I am missing something very important? I have gone through many tutorials, and I can't seem to figure out what the issue is. Any help is greatly appreciated!
Calling .send() in the .onopen() function results in the error:
WebSocket connection to 'ws://127.0.0.1:5959/?.kl=Y' failed: WebSocket is closed before the connection is established.
UPDATE
The error is on the server side. It might be related to me handling the connection as though it were a socket in some regards in the C code.
If no errors are logged at server, the issue could be call to .send() before open event occurs. Call .send() within open event handler. Else, the issue is at server.
const socket = new WebSocket("ws://echo.websocket.org/");
socket.onopen = function(e) {
socket.send("WebSocket rocks");
console.log("self.socket event.type:", e.type);
};
socket.onmessage = function(e) {
console.log(e.data);
};
socket.onerror = function(e) {
console.log("self.socket error", e);
};
socket.onclose = function(e) {
console.log("self.socket event.type", e.type);
};
See How do I test my WebSocket which is developed in JavaScript
plnkr http://plnkr.co/edit/W8Wgyw0mbxdMkMMe4wg4?p=preview

libuv tcp connections issue

I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include "uv.h"
int64_t counter = 0;
void on_new_connection(uv_stream_t *server, int status);
int main(void)
{
uv_tcp_t server;
uv_tcp_init(uv_default_loop(), &server);
struct sockaddr_in socket;
uv_ip4_addr("127.0.0.1", 7000, &socket);
uv_tcp_bind(&server, (struct sockaddr *) &socket, 42);
int r = uv_listen((uv_stream_t*) &server, 128,
on_new_connection);
if (r) {
fprintf(stderr, "Listen error\n");
return 1;
}
return uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}
void on_new_connection(uv_stream_t *server, int status)
{
printf("new connection\n");
if (status == -1) {
return;
}
uv_tcp_t *client = malloc(sizeof(uv_tcp_t));
uv_tcp_init(uv_default_loop(), client);
// if (uv_accept(server, (uv_stream_t*) client) == 0) {
// uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
// }
// else {
// uv_close((uv_handle_t*) client, NULL);
// }
}
I just want to start this server and connecting (via telnet or a browser) to this server.
Everything seems ok except that the first connection always print the "new connection" string in the on_new_connection but any new telnet sessions that I start don't print new connection.
What am I missing? it seems that the on_new_connection callback is called only once, why?
Why did you comment out the code with the call to uv_accept()? In a connection callback, the user has to call uv_accept() to accept the pending connection. The server i/o watcher is then resumed after this call. In your case, the watcher doesn't get resumed. That's why you observe a single callback.
Possibly, you are not specifying the correct port with telnet?
telnet 127.0.0.1 7000
You're also not checking the status value. You always get the callback, but you don't know if it's an error of some sort

Executing child process in new terminal

I want to make a simple chat application for unix.
I have created one server which supports multiple clients. When ever a new client connects to the server a new process is created using fork command. Now the problem is all the child processes share the same stdin on the server, cause of this in order to send a message to 2nd clien 1st child prosess has to terminte. In order to resolve this I would like to run each child process in a new terminal.
This can be achieved by writing the code for the child process code in a new file and executing it like xterm -e sh -c .(i have not tried this though).
What i really want is not to have two file just to fireup a new terminal and run rest of the code in it.
int say(int socket)
{
char *s;
fscanf(stdin,"%79s",s);
int result=send(socket,s,strlen(s),0);
return result;
}
int main()
{
int listener_d;
struct sockaddr_in name;
listener_d=socket(PF_INET,SOCK_STREAM,0);
name.sin_family=PF_INET;
name.sin_port=(in_port_t)htons(30000);
name.sin_addr.s_addr=htonl(INADDR_ANY);
int c = bind(listener_d,(struct sockaddr *)&name,sizeof(name)); //Bind
if(c== -1)
{
printf("\nCan't bind to socket\n");
}
if(listen(listener_d,10) == -1) // Listen
{
printf("\nCan't listen\n");
}
puts("\nWait for connection\n");
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d,
(struct sockaddr*)&client_addr,&address_size); //Accept
if(connect_d== -1)
{
printf("\nCan't open secondary socket\n");
}
if(!fork())
{
close(listener_d);
char *msg = "welcome Sweetone\n";
if(send(connect_d,msg,strlen(msg),0))
{
printf("send");
}
int k=0;
while(k<5)
{
say(connect_d);
++k;
}
close(connect_d);
exit(0);
}
close(connect_d);
}
close(listener_d);
return 0;
}
I think the message sending between your client and servers is a bit unusual. It is more common, in this simple "just test how it works" scenario to have the clients sending messages to the server. As an example I could mention a simple echo service, which mirrors everything a client sends, back to the client. Is this design forced by some requirements?
Critique aside, I have two separate changes that could make your current design work. They both involve changing the reading of input in the subservers.
Alternative 1:
Instead of reading from stdin, create a named pipe ( see man 3 mkfifo), fex /tmp/childpipe"pid_of_subserver_here". You could create the pipe in say() and open it for reading. Then use echo (man echo) to write to the pipe echo "My message" > /tmp/childpipe"NNNN". Before exiting the child, remember to remove the pipe with unlink()
Alternative 2:
Create an unnamed pipe between server and each subserver. This makes the code much more messy, but avoids creating named pipes and using echo. Example code is included below. It has insufficient error handling (like most example code) and does not handle disconnecting client properly.
Example usage: 1) start server ./a.out 2) (connect client in external window (e.g. nc localhost 30000) 3) write to client 1 by typing "1Hello client one" 4) (connect second client in third window etc) 4) Write to second client by typing "2Hello second client"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
enum max_childeren{
MAX_CHILDEREN = 50
};
int say(int socket)
{
char buf[513] = {0};
fgets(buf, sizeof(buf), stdin);
int result=send(socket, buf, strlen(buf),0);
return result;
}
int main()
{
int listener_d;
struct sockaddr_in name;
listener_d=socket(PF_INET,SOCK_STREAM,0);
name.sin_family=PF_INET;
name.sin_port=(in_port_t)htons(30000);
name.sin_addr.s_addr=htonl(INADDR_ANY);
int on = 1;
if (setsockopt(listener_d, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0){
perror("setsockopt()");
}
int c = bind(listener_d,(struct sockaddr *)&name,sizeof(name)); //Bind
if(c== -1)
{
printf("\nCan't bind to socket\n");
}
if(listen(listener_d,10) == -1) // Listen
{
printf("\nCan't listen\n");
}
// Edited here
int number_of_childeren = 0;
int pipes[2] = {0};
int child_pipe_write_ends[MAX_CHILDEREN] = {0};
fd_set select_fds;
FD_ZERO(&select_fds);
puts("\nWait for connection\n");
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
// Edited here, to multiplex IO
FD_SET(listener_d, &select_fds);
FD_SET(STDIN_FILENO, &select_fds);
int maxfd = listener_d + 1;
int create_new_child = 0;
int connect_d = -1; // moved here
select(maxfd, &select_fds, NULL, NULL, NULL);
if (FD_ISSET(listener_d, &select_fds)){
connect_d = accept(listener_d,
(struct sockaddr*)&client_addr,&address_size); //Accept
if(connect_d== -1)
{
printf("\nCan't open secondary socket\n");
exit(EXIT_FAILURE);
}
create_new_child = 1;
}
char buf[512] ={0};
char *endptr = NULL;
if (FD_ISSET(STDIN_FILENO, &select_fds)){
fgets(buf, sizeof(buf), stdin);
long int child_num = strtol(buf, &endptr, 10);
if (child_num > 0 && child_num <= number_of_childeren) {
write(child_pipe_write_ends[child_num - 1], endptr, strnlen(buf, sizeof(buf)) - (endptr - buf));
}
else {
printf("Skipping invalid input: %s\n", buf);
}
}
if (create_new_child != 1)
continue;
number_of_childeren++; // Edited here
int error = pipe(pipes);
if (error != 0){
//handle errors
perror("pipe():");
exit(EXIT_FAILURE);
}
child_pipe_write_ends[number_of_childeren - 1] = pipes[1];
if(!fork())
{
error = dup2(pipes[0], STDIN_FILENO);
if (error < 0){ // could also test != STDIN_FILENO but thats confusing
//handle errors
perror("dup2");
exit(EXIT_FAILURE);
}
close(pipes[0]);
close(listener_d);
char *msg = "welcome Sweetone\n";
if(send(connect_d,msg,strlen(msg),0))
{
printf("send\n");
}
int k=0;
while(k<5)
{
say(connect_d);
++k;
}
close(connect_d);
exit(0);
}
close(connect_d);
close(pipes[0]);
}
close(listener_d);
return 0;
}
The code needs refactoring into functions. It is too long. I tried to do the least possible amount of changes, so I left the restructuring as an exercise.
fscanf(stdin,"%79s",s);
Why? Is it tcp-chat? You have some socket for each client and if yoy want to "say" something then you must to use client. It's true logick.
The server usually sends a service messages only. It's true logick too.
But if you want new terminal then you can try to use a exec's family from unistd.h .

Resources