[libnice] [Nice] Help sending data in reliable mode

Marco Fantasia fantasia.marco at gmail.com
Thu Mar 12 09:12:53 PDT 2015

I'm currenting writing a project in which two nodes communicate each 
other and the controlling agent sends data to the other one.
I've implementing my communication structure using XMPP and I'm able to 
let my nodes communicate.
You can find my project on GitHub: 
Now I have to let the controlling node to share a file to the controlled 
one. I have followed this discussion 
but I have still problems.
Here it is the code that I'm testing:
/ Connect to the signals
"candidate-gathering-done",G_CALLBACK(cb_candidate_gathering_done), NULL);
   g_signal_connect(agent, "component-state-changed", 
G_CALLBACK(cb_component_state_changed), NULL);
   if (controlling){
       //find out file size
       stat("test.txt", &sstr);
       file = fopen("test.txt", "rb");
       g_signal_connect(agent, "reliable-transport-writable", 
G_CALLBACK(writable_cb), file);
   } else{
       file_received = fopen("testFile-received.txt", "ab");
cb_nice_recv(NiceAgent * agent, guint stream_id, guint component_id,
         guint len, gchar * buf, gpointer user_data) {
     if (len == 1 && buf[0] == '\0'){
         if (!file_received){
         g_main_loop_quit (gloop);
     if (file_received) {
         int writen = fwrite(buf, sizeof(char), len, file_received);
         printf("RECEIVED FILE: buf size: %d , writen size: %d\n", len,
     } else {
         perror("ERROR OPENING FILE\n");

writable_cb(NiceAgent *agent, guint sid, guint cid, gpointer data) {
     if (!file){
         file = data;
     char *file_chunk = malloc(CHUNK_SIZE);
     size_t nbytes = 0;
     int sent = 0;
     while ((nbytes = fread(file_chunk, sizeof(char), CHUNK_SIZE, file)) 
 > 0) {
         //printf("SENDING: %s\n",file_chunk);
         //Returns: The number of bytes sent
         sent = nice_agent_send(agent, stream_id, 
NICE_COMPONENT_TYPE_RTP,nbytes, file_chunk);
         /* If return value is -1 it means nothing was sent */
          if (sent==-1)
             sent = 0;
         printf("SENT FILE: chunk size: %zu send result: 
         if (sent < nbytes) {

             /* seek back to the last byte that was actually sent, so it 
              read on the next pass */
             fseek(file, -(nbytes - sent), SEEK_CUR);
             /* break and exit the function, wait for the next signal 
              to continue sending the rest of the data */

     if (sent_byte==total_byte){
         nice_agent_send(agent, stream_id, 1, 1, "\0");
         g_main_loop_quit (gloop);

I've tried this code in a local env and it's all working fine with files 
of different size. (dd if=/dev/urandom of=test.txt count=1024 ibs=1   
---> 1K)
The problems come when i want to use my code in real life.
I've tried to send:

  * 40-byte-size file: OK
  * 1KB-size file: OK
  * +2KB-size file: problems

Output Controlling node with 2K size:

waiting for state READY or FAILED signal...SIGNAL: state changed 1 1 
SENT FILE: chunk size: 1024 send result: 1024.
SENT FILE: chunk size: 1024 send result: 1024.

Output controlled node:

RECEIVED FILE: buf size: 1024 , writen size: 1024
.... hunging up....

What am I missing? Could you help me please?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/nice/attachments/20150312/bafb1a9e/attachment.html>
-------------- next part --------------
 * main.h
 *  Created on: 05/mar/2015
 *      Author: marco

#ifndef MAIN_H_
#define MAIN_H_

#define CHUNK_SIZE 1024

GMainLoop *gloop;
gchar *stun_addr;
guint stun_port;
gboolean controlling;
gboolean exit_thread, candidate_gathering_done, negotiation_done;
GMutex gather_mutex, negotiate_mutex;
GCond gather_cond, negotiate_cond;
gboolean reading_done;

static const gchar *state_name[] = {"disconnected", "gathering", "connecting",
                                    "connected", "ready", "failed"};

static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id,
    gpointer data);
static void cb_component_state_changed(NiceAgent *agent, guint stream_id,
    guint component_id, guint state,
    gpointer data);
void cb_nice_recv(NiceAgent *agent, guint stream_id, guint component_id,
    guint len, gchar *buf, gpointer data);
void writable_cb (NiceAgent *agent, guint sid, guint cid, gpointer data);
static void * example_thread(void *data);

#endif /* MAIN_H_ */
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TestPseudoTcpFileTransfer.c
Type: text/x-csrc
Size: 10649 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/nice/attachments/20150312/bafb1a9e/attachment.c>

More information about the nice mailing list