Multithreaded FTP server socket VC++ - multithreading

I am writing a basic FTP client server code in VC++ with multithreading.
The code works fine with single client but it doesn't work for 2 clients. I don't know how socket keep information for multiple clients.
If I do CD from on client it reflects in other one too. I mean if two different clients cannot work on different server directory.
May be something very basic I am missing here due to my lack of knowledge of socket programming.
code to accept connections:
void acceptUserConnections()
{
calen=sizeof(ca);
int thread_count = 0;
thread t[num_threads];
while(1)
{
cout<<"Accepting user connections: "<<endl;
if((cs=accept(serverSocket,&ca.generic,&calen))==INVALID_SOCKET)
throw "Couldn't accept connection\n";
string userAddress(inet_ntoa(ca.ca_in.sin_addr));
thread_count = thread_count +1;
t[thread_count] = thread(handleUserConnection, cs);
t[thread_count].detach();
//handleUserConnection(clientSocket);
}
}
}
to handle:
void handleUserConnection(SOCKET clientSocket)
{
if (strcmpi(command.c_str(),"PWD")==0)
{
ftpPWD(clientSocket);
}
}
for CD
void ftpCD(string directory, SOCKET clientSocket)
{
memset(szbuffer,'\0',1024);
if(!SetCurrentDirectory(directory.c_str()))
{
cout<<strerror(errno)<<endl;
sprintf(szbuffer,"System cannot find the specified directory.");
if(send(clientSocket,szbuffer,1024,0) == SOCKET_ERROR)
throw SEND_FAILED_MSG;
}
else
{
sprintf(szbuffer,"Directory changed successfully.");
if(send(clientSocket,szbuffer,1024,0) == SOCKET_ERROR)
throw SEND_FAILED_MSG;
}
}
for PWD
void ftpPWD(SOCKET clientSocket)
{
memset(szbuffer,'\0',1024);
int nBufferLength =GetCurrentDirectory(MAX_PATH, pwd);
if(!nBufferLength)
{
sprintf(szbuffer,"Failed to get current directory");
if(send(clientSocket,szbuffer,1024,0) == SOCKET_ERROR )
throw SEND_FAILED_MSG;
}
else
{
sprintf(szbuffer,(string(pwd)).c_str());
if(send(clientSocket,szbuffer,1024,0) == SOCKET_ERROR )
throw SEND_FAILED_MSG;
}
}
}

Related

CAN socketCAN - how reliable is for file transfer

I want to send files (mp3 about 4MB each) to devices connected via CAN. I'm testing with Linux machine and ESP32. The problem is that not all data arrives to destination.
I'm sending from Linux machine using socketCAN and UCCB USB-CAN converter
https://www.tindie.com/products/lll7/can-usb-converter-uccb/ to ESP32, or between two ESP32.
When sending from PC Linux to ESP32, sometimes only 1/3 of mp3 file is saved, during sending I have candump slcan0 command running and it looks like all data is on the bus, but ESP32 does not see some frames.
When I send between two ESP32, for instance 4MB of data, receiving ESP32 gets around 3,98MB of data, some frames gets lost.
I'm using command "slcand -o -c -f -s8 /dev/ttyACM0 slcan0" to create slcan0 interface, although changing the speed with -s switch does not seem to work.
Am I missing something, or CAN is not suitable for such high speed, high load operations? I want to have 30 devices on canbus that receive around 1GB of data (mp3 files)
code below:
CanMgr::CanMgr(QObject *parent,LogModel *logModel):QObject(parent) {
this->logModel=logModel;
send_timer=new QTimer(this);
send_timer->setSingleShot(true);
send_timer->setInterval(1);
connect(send_timer,&QTimer::timeout,this,&CanMgr::processQueue);
}
void CanMgr::connectDevice() {
QString errorString;
m_canDevice = QCanBus::instance()->createDevice(m_pluginName,m_socketName,&errorString);
if (!m_canDevice) {
qDebug()<<QString("Error creating device '%1', reason: '%2'").arg(m_pluginName).arg(errorString);
return;
}
m_numberFramesWritten = 0;
connect(m_canDevice, &QCanBusDevice::errorOccurred, this, &CanMgr::processErrors);
connect(m_canDevice, &QCanBusDevice::framesReceived, this, &CanMgr::processReceivedFrames);
connect(m_canDevice, &QCanBusDevice::framesWritten, this, &CanMgr::processFramesWritten);
connect(m_canDevice, &QCanBusDevice::stateChanged,this, &CanMgr::stateChanged);
m_canDevice->setConfigurationParameter(QCanBusDevice::BitRateKey,"250000");
if (!m_canDevice->connectDevice()) {
qDebug()<<tr("Connection error: %1").arg(m_canDevice->errorString());
delete m_canDevice;
m_canDevice = nullptr;
} else {
QVariant bitRate = m_canDevice->configurationParameter(QCanBusDevice::BitRateKey);
if (bitRate.isValid()) {
qDebug()<<tr("Plugin: %1, connected to %2 at %3 kBit/s").arg(m_pluginName).arg(m_socketName).arg(bitRate.toInt() / 1000);
} else {
qDebug()<<tr("Plugin: %1, connected to %2").arg(m_pluginName).arg(m_socketName);
}
}
}
void CanMgr::sendFileContent(QByteArray data) {
quint32 frameId = 0;
quint32 dev_id=2;
quint32 cmd_id=0;
frameId=(dev_id<<16) | cmd_id;
m_canDevice->clear();
QByteArray size=Helper::byteArrFromInt((quint32)data.size(),8);
qDebug()<<"CanMgr::sendFileContent file size in bytes:"<<Helper::printHex(size);
QCanBusFrame frame = QCanBusFrame(frameId,size);
frame.setExtendedFrameFormat(true);
qDebug()<<"frame data:"<<frame.toString()<<" stat:"<<m_canDevice->state();
queue.append(frame);
frameId = 0;
dev_id=2;
cmd_id=1;
frameId=(dev_id<<16) | cmd_id;
for(int i=0;i<data.size();i+=8) {
QCanBusFrame frame = QCanBusFrame(frameId, data.mid(i,8));
frame.setExtendedFrameFormat(true);
queue.append(frame);
}
frameId = 0;
dev_id=2;
cmd_id=2;
frameId=(dev_id<<16) | cmd_id;
frame = QCanBusFrame(frameId, size);
frame.setExtendedFrameFormat(true);
queue.append(frame);
process_frame=false;
send_timer->start();
}
void CanMgr::processQueue() {
if(queue.isEmpty()) {
qDebug()<<"CanMgr::processQueu queue empty";
return;
}
if(process_frame) {
;
}
else {
curr_frame=queue.dequeue();
process_frame=true;
}
qDebug()<<"CanMgr::processQueue frame data:"<<curr_frame.toString()<<" towrite:"<<m_canDevice->framesToWrite()<<" left:"<<queue.count();
m_canDevice->writeFrame(curr_frame);
}
void CanMgr::processFramesWritten(qint64 count) {
qDebug()<<"CanMgr::processFramesWritten count:"<<count;
process_frame=false;
this->processQueue();
}
QString CanMgr::processErrors(QCanBusDevice::CanBusError error) const
{
QString err_str;
switch (error) {
case QCanBusDevice::ReadError:
case QCanBusDevice::WriteError:
case QCanBusDevice::ConnectionError:
case QCanBusDevice::ConfigurationError:
case QCanBusDevice::UnknownError:
err_str= m_canDevice->errorString();
qDebug()<<"Error:"<<err_str;
send_timer->start();
return err_str;
break;
default:
break;
}
}
Best,
Marek

C++/CLI code doesn't enter .then part of the create_task function

This is the code that I have:
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
XTRACE(L"=========================================will start onNavigated ");
auto mediaCapture = ref new Windows::Media::Capture::MediaCapture();
m_mediaCaptureMgr = mediaCapture;
IAsyncAction ^asyncAction = m_mediaCaptureMgr->InitializeAsync();
try
{
create_task(asyncAction).then([this](task<void> initTask)
{
XTRACE(L"=========================================will start onNavigated 2");
try
{
initTask.get();
auto mediaCapture = m_mediaCaptureMgr.Get();
if (mediaCapture->MediaCaptureSettings->VideoDeviceId != nullptr && mediaCapture->MediaCaptureSettings->AudioDeviceId != nullptr)
{
String ^fileName;
fileName = VIDEO_FILE_NAME;
XTRACE(L"=================================Device initialized successful\n");
create_task(KnownFolders::VideosLibrary->CreateFileAsync(fileName, Windows::Storage::CreationCollisionOption::GenerateUniqueName))
.then([this](task<StorageFile^> fileTask)
{
XTRACE(L"=================================Create record file successful\n");
m_recordStorageFile = fileTask.get();
MediaEncodingProfile^ recordProfile = nullptr;
recordProfile = MediaEncodingProfile::CreateMp4(Windows::Media::MediaProperties::VideoEncodingQuality::Auto);
stream = ref new InMemoryRandomAccessStream();
return m_mediaCaptureMgr->StartRecordToStreamAsync(recordProfile, stream);
}).then([this](task<void> recordTask)
{
try
{
recordTask.get();
XTRACE(L"=================================Start Record successful\n");
}
catch (Exception ^e)
{
XTRACE(L"======ERRROR is : %d", e);
}
});
}
else
{
XTRACE(L"=================================No VideoDevice/AudioDevice Found\n");
}
XTRACE(L"=================================WILL EXIT \n");
}
catch (Exception ^ e)
{
XTRACE(L"============================================ERROR IS: %d\n", e);
}
}
);
}
catch (Exception ^ e)
{
XTRACE(L"============================================before create task ERROR IS: %d\n", e);
}
XTRACE(L"=================================SLEEP 20000====================================\n");
std::chrono::milliseconds dura(20000);
std::this_thread::sleep_for(dura);
XTRACE(L"=================================TIME ENDED====================================\n");
// stop device detection
try
{
XTRACE(L"=================================Stopping Record\n");
create_task(m_mediaCaptureMgr->StopRecordAsync())
.then([this](task<void> recordTask)
{
try
{
recordTask.get();
XTRACE(L"=================================Stop record successful: %d\n", stream->Size);
}
catch (Exception ^e)
{
XTRACE(L"=================================ERROR while stoping 2: %d\n", e);
}
});
}
catch (Exception ^e)
{
XTRACE(L"=================================ERROR try catch 3 stoping: %d\n", e);
}
CloseHandle(ghEvent);
// destruct the device manager
XTRACE(L"=====================================================================END\n");
}
In the log I see:
=========================================will start onNavigated
And then directly:
=====================================================================
=================================SLEEP 20000====================================
What is strange is that I had this code in a unittest and it worked, I created a new windows phone project with a UI and it doesn't work
Apparently it was because of the ThreadSleep:
std::chrono::milliseconds dura(20000);
std::this_thread::sleep_for(dura);
In the library project where I was working I was using a different sleep, provided by the library, and that would work. So this was the line that would cause the application to block itself.
I worked my way around this by including 2 buttons in the app, for the start and stop recording.

NetMQ in Publisher and subscriber not working

I'm trying to implement the NetMQ Pub/Sub Model, but the Subscriber is not receiving any messages. What possibly is wrong here?
private static void ServerTask()
{
using (var context = NetMQContext.Create())
{
using (var socket = context.CreateSubscriberSocket())
{
socket.Bind("tcp://10.120.19.109:5000");
socket.Subscribe(string.Empty);
while (true)
{
Thread.Sleep(100);
string receivedMessage = socket.ReceiveString();
Console.WriteLine("Received: " + receivedMessage);
}
}
}
}
public static void ClientTask()
{
using (NetMQContext ctx = NetMQContext.Create())
{
using (var socket = ctx.CreatePublisherSocket())
{
socket.Connect("tcp://10.120.19.109:5000");
string obj = "hi";
socket.Send(obj);
}
}
}
Both are in different apps.
If you are new to NetMQ I suggest reading the zeromq guide http://zguide.zeromq.org/page:all.
Bottom line is that you are sending the message before the subscriber sent the subscription.
Pubsub in zeromq and NetMQ is like radio, you will only get messages from the moment you start listen.
To simple way to do it (not a real life solution) is to sleep for some time after the connect.
For real life solution I need to understand what are you trying to achieve
issue is like
using (NetMQContext ctx = NetMQContext.Create())
{
using (var publisher = ctx.CreatePushSocket())
{
publisher.Bind("tcp://localhost:5000");
int i = 0;
while (true)
{
try
{
publisher.Send(i.ToString(), dontWait:true);
Console.WriteLine(i.ToString());
}
catch (Exception e)
{
}
finally
{
i++;
}
}
}
Now , this code works. But if I move my while(true) loop outside. and call this code from some other function Which forces push socket and context to be created as new everytime.. this doesnot work.

E_SCN_READINCOMPATIBLE Notification error thrown while scanning bar code on MC9090G

I'm using EMDK 2.5 (VS2008 and VC# and .NetCF3.5) Barcode2 class from the library to write a sample application to scan bar codes. I followed the samples available in EMDK namely CS_Barcode2Sample1 project.Every time I hardware trigger the scan the notification "E_SCN_READINCOMPATIBLE" is thrown and not able to retrieve the scanned data. The documentation doesn't say much about the cause of E_SCN_READINCOMPATIBLE notification and no luck from Google search. I tried several options including making use of Symbol.Barcode and the outcome is same.
I also tried EMDK 2.3 but the result is same.
I've pasted the whole code here....
public partial class Form1 : Form
{
private Barcode2 myBarcode2 = null;
public Form1()
{
InitializeComponent();
InitBarcode();
}
public bool InitBarcode()
{
// If the Barcode2 object is already initialized then fail the initialization.
if (myBarcode2 != null)
{
return false;
}
else // Else initialize the reader.
{
try
{
Symbol.Barcode2.Device[] AvailableDevices = Symbol.Barcode2.Devices.SupportedDevices;
if (AvailableDevices.Length == 0)
{
return false;
}
if (AvailableDevices.Length == 1)
{
//get the first available scanner in the list
Symbol.Barcode2.Device MyDevice = AvailableDevices[0];
// Create the reader, based on selected device.
myBarcode2 = new Barcode2(MyDevice);
// Attach a scan notification handler.
//this.myScanNotifyHandler = new Barcode2.OnScanHandler(myBarcode2_ScanNotify);
myBarcode2.OnScan += myBarcode2_ScanNotify;
// Attach a status notification handler.
//this.myStatusNotifyHandler = new Barcode2.OnStatusHandler(myBarcode2_StatusNotify);
myBarcode2.OnStatus += myBarcode2_StatusNotify;
myBarcode2.Config.TriggerMode = TRIGGERMODES.HARD;
// Submit a scan.
myBarcode2.Scan(5000);
}
}
catch (OperationFailureException ex)
{
MessageBox.Show("Exception Raised 1");
return false;
}
catch (InvalidRequestException ex)
{
MessageBox.Show("Exception Raised 2");
return false;
}
catch (InvalidIndexerException ex)
{
MessageBox.Show("Exception Raised 3");
return false;
}
}
return false;
}
private void myBarcode2_ScanNotify(ScanDataCollection scanDataCollection)
{
// Checks if the BeginInvoke method is required because the OnScan delegate is called by a different thread
if (this.InvokeRequired)
{
// Executes the OnScan delegate asynchronously on the main thread
this.BeginInvoke(new Barcode2.OnScanHandler(myBarcode2_ScanNotify), new object[] { scanDataCollection });
}
else
{
// Get ScanData
ScanData scanData = scanDataCollection.GetFirst;
int i;
switch (scanData.Result)
{
case Symbol.Barcode2.Results.SUCCESS:
String str = scanData.Text;
myBarcode2.Config.TriggerMode = TRIGGERMODES.HARD;
myBarcode2.Scan(5000);
break;
case Symbol.Barcode2.Results.E_SCN_READTIMEOUT:
break;
case Symbol.Barcode2.Results.CANCELED:
break;
case Symbol.Barcode2.Results.E_SCN_DEVICEFAILURE:
i = 93;
break;
default:
if (scanData.Result == Symbol.Barcode2.Results.E_SCN_READINCOMPATIBLE)
{
// If the failure is E_SCN_READINCOMPATIBLE, exit the application.
MessageBox.Show("Fatal Error");
this.Close();
return;
}
break;
}
}
}
private void myBarcode2_StatusNotify(StatusData statusData)
{
// Checks if the Invoke method is required because the OnStatus delegate is called by a different thread
if (this.InvokeRequired)
{
// Executes the OnStatus delegate on the main thread
this.Invoke(new Barcode2.OnStatusHandler(myBarcode2_StatusNotify), new object[] { statusData });
}
else
{
int i;
switch (statusData.State)
{
case States.IDLE:
break;
case States.READY:
break;
default:
break;
}
}
}
}
}
I've went thru this recently also, as I observed, it probably due to the scanner device is occupied by other application, where the scan request has been queued already, you can go to memory management, and kill the suspect app, and try your app again.
Refer to the Symbol FAQ

Natively buffering audio

This probably is one of my own mistakes, but I can't seem to find what is wrong. After trying to improve performance of my application, I moved audio buffering from the Java layer to the native layer. Audio handling (recording/playing) is already done natively using the OpenSL ES API.
Yet the native buffering is causing my application to crash whenever I start the application. I use a simple Queue implementation as my buffer, where the first node is the oldest data (FIFO).
struct bufferNode{
struct bufferNode* next;
jbyte* data;
};
struct bufferQueue{
struct bufferNode* first;
struct bufferNode* last;
int size;
};
The audio data is referred to by the jbyte* in the bufferNode. Access to the Queue is done via these two methods, and is synchronized with a mutex.
void enqueueInBuffer(struct bufferQueue* queue, jbyte* data){
SLresult result;
if(queue != NULL){
if(data != NULL){
result = pthread_mutex_lock(&recMutex);
if(result != 0){
decodeMutexResult(result);
logErr("EnqueueInBuffer", "Unable to acquire recording mutex");
} else {
struct bufferNode* node = (struct bufferNode*)malloc(sizeof(struct bufferNode));
if(node == NULL){
logErr("EnqueueInBuffer", "Insufficient memory available to buffer new audio");
} else {
node->data = data;
if(queue->first == NULL){
queue->first = queue->last = node;
} else {
queue->last->next = node;
queue->last = node;
}
queue->size = queue->size + 1;
node->next = NULL;
}
}
result = pthread_mutex_unlock(&recMutex);
if(result != 0){
decodeMutexResult(result);
logErr("EnqueueInBuffer", "Unable to release recording mutex");
}
} else {
logErr("EnqueueInBuffer", "Data is NULL");
}
} else {
logErr("EnqueueInBuffer", "Queue is NULL");
}
}
void dequeueFromBuffer(struct bufferQueue* queue, jbyte* returnData){
SLresult result;
result = pthread_mutex_lock(&recMutex);
if(result != 0){
decodeMutexResult(result);
logErr("DequeueFromBuffer", "Unable to acquire recording mutex");
} else {
if(queue->first == NULL){
returnData = NULL;
} else {
returnData = queue->first->data;
struct bufferNode* tmp = queue->first;
if(queue->first == queue->last){
queue->first = queue->last = NULL;
} else {
queue->first = queue->first->next;
}
free(tmp);
queue->size = queue->size - 1;
}
}
result = pthread_mutex_unlock(&recMutex);
if(result != 0){
decodeMutexResult(result);
logErr("DequeueFromBuffer", "Unable to release recording mutex");
}
}
Where the log and decode methods are selfdeclared utility methods. Log just logs the message to the logcat, while the decode methods "decode" any error number possible from the previous method call.
Yet I keep getting an error when I try to enqueue audio data. Whenever I call the enqueueInBuffer method, I get a SIGSEGV native error, with code 1 (SEGV_MAPERR). Yet I can't seem to find what is causing the error. Both the Queue and the audio data exist when I try to make the enqueueInBuffer method call (which is done in an OpenSL ES Recorder callback, hence the synchronization).
Is there something else going on what causes the segmentation fault? Probably I am responsible for it, but I can't seem to find the error.
Apparently, this was caused by a line of code I have in my OpenSL ES Recorder callback.
The callback originally looked like this:
void recorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context){
SLresult result;
enqueueInBuffer(&recordingQueue, (*recorderBuffers[queueIndex]));
result = (*bq)->Enqueue(bq, recorderBuffers[queueIndex], RECORDER_FRAMES * sizeof(jbyte));
if(checkError(result, "RecorderCallB", "Unable to enqueue new buffer on recorder") == -1){
return;
}
queueIndex = queueIndex++ % MAX_RECORDER_BUFFERS;
}
However, it seems that the last line of the callback didn't correctly create the new index. The buffers I use are in an array, which is 4 long.
Changing the last line to
queueIndex = (queueIndex + 1) % MAX_RECORDER_BUFFERS;
seems to have fixed the error.

Resources