Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

UNIX SOCKET PROGRAMMING IN C

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the syntax (struct sockaddr*)&serveraddr mean in the CONNECT system call in the following UNIX SOCKET PROGRAMMING code in C
WhatsApp-Image-2019-08-06-at-1.11.16-AM.jpeg
[Thumbnail for WhatsApp-Image-2019-08-06-at-1.11.16-AM.jpeg]
 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
serveraddr was declared a struct serveraddr_in.  connect() wants a pointer to a serveraddr, "(struct sockaddr*)&serveraddr" casts serveraddr_in to a serveraddr.  Keep in mind the underlying data doesn't change, just what connect thinks it's being fed.

Think of it as primitive inheritance.  serveraddr_in can be thought of as inheriting serveraddr.

The joys of C

Edit.  Googled for the definitions of serveraddr and serveraddr_in and found this, which explains it better than I ever could:  w00t!
 
Sheriff
Posts: 22017
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jim Venolia wrote:serveraddr was declared a struct serveraddr_in.  connect() wants a pointer to a serveraddr, "(struct sockaddr*)&serveraddr" casts serveraddr_in to a serveraddr.  Keep in mind the underlying data doesn't change, just what connect thinks it's being fed.


A bit more detail: &serveraddr is a struct sockaddr_in* (pointer to a struct sockaddr_in). The connect function expects a struct sockaddr* (pointer to a struct sockaddr). These two cannot be implicitly converted to another, so the cast to struct sockaddr* (pointer to  a struct sockaddr) is necessary.
 
Saloon Keeper
Posts: 22646
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's parse this.

serveraddr is a struct of type sockaddr_in used to hold information about the connection. Specifically,  it has fields defined for protocol mode (AF_INET), host IP address and port.

The "&" operator in C returns the address of an object. That is, a pointer to serveraddr.

But these days C can be fairly picky about what data types your can feed to a function and that includes the connect() function. It wants a pointer to a sockaddr, not a sockaddr_in, so the cast operator (struct sockaddr *) is used to tell the compiler that serveraddr is, in fact a sockaddr struct. Or more precisely, that this pointer to a sockaddr_in can be treated as a pointer to a sockaddr.  As Jim said, a primitive form of inheritance.

That's one of the reasons C++ was developed. Inheritance is built into C++ so you don't have to do as much of this sort of kluge. Casting is a dangerous operation. If you mis-cast an elephant as a chicken and then try to eat it, you may regret it. That's one of the virtues of Java. It reduces the need for casting even further than C++ does and it does more careful checking of what's being cast as what.
reply
    Bookmark Topic Watch Topic
  • New Topic