ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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
#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.
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.
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
@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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.