LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2019, 06:47 PM   #16
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13

Quote:
Originally Posted by rtmistler View Post
Well, you changed a few lines, such as how you use inet_pton() for one. Perhaps you can diff things, or as suggested you can compare with the last working copy.

Suggest you use GBD and single step through this code to unit test it.
I have been, the issue is pointer and heap related, but I am just an idiot pretending so I don't care at this point. Thank you for your help, but at this point I don't care. I'll figure it out or ask my teacher because a lot of this is do to how I view correct code, but fixing this isn't worth getting crap from people to lazy to actually follow a thread.
 
Old 06-22-2019, 07:09 PM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by FOSSilized_Daemon View Post
I have been, the issue is pointer and heap related, but I am just an idiot pretending so I don't care at this point. Thank you for your help, but at this point I don't care. I'll figure it out or ask my teacher because a lot of this is do to how I view correct code, but fixing this isn't worth getting crap from people to lazy to actually follow a thread.
Given that post, this is my last advice.

You're using the inet library call incorrectly. Check the manpage.

Do not believe you should be declaring a struct sockaddr_in within your code, this is already a library defined structure. Hadn't noticed that earlier.

Best of luck with your coding endeavors.

EDIT: Given this editorial addition to post #1. It's very rude to your fellow LQ members. People are here to help, however when they see posts like this, it is difficult for them to commiserate.
Suggest you review the LQ Site Rules:
Quote:
Originally Posted by FOSSilized_Daemon View Post
NOT SOLVED IN ANY WAY SHAPE OR FORM. I GOT IT WORKING BUT NEEEEEEEEEED IT TO WORK A VERY SPECIFIC WAY AND AM GETTING CRAP FOR 100% IN EVERYWAY SHAPE AND FORM UNDERSTANDING WHY IT ISN'T WORKING AND IT'S NOT WORTH THE SPAM IN MY INBOX SO I AM CLOSING THE THREAD.

Last edited by rtmistler; 06-22-2019 at 07:35 PM.
 
Old 06-22-2019, 07:29 PM   #18
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
Given that post, this is my last advice.

You're using the inet library call incorrectly. Check the manpage.

Do not believe you should be declaring a struct sockaddr_in within your code, this is already a library defined structure. Hadn't noticed that earlier.

Best of luck with your coding endeavors.
I can't use pointers, ugh damn libraries. Thank you though, oh I found gethostname() which seems like it may be right. Thank you for the help.
 
Old 06-23-2019, 01:02 AM   #19
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by FOSSilized_Daemon View Post
I have and I have been. I'm... whatever. Then don't respond, if you aren't going to follow the thread and actually see what I am talking about don't respond. "None of this worked" = "none of your suggestions worked". Sorry, but this crap irrates me, people don't pay attention and then get mad at others. Forget I said anything, I have this (I mean I don't I've been at this all day/night but I'll either figure it out or get to class and ask my teacher.)
Take a deep breath. No one is mad.

Quote:
"None of this worked" = "none of your suggestions worked"
Which illustrates the point being made there, and demonstrates that you, again, either did not understand or did not take time to consider it. Lack of useful replies on your part does not constitute lack of attention or laziness by others.

My own advice was, and still is, intended to encourage you to focus on your "methods" which in my considered opinion are causing you (and those offering help) more problems than your code. Please take time to consider the points being made.

Please review the Site FAQ for guidance in posting well formed questions and general forum usage.

All of that said, such outbursts as displayed here and your other post edits are not acceptable behavior in the LQ forums. Referring to help offered by others as crap or spam is completely unacceptable behavior, and disrespectful of the time they take to consider, partially or fully, your questions. Please work to quickly improve that behavior, or take it elsewhere.

Please review LQ Rules for the limits of behavior.

Last edited by astrogeek; 06-23-2019 at 01:35 AM. Reason: Added rules link
 
1 members found this post helpful.
Old 06-23-2019, 04:53 AM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Fix of program-code in post#12
Code:
#include <errno.h>

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <netinet/in.h>
#include <string.h>

#include <unistd.h>
#include "repos.h"

int main()
{
        int socket_desc;
        socket_desc = socket(AF_INET, SOCK_STREAM, 0); /* create socket with IPv4 and TCP protocol */

        if (socket_desc == -1)
                printf("could not create socket\n");

        struct sockaddr_in *serv_addr = calloc(1, sizeof (struct sockaddr_in));

        serv_addr->sin_family = AF_INET;
        serv_addr->sin_port = htons(80);

        if(inet_pton(AF_INET, "79.125.105.113", &serv_addr->sin_addr) <= 0) { /* just set dest here, no const char * incompat issues this way */
                printf("error");
                return -1;
        }

        if (connect(socket_desc, (struct sockaddr *)serv_addr, sizeof(*serv_addr)) < 0) {
                fprintf (stderr, "connect error %d: %s\n", errno, strerror (errno));
                return 1;
        }

        else
                puts("connected");

        free(serv_addr);
        close(socket_desc);

        return 0;
}
 
1 members found this post helpful.
Old 06-23-2019, 10:45 AM   #21
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by NevemTeve View Post
Fix of program-code in post#12
Code:
#include <errno.h>

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <netinet/in.h>
#include <string.h>

#include <unistd.h>
#include "repos.h"

int main()
{
        int socket_desc;
        socket_desc = socket(AF_INET, SOCK_STREAM, 0); /* create socket with IPv4 and TCP protocol */

        if (socket_desc == -1)
                printf("could not create socket\n");

        struct sockaddr_in *serv_addr = calloc(1, sizeof (struct sockaddr_in));

        serv_addr->sin_family = AF_INET;
        serv_addr->sin_port = htons(80);

        if(inet_pton(AF_INET, "79.125.105.113", &serv_addr->sin_addr) <= 0) { /* just set dest here, no const char * incompat issues this way */
                printf("error");
                return -1;
        }

        if (connect(socket_desc, (struct sockaddr *)serv_addr, sizeof(*serv_addr)) < 0) {
                fprintf (stderr, "connect error %d: %s\n", errno, strerror (errno));
                return 1;
        }

        else
                puts("connected");

        free(serv_addr);
        close(socket_desc);

        return 0;
}
You'll have a memory leak since you don't free serv_addr before exiting on failure. Don't forget to close the socket on connection failure as well. You don't really need dynamic memory allocation to begin with, though.

Last edited by individual; 06-23-2019 at 10:49 AM. Reason: Grammar fix.
 
Old 06-26-2019, 11:27 PM   #22
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
EDIT: Given this editorial addition to post #1. It's very rude to your fellow LQ members. People are here to help, however when they see posts like this, it is difficult for them to commiserate.
Suggest you review the LQ Site Rules:
I know it upset the mod to read that, but I really hate when people don't read a post and then reply to it (something this mod did and then posted that he did). This is one of the biggest reasons I left Reddit and refuse to use most forums. This wasn't meant to be directed at everyone (pretty much everyone has been extremely helpful), but it's sort of the unspoken rule of the net (especially IT communities) that if you don't read the post and follow the thread don't reply and get involved. Sorry if he and others were offended, but I was pretty annoyed to get thrashed over something he didn't noticed and out right refused to read. Reading the thread is something people should do... sorta for this exact reason. I shouldn't be expected to take time to write a well formatted and well thought out post just to have people ignore everything and say something 100% irrelevant and incorrect based on evidence and prior posts they WOULD be aware of had they read the thread. In general, if you don't feel like reading and following a thread; it isn't one you should be replying too. I take the time to read others threads and give well thought out responses, I don't think it's too much to expect the same. I find it annoying and rude of others to completely disregard the time and work others take to supply detailed and well formatted posts and replying with a "I think you aren't (or are) doing (or not doing) X (or Y) so (their response)" when if they had read the thread (again this is a HUGE issue that is only getting worse on the net) they would have seen that the writer did or didn't do X or Y. I wouldn't expect someone to just read the headline of an article and then take a side and I don't expect someone to read a header or username and give a reply.
 
Old 06-26-2019, 11:35 PM   #23
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by NevemTeve View Post
Fix of program-code in post#12
Code:
#include <errno.h>

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <netinet/in.h>
#include <string.h>

#include <unistd.h>
#include "repos.h"

int main()
{
        int socket_desc;
        socket_desc = socket(AF_INET, SOCK_STREAM, 0); /* create socket with IPv4 and TCP protocol */

        if (socket_desc == -1)
                printf("could not create socket\n");

        struct sockaddr_in *serv_addr = calloc(1, sizeof (struct sockaddr_in));

        serv_addr->sin_family = AF_INET;
        serv_addr->sin_port = htons(80);

        if(inet_pton(AF_INET, "79.125.105.113", &serv_addr->sin_addr) <= 0) { /* just set dest here, no const char * incompat issues this way */
                printf("error");
                return -1;
        }

        if (connect(socket_desc, (struct sockaddr *)serv_addr, sizeof(*serv_addr)) < 0) {
                fprintf (stderr, "connect error %d: %s\n", errno, strerror (errno));
                return 1;
        }

        else
                puts("connected");

        free(serv_addr);
        close(socket_desc);

        return 0;
}
Thank you so much! Ahhhhhhhhhhhhhhhh it worked, you are the greatest person ever. Thank you thank you thank you, all hail the heap!

Last edited by anon033; 06-27-2019 at 12:10 AM.
 
Old 06-26-2019, 11:55 PM   #24
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by individual View Post
You'll have a memory leak since you don't free serv_addr before exiting on failure. Don't forget to close the socket on connection failure as well. You don't really need dynamic memory allocation to begin with, though.
Personally I prefer dynamics, I know we have 64 bit machines; but code should be as minimal as possible. Thank you so much for catching that, fixes applied

Last edited by anon033; 06-27-2019 at 12:18 AM.
 
Old 06-27-2019, 12:27 AM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: Do you have any more open questions regarding sockaddr_in or connect, or is it actually solved?
 
1 members found this post helpful.
Old 06-27-2019, 12:30 AM   #26
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Talking

Quote:
Originally Posted by NevemTeve View Post
@OP: Do you have any more open questions regarding sockaddr_in or connect, or is it actually solved?
It's finally solved, seriously thank you so much. I am very anal about mem and flow control and this means so much to me, this program is my first C program and it's my baby this means so much. This was bothering me so much, thank you
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing a struct within a struct? smoothdogg00 Programming 3 12-21-2006 01:38 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration