Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:54:53+00:00 2026-06-13T22:54:53+00:00

I just tried a really simple example to get started with sockets to communicate

  • 0

I just tried a really simple example to get started with sockets to communicate between a C app and Python. Here is the very simple Python script:

import socket

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/demo_socket")
print "Sending..."
s.send("Hello C from Python!")
data = s.recv(1024)
s.close()
print 'Received', repr(data)

And here the corresponding C code, without the headers:

int main(void)
{  
  struct sockaddr_un address;
  int socket_fd, connection_fd;
  socklen_t address_length;
  pid_t child;  

  char buffer[256];
  int n;

  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (socket_fd < 0){
      printf("socket() failed\n");
      return 1;
  } 

  unlink("/tmp/demo_socket");
  memset(&address, 0, sizeof(struct sockaddr_un));

  address.sun_family = AF_UNIX;
  snprintf(address.sun_path, UNIX_PATH_MAX, "/tmp/demo_socket");

  if (bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) {
      printf("bind() failed\n");
      return 1;
  }
  if(listen(socket_fd, 5) != 0) {
      printf("listen() failed\n");
      return 1;
  }

  address_length = sizeof(address);  
  while((connection_fd = accept(socket_fd, 
                   (struct sockaddr *) &address,
                   &address_length)) > -1)
  {
      printf("successfully received data\n");
      bzero(buffer,256);
      n = read(connection_fd,buffer,255);
      if (n < 0) error("ERROR reading from socket");
         printf("Here is the message: %s\n\n", buffer);
      strcpy(buffer, "Hi back from the C world"); 
      n = write(connection_fd, buffer, strlen(buffer));
      if (n < 0) 
         printf("ERROR writing to socket\n");           
      break;      
  }
  close(socket_fd);
  close(socket_fd);
  return(0);
}

The output is then as follows when running them in parallel:

 Successfully received data
 Here is the message: Hello C from Python!

 ERROR writing to socket
 close failed in file object destructor:
 Error in sys.excepthook:

 Original exception was:

So the good news is, receiving stuff from the Python world works fine, but I get an error when trying to write something back, plus it seems I get an exception in the end. Anyone an idea what I am doing wrong here?

EDIT: Thanks guys, I did the correction that you suggested, the message exchange works fine, just the C-application is still causing trouble and I am not sure why…

close failed in file object destructor:
Error in sys.excepthook:

Original exception was:
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:54:54+00:00Added an answer on June 13, 2026 at 10:54 pm

    You are trying to write to listening socket

    write(socket_fd, buffer, strlen(buffer));
    

    You should try

    write(connection_fd, buffer, strlen(buffer));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

really simple question. tried googling I just got a new iPhone 4S and restored
This seems really dumb. I've tried a bunch of different ways and it's just
I just tried to run my test app and I got this error: 2012-06-16
I just tried to rebase a very old branch with a minor modification onto
I'm struggling to get started with the C++ ICU library. I have tried to
Simple problem, though apparently not a simple solution. Example here: http://myhideout.eu/new/ Basically the site
I have just gotten started with the Yii framework, trying to create a simple
Let me preface by I am just starting Python so if this is really
I want to make a really simple iphone app: one screen with a single
Just tried some small graphics application of mine on Windows 7, and I'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.